I have a custom meta box set up that allows users to paste the Youtube URL of a video so that it can be embedded into a post/page.
The meta box can be repeated so that a user can add as few or as many URL’s as they wish so I’m using this code snippet to display each as a list item;
<?php
$video = get_post_meta($post->ID, 'youtube-url');
foreach ($video as $vid) {
echo '<li>'.$vid.'</li>';
}
?>
Is there a way that I can run the_content filter on each individual list item so that I can make use of oEmbed that is shipped with WordPress?
Or perhaps there’s a more efficient way…
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
All you need to do use apply_filters.
foreach ($video as $vid) {
echo '<li>'.apply_filters('the_content',$vid).'</li>';
}
It may be more efficient to concatenate a string and then run the filter on the whole thing.
$lis = '';
foreach ($video as $vid) {
$lis .= '<li>'.$vid.'</li>';
}
echo apply_filters('the_content',$lis);
I haven’t benchmarked the one versus the other but given that the latter calls the filter once and the former many times, I’d bet on the latter.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0