0

I'm running the YouTube Video Fetcher plugin and it fetches youtube videos and displays it on your website. However, instead of displaying the video after the post, I want to hook the code into a custom container.

Here is the function in the plugin used to display the video after the post:

//The below function is used to display the video after the post.
function append_the_video($content){
    return $content.youtube_video();
}
add_filter('the_content', 'append_the_video');

I tried swapping out the following line:

add_filter('the_content', 'append_the_video');

with this line but it is not working:

add_action('my_custom_hook_name', 'append_the_video');
0

Something like what you are doing should work but I think you have a couple of things going wrong.

First, it sounds like you've hacked the plugin. Don't do that, just remove the callback from the the_content hook.

In your theme's functions.php add:

remove_filter('the_content', 'append_the_video');

The youtube_video() function depends on the $post variable. That means it is an essentially "Loop-only" function. You can't move it outside the Loop if that is what you are trying to do. It won't work correctly.

You can run it in a different location by adding your own callback to the_content.

function my_prepend_the_video($content){
    return youtube_video().$content;
}
add_filter('the_content', 'my_prepend_the_video');

And of course, you can add other markup if you need to.

2
  • Thanks for the feedback. Any chance I can output the youtube video into a div container? For instance, I run the following function (as an example) to pull a custom field value into a custom container. function post_spotify_embed() { global $post; $spotifyembed = get_post_meta($post->ID, 'spotify-embed', true); if ($spotifyembed) echo '<iframe src="https://embed.spotify.com/?uri=' . $spotifyembed . '" width="640" height="720" frameborder="0" allowtransparency="true"></iframe>'; } add_action('hook_top_embed_code','post_spotify_embed',0); Or is this still not possible? – Aaron Mar 10 '14 at 4:33
  • Of course, add the div markup to the filter callback. – s_ha_dum Mar 10 '14 at 4:35

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.