admin管理员组文章数量:1026989
I use this code to add a post to Wordpress:
$new_post = array(
'post_title' => $title,
'post_content' => "test ",
'post_status' => 'publish',
'post_author' => $author,
'post_type' => 'post'
);
wp_insert_post($new_post, true);
The problem is that youtube links will not be embedded. However, if I post it manually to WordPress then the links will be automatically embedded.
What do I need to do in order for WordPress to automatically transform youtube URL to embedded videos?
Another answer from this site says to use wp_insert_post() but how would I extract youtube URL's from the post content considering there are various youtube URL syntaxes. Embeds page on WP Codex says to just put the "video URL into the content area". I am surprised there is not a function to automatically embeds all youtube videos when using wp_insert_post() ...unless I am missing something?
Thanks
I use this code to add a post to Wordpress:
$new_post = array(
'post_title' => $title,
'post_content' => "test https://www.youtube/watch?v=4g3e8XwVIYc https://youtu.be/LPpW_8c5jE4",
'post_status' => 'publish',
'post_author' => $author,
'post_type' => 'post'
);
wp_insert_post($new_post, true);
The problem is that youtube links will not be embedded. However, if I post it manually to WordPress then the links will be automatically embedded.
What do I need to do in order for WordPress to automatically transform youtube URL to embedded videos?
Another answer from this site says to use wp_insert_post() but how would I extract youtube URL's from the post content considering there are various youtube URL syntaxes. Embeds page on WP Codex says to just put the "video URL into the content area". I am surprised there is not a function to automatically embeds all youtube videos when using wp_insert_post() ...unless I am missing something?
Thanks
Share Improve this question asked Apr 12, 2019 at 1:14 32454325342523245432534252 1174 bronze badges1 Answer
Reset to default 0Youtube urls and other services that embed content in post content work via a feature called oEmbed. You need to use wp_oembed_get()
to fetch the embedded content for this to work.
In your case, you're inserting content directly, so you probably don't want to just call wp_oembed_get()
and call it a day. That won't work. It's better to run the functions WordPress has built in to support post content.
apply_filters( 'the_content', $content );
This runs when you output the_content()
in your template and includes things like shortcode expansion and oembed discovery. But, it only works if your content is formatted in the right way. You said, "Embeds page on WP Codex says to just put the "video URL into the content area" which is correct with one clarifying point:
Make sure the URL is on its own line and not hyperlinked.
Your insert will look like this:
$new_post = array(
'post_title' => $title,
'post_content' => 'test
https://www.youtube/watch?v=4g3e8XwVIYc
https://youtu.be/LPpW_8c5jE4',
'post_status' => 'publish',
'post_author' => $author,
'post_type' => 'post',
);
wp_insert_post( $new_post, true );
Notice the new lines for your post content. This will convert these youtube urls into the appropriate embeds when it's output in your template.
I use this code to add a post to Wordpress:
$new_post = array(
'post_title' => $title,
'post_content' => "test ",
'post_status' => 'publish',
'post_author' => $author,
'post_type' => 'post'
);
wp_insert_post($new_post, true);
The problem is that youtube links will not be embedded. However, if I post it manually to WordPress then the links will be automatically embedded.
What do I need to do in order for WordPress to automatically transform youtube URL to embedded videos?
Another answer from this site says to use wp_insert_post() but how would I extract youtube URL's from the post content considering there are various youtube URL syntaxes. Embeds page on WP Codex says to just put the "video URL into the content area". I am surprised there is not a function to automatically embeds all youtube videos when using wp_insert_post() ...unless I am missing something?
Thanks
I use this code to add a post to Wordpress:
$new_post = array(
'post_title' => $title,
'post_content' => "test https://www.youtube/watch?v=4g3e8XwVIYc https://youtu.be/LPpW_8c5jE4",
'post_status' => 'publish',
'post_author' => $author,
'post_type' => 'post'
);
wp_insert_post($new_post, true);
The problem is that youtube links will not be embedded. However, if I post it manually to WordPress then the links will be automatically embedded.
What do I need to do in order for WordPress to automatically transform youtube URL to embedded videos?
Another answer from this site says to use wp_insert_post() but how would I extract youtube URL's from the post content considering there are various youtube URL syntaxes. Embeds page on WP Codex says to just put the "video URL into the content area". I am surprised there is not a function to automatically embeds all youtube videos when using wp_insert_post() ...unless I am missing something?
Thanks
Share Improve this question asked Apr 12, 2019 at 1:14 32454325342523245432534252 1174 bronze badges1 Answer
Reset to default 0Youtube urls and other services that embed content in post content work via a feature called oEmbed. You need to use wp_oembed_get()
to fetch the embedded content for this to work.
In your case, you're inserting content directly, so you probably don't want to just call wp_oembed_get()
and call it a day. That won't work. It's better to run the functions WordPress has built in to support post content.
apply_filters( 'the_content', $content );
This runs when you output the_content()
in your template and includes things like shortcode expansion and oembed discovery. But, it only works if your content is formatted in the right way. You said, "Embeds page on WP Codex says to just put the "video URL into the content area" which is correct with one clarifying point:
Make sure the URL is on its own line and not hyperlinked.
Your insert will look like this:
$new_post = array(
'post_title' => $title,
'post_content' => 'test
https://www.youtube/watch?v=4g3e8XwVIYc
https://youtu.be/LPpW_8c5jE4',
'post_status' => 'publish',
'post_author' => $author,
'post_type' => 'post',
);
wp_insert_post( $new_post, true );
Notice the new lines for your post content. This will convert these youtube urls into the appropriate embeds when it's output in your template.
本文标签: embedHow to auto embedded youtube with wpinsertpost()
版权声明:本文标题:embed - How to auto embedded youtube with wp_insert_post() 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745596434a2158199.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论