To circumvent this, you can use
strpos()
withthe_posts
hook, which gets fired before wp_head. But one of the drawbacks is that its an extra pass at the data.
You’re essentially doing the same inside the callback you’re hooking onto wp_head
, i don’t see the difference.. (please clarify if you think otherwise)..
Whether you hook onto the_posts
or wp_head
you’re still iterating over the same array data, ie. $posts
..
I personally don’t see the issue with setting a flag inside the the_posts
hook and if there’s a concern that the hook may get called multiple times, simply check for the flag at the start of the callback and return if it’s already set(ensuring only one iteration over that array).
I’m actually using this approach in a plugin i’m waiting to release, and i also realised the method Scribu posted about doesn’t really work when you’re using CSS, which can’t go in the foot. Scribu mentioned possibly injecting CSS into the head with JS, but i personally find that somewhat hacky(my personal opinion).
I have a function hooked onto the_posts
which goes like this..
public function on_the_posts( $posts ) {
if( empty( $posts ) || $this->has_tabs )
return $posts;
// trimmed code not relevant to the example
foreach( $posts as $post ) {
if( !stripos( $post->post_content, '[end_tabset]' ) )
continue;
$this->has_tabs = true;
}
return $posts;
}
How would this be any different inside a callback on wp_head
, i’m still going to need to iterate over $posts
aren’t i?
Is this a bad implementation of wp_enqueue_script for conditional usage?
No, i don’t think so personally.