• Resolved VitalRemains

    (@vitalremains)


    I’ve seen many posts that share this title but I want something different than them.

    I am currently writing a plugin so that only logged-in users can see specific pages. I tried to achieve this by adding a simple if-block with a check to these pages if the current user is logged-in.

    On my menu page, all pages are listed with a checkbox beside them. Now comes the part where I need help. I read about using add_filter or enqueue_script to insert content into a page. But it seems I can’t specify on what page to use these functions without writing them directly into the pages.

    So is there a way I can alter the content of a page dynamically, maybe based on the id or something?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    add_filter() would be much preferable to enqueue_script() in this case. Adding, or ‘hooking’ filters and actions is a fundamental part of altering WordPress functionality, especially for plugin authors. See Plugin API for more on this.

    The filter you are interested in this case is ‘the_content’. The current post’s content is passed to the function you add. Use your if-block to decide whether to return the passed content or an empty string (or maybe something like “Please log in”).

    Thread Starter VitalRemains

    (@vitalremains)

    Thanks for the reply. I actually tried to use this but ran into a dead end, because there’s no way to tell add_filter() to dynamically apply.
    The solution I came up with is using add_post_meta() on the pages I want to be disabled for guests, since this function has a page ID parameter.
    And then, in the add_filter() function I checked for the value of the meta tag to determine whether the page is visible.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Insert content into specific page’ is closed to new replies.