php - Copy a URL from a post to a custom field in Wordpress -
i'm using ifttt.com import youtube videos wordpress. 1 of elements importing url of youtube video. copy url custom field theme comes display embedded video. realize can import embedded video ifttt post prefer formatting theme provides when using video embed custom fields created.
example:
url imported post looks this:
http://www.youtube.com/watch?v=qcvqpcy1au4
(example taken ifttt hope video selection doesn't annoy much)
i need copy videoembedcustomfield this: http://www.youtube.com/embed/qcvqpcy1au4
i unfortunately not have code work from. looking plugins update posts. or advice appreciated.
- import video youtube wordpress post using ifttt.com. works.
on import, need function can recognize url imported post
example: <a href="http://www.youtube.com/embed/qcvqpcy1au4" id="youtubelink" class="ytlink">youtube link</a>
if url exists transform link
http://www.youtube.com/embed/qcvqpcy1au4
and insert in custom field
so, need function in appropriate place conversion via regex:
public static function convertyoutube($original) { $pattern = '/http:\/\/www\.youtube\.com\/watch\?v=([\w]+)/'; $replacement = 'http://www.youtube.com/embed/${1}'; return $embedlink = preg_replace($pattern,$replacement,$original); }
the pattern
dictates part of input want capture. in case it's ([\w]+)
clause, translating "grab sequence of 1 or more alphanumeric characters" happen come after (properly escaped) constant youtube url.
preg_replace
php function allow take part grabbed , replace rest. have converted url can drop whatever field need.
Comments
Post a Comment