0

I am currently using a 'the_content' filter to add some HTML to the bottom of my posts and pages.

But, I want to add another 'the_content' filter to a handful of specific pages (using page IDs).

Is it ok to use multiple 'the_content' filters?

here is my current code:

add_filter( 'the_content', 'my_content_filter' );

function my_content_filter( $content ) {
if ( is_single() || is_page()) {
  $content .= '[html goes here]';
}
return $content;
}

2 Answers 2

2

You can add as many page i.d's and conditional tags as you like.

You can use the_content filter in as many functions as you like as long as they use different function names.

However, when you add HTML or text content after posts, you need to use code like this:

add_filter( 'the_content', 'your_content_filter' );

function your_content_filter( $content ) {

if ( is_single('007') || is_page('100') ) {

$your_content = 'ADD Your HTML Content Here';

$content .= $your_content;
}
return $content;
}

You could also wrap your HTML content in a class like this:

$your_content = '<div class="your-class">Add Your HTML Content Here</div>';

You can also add a 3rd parameter for positioning priority if you want to execute the function after other functions using the same hook.

add_filter( 'the_content', 'your_content_filter', 15 );

This function will run after any others using the same hook as 10 is the default when not specified as it uses 15. If you want to run the function before others using the same filter, you can use a priority of less then 10.

1

Of course, you can—and that's exactly what filters are there for.

the_content already has a number of filters attached, as you can see with the following:

var_dump($GLOBALS['wp_filter']['the_content']);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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