我有一个插件,可以在发布帖子时(不保存为草稿)向自定义字段添加文本和URL。我想获取/制作一个函数或插件,在自定义字段中进行搜索,并从URL侧面加载图像,然后将该图像添加为特色图像。
在进行搜索时,我从另一个StackExchange帖子中找到了此功能,该帖子旨在从Youtube视频中获取缩略图:
function set_youtube_as_featured_image($post_id) {
// only want to do this if the post has no thumbnail
if(!has_post_thumbnail($post_id)) {
// find the youtube url
$post_array = get_post($post_id, ARRAY_A);
$content = $post_array[\'post_content\'];
$youtube_id = get_youtube_id($content);
// build the thumbnail string
$youtube_thumb_url = \'http://img.youtube.com/vi/\' . $youtube_id . \'/0.jpg\';
// next, download the URL of the youtube image
media_sideload_image($youtube_thumb_url, $post_id, \'Sample youtube image.\');
// find the most recent attachment for the given post
$attachments = get_posts(
array(
\'post_type\' => \'attachment\',
\'numberposts\' => 1,
\'order\' => \'ASC\',
\'post_parent\' => $post_id
)
);
$attachment = $attachments[0];
// and set it as the post thumbnail
set_post_thumbnail( $post_id, $attachment->ID );
} // end if
} // set_youtube_as_featured_image
add_action(\'save_post\', \'set_youtube_as_featured_image\');
function get_youtube_id($content) {
// find the youtube-based URL in the post
$urls = array();
preg_match_all(\'#\\bhttps?://[^\\s()<>]+(?:\\([\\w\\d]+\\)|([^[:punct:]\\s]|/))#\', $content, $urls);
$youtube_url = $urls[0][0];
// next, locate the youtube video id
$youtube_id = \'\';
if(strlen(trim($youtube_url)) > 0) {
parse_str( parse_url( $youtube_url, PHP_URL_QUERY ) );
$youtube_id = $v;
} // end if
return $youtube_id;
} // end get_youtube_id
这里唯一缺少的是搜索自定义字段并从中提取URL的功能,尽管该功能在查找URL方面的方向是正确的。有人能告诉我这里的方向吗?文本在自定义字段中的格式如下所示。它是唯一的URL,并带有一个img html标记。
<div class="newsread " style="margin-bottom: 20px;">
<div id="ncbubuhome">
<p>Erdenet Mining Corp. is the Mongolian and Russian joint venture to hold copper and molybdenum mining and production of copper and molybdenum concentrates and is counted as one of the biggest manufactures in Asian region. The corporation held its annual report last Friday in Erdenet city.</p>
<p>We are delivering the numbers of Erdenet Mining Corp. in comparison with Oyu Tolgoi LLC. Although those entities have differences in their operation period and investment agreement, those two are considered as biggest projects for Mongolia.</p> <p><img alt="" src="http://gstat.mn/newsn/images/ck/2015/03/31/Erdenet_Oyutolgoi_Eng-171337-1287730770.jpeg" style="height:3572px; width:825px"></p> </div>
我一直在对此大发雷霆,并尝试了一些方法:
将自定义字段的内容复制到贴子正文,然后使用插件自动设置外部图像(我认为事情的实例化顺序不对,我无法使其正常工作)
在文章正文中添加一个带有占位符的短码,该占位符将拉入自定义字段,但无法确定如何在文本编辑器中使用占位符非常感谢您的帮助!我已经试了三天了-罗伯特