0

I'm creating a plugin that needs to replace the_content of only two specific pages that it created upon activation.

Here is my code:

add_filter('the_content', 'emd_content');  

function emd_content() {
  if ( is_page('Member Directory') ) {
      include dirname( __FILE__ ) . '/content-members.php'; 
    }
    elseif ( is_page('Contact Members') ) {
      include dirname( __FILE__ ) . '/content-member-contact-form.php'; 
    }
    else {
        the_content();
    }
}

The code above gives me a 500 Internal Server Error for pages other than Member Directory and Contact Members, which both work properly.

If I remove else { the_content(); }, then the Member Directory and Contact Members work correctly, but all other pages don't display anything where the_content should be displayed.

I also tried wrapping the add_filter() within if(is_page('Member Directory') || is_page('Contact Members')) { }. the_content worked for other pages, but not for the two created by the plugin.

What is a better/proper way to replace the_content for these two pages using my plugin?

3

You need to return the filtered content, try like this:

add_filter('the_content', 'emd_content');  

function emd_content( $content ) {
  if ( is_page('Member Directory') ) {
      include dirname( __FILE__ ) . '/content-members.php'; 
    }
    elseif ( is_page('Contact Members') ) {
      include dirname( __FILE__ ) . '/content-member-contact-form.php'; 
    }
    else {
        return $content;
    }
}

But the better way is to use custom page template for those pages.

2
  • Thank you for your answer! It is so similar to the solution I decided to go with that I accepted it as the correct answer. I read elsewhere that custom page templates are recommended for this, but it just seems like overkill when just needing to replace content for two pages. Thanks again! Oct 26 '14 at 15:04
  • The link to the "custom page template" is broken.
    – StR
    Jan 21 '20 at 22:07
3

Ashok's answer works, but I also found the following, which is what I decided to go with.

In a nutshell, a filter must always return a value - the_content() just echos and is likely causing an infinite loop since it would call your filter.

function emd_content($content) {
  if ( is_page('Member Directory') ) {
      $content = include dirname( __FILE__ ) . '/content-members.php'; 
    }
    elseif ( is_page('Contact Members') ) {
      $content = include dirname( __FILE__ ) . '/content-member-contact-form.php'; 
    }
    else {
      $content = $content;
    }
      return $content;
}
1
  • 1
    Isn't the else { $content = $content } redundant? If we didn't change the content in the first if or in the elseif, it hasn't changed so just return it as it was passed to the function.
    – Prdufresne
    Jul 8 '19 at 3:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.