iridian
iridian

Reputation: 709

Limiting Wordpress' "the_content" filter

Using Wordpress you can add a hook to "the_content". In my code it looks like this.

add_filter("the_content", some_func);

So, in Wordpress every time there is a post "some_func" gets ran. So, let's say "some_func" looked like this.

function some_func( $content ) {
    return $content . "<div id='my_div'></div>";
}

Using an ID attribute means there can only be one HTML element. What happens is, this function gets ran for every post there is in the database. That means if I had two posts then I have two divs with the ID "my_div" and if I have Javascript that relies on those IDs then the Javascript won't act correctly. Same for CSS.

So, what I need is a way to have this function ran once and no more than once. Anyone know of a way, maybe a wrapper, or a different filter I should be using?

Upvotes: 1

Views: 900

Answers (1)

bozdoz
bozdoz

Reputation: 12880

I haven't tested this, but perhaps you can add this to the top of your some_func function:

remove_filter( 'the_content', 'some_func' );

In theory, it would call the filter once, and remove the filter after the first call.

Upvotes: 2

Related Questions