Wordpress/Custom field Youtube / I want to replace video url (watch?v= with /v/) with php
I want to replace in post and archive custom 开发者_StackOverflowfield the youtube url with php.
- From: http://www.youtube.com/watch?v=FKAjQfL31Dw To:
- http://www.youtube.com/v/FKAjQfL31Dw
My code is:
<?php if ( get_post_meta($post->ID, 'ixosrip', true) ) { ?>
<embed style="width:150px;height:25px;" allowfullscreen="false" type="application/x-shockwave-flash" src="http://www.youtube.com/v/<?php echo get_post_meta($post->ID, "ixosrip", $single = true); ?>&ap=%2526fmt%3D18&autoplay=0&rel=0&fs=1&color1=0xC0C0C0&color2=0xFFFFFF&border=0&loop=0">
<?php } else { ?>
<em>No sound</em>
<?php } ?>
Current i use a javascript, taked from here: find all youtube links with js (jquery) / But its loads very slow my site. Is there any way to do this with php in the custom field?
Thank you. David.
If you can iterate over your custom fields, just do it with a simple PHP str_replace
:
$new_url = str_replace('/watch?v=', '/v/', $old_url);
Where $old_url
is your video's current slug.
UPDATED ANSWER: You could parse the URL, grabbing the v
component:
parse_str($video_url, $params);
$video_id = $params['v'];
$video_url = 'http://www.youtube.com/v/'.$video_id;
You would put this in your loop or your post page template or whatever, where $video_url
is the field containing, well, the YouTube video URL you want to alter.
Aye there, One thing I can think of is making 2 custom fields where one would be the id and would have a value like 'FKAjQfL31Dw'
...and the other would have a value of either 'v/' or 'watch?v=' depending on which one u choose. (can be done via radio buttons)
so ur code might look something like
<embed style="width:150px;height:25px;" allowfullscreen="false" type="application/x-shockwave-flash" src="http://www.youtube.com/
<?php $key='youtube_format'; echo get_post_meta($post->ID, $key, true); ?>
<?php $key='youtube_id'; echo get_post_meta($post->ID, $key, true); ?>
&ap=%2526fmt%3D18&autoplay=0&rel=0&fs=1&color1=0xC0C0C0&color2=0xFFFFFF&border=0&loop=0">
If your trying to do this in archives, you would most likely add this code to content.php or content-video.php.
精彩评论