WordPress.org

Ready to get started?Download WordPress

Forums

How to embed audio / video with URL retrieved from custom fields? (7 posts)

  1. benbkk
    Member
    Posted 11 months ago #

    Hello,

    I'm trying to make my client's life even easier by adding a custom field for audio url and video url using advance custom field / options framework. I have tried both cause none of them working.

    (They somehow got confused why there is post format options but there is nowhere to put the URL or anything, so what is the difference? - I think quite logical confusion by the way)

    It has no problem returning the value but only shown as the url instead of a player embedded.

    However if I put the url on the content editor, it works.

    Why this happen and how to solve it? Any help would be very much appreciated.

    Thanks

  2. Devin Price
    Member
    Posted 11 months ago #

  3. benbkk
    Member
    Posted 11 months ago #

    Hi Devin,

    Thanks for the reply.
    Yes I did try with shortcode also, but didn't work either. The difference is only now it becomes a link rather than just an echo. But still its not embedded.

    It only works if I call the_content();
    I have tried put it as excerpts, it didn't work either.

    Its that the way it is or have I missed something?

    Thanks again

  4. Devin Price
    Member
    Posted 11 months ago #

    Not sure why that wouldn't work. But you could also just try inserting the URL into the_content directly before it renders using a filter:

    http://codex.wordpress.org/Plugin_API/Filter_Reference/the_content

  5. benbkk
    Member
    Posted 11 months ago #

    Hi Devin,

    Thanks for your help.
    Turns out there was small mistake in the field video_url. It works after I fixed it,

    BUT... it only works with [video src=" "] shortcode. That means, it is still different from the_content. Because on the_content, URL only is enough to render a player.

    It doesn't work either if I put common external source like youtube, even if i use [youtube=] shortcode.

    Means I still need to find another way with this filter you recommended,
    however... I've spent hours but still can't figure out how to use the filter for this purpose

    I don't quite get it. If I use this filter, to my understanding it will add the URL to the_content. But wouldn't that be I still need to call the_content(); instead of the custom field itself? -which didn't work anyway after I tried lol!

    Could you please please show an example how to use it for my purpose? :)

    Many thanks
    Ben

  6. benbkk
    Member
    Posted 11 months ago #

    Hi again Devin,

    Start to get it but still not working as expected. Here's my code

    // video url as content
    function video_url_content($content) {
    	global $post;
    	if ( has_post_format('video', $post_id) ) {
    	$video_url = get_post_meta($post->ID, '_format_video_embed', true);
    	$content .= $video_url;
    }
    return $content;
    }
    add_filter( 'the_content', 'video_url_content');

    with the above, I got the URL added to the_content but it is not rendered as player, only URL being echoed.

    If I put the shortcode there so

    //video url as content
    function video_url_content($content) {
    	global $post;
    	if ( has_post_format('video', $post_id) ) {
    	$video_url = get_post_meta($post->ID, '_format_video_embed', true);
    	$video = do_shortcode('[video src="' . $video_url . '"]');
    	$content .= $video;
    }
    return $content;
    }
    add_filter( 'the_content', 'video_url_content');

    I can get the video embedded, but not with external source video (youtube, etc), even if I change the shortcode to [youtube=...] - this will result in a youtube link displayed, not the player.

    What have I missed?

    Many thanks
    Ben

  7. benbkk
    Member
    Posted 11 months ago #

    Hi again Devin,

    Geez i hope im not too much on this. I finally able to get it working with the below

    function video_url_content($content) {
    	global $post;
    	if ( has_post_format('video', $post_id) ) {
    	$supported = wp_get_video_extensions();
    	$video_url = get_post_meta($post->ID, '_format_video_embed', true);
    	$video_pathinfo = pathinfo($video_url);
    	$video_ext = $video_pathinfo['extension'];
    		if ( in_array($video_ext, $supported) ) {
    			$video = do_shortcode('[video src="' . $video_url . '"]');
    		} else {
    			$video = wp_oembed_get($video_url);
    		}
    	$content .= $video;
    }
    return $content;
    }
    add_filter( 'the_content', 'video_url_content');

    So if the $video_url has one of the standard extensions that is supported by wp, it will do the video src shortcode. If not, it will try to oembed the url.

    Is this ok? or would you know any other/better solution?

    Thanks
    Ben

Reply

You must log in to post.

About this Topic