I have a script that adds some styles to OEmbedded content. At the moment its running on every page. I was wondering if anyone knew how to only enqueue the script if the page has an OEmbed on it.

I have no code to show as google doesn't return anything on the topic

  • What kind of oembedded content is this? Do you have code for the oembedding we can see that might aid in generating the code to enqueue such a script/style? Do you have any ideas or starting points you think might be helpful for others researching an answer? – Tom J Nowell Apr 28 '14 at 15:29
  • Fair enough on the edit sorry I was in a bad mood. I've developed this really negative feeling about using this site, but its really good and helpful and the people on it are quite cool, so i need to change that :) – danbrown Apr 28 '14 at 18:27
  • I assumed WordPress used the video or embed shortcode when spitting out the content. You may be able to use a version of this function to test if a oembed is being used. – Howdy_McGee Apr 30 '14 at 14:08
  • Things to note for potential answers, oembeds dont use shortcodes ( though shortcodes can use oembeds, e.g. the oembed shortcode ), and there is a caching mechanism for oembeds in post content – Tom J Nowell May 2 '14 at 11:03

Well the first thing to consider is that the wp_enqueue_scripts action hook fires before the filters on the page/post content are run.

So I would add a function to the wp action hook and test the content for oembeds there.

add_action( 'wp', 'your_oembed_test_function' );
function your_oembed_test_function(){
    //earliest reliable hook to get $post variable
    global $post;

    //do your tests with $post content

    if( $oembed_exists_results ) wp_enqueue_script( $your_handle );


Because WP fires before wp_enqueue_scripts you will be able to choose which scripts/styles you want to show based on which oembeds you find in the content.

  • Ah ok so would you check the content for whether it contained an url with an oembed in it? E.g. The oembed url is site.com/events/open-evening so you could just do a string check for site.com/events, and if it contained that, then you'd enqueue the event oembed script / style...? – danbrown May 13 '14 at 14:30
  • Yes, that is what I was getting at. Any filters specific to oembed are not fired until just before the content is being displayed, so that is why I am suggesting doing the check earlier. – Douglas.Sesar May 13 '14 at 17:46

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.