我想播放我所有的预览视频链接,只与JW播放器。我安装jw & jw 7用于wp。但它只能影响到新的帖子!如何用默认的WordPress视频播放器替换它?
发布于 2016-11-26 09:07:46
这里有一个按需应用的小脚本(取消注释delete_option行再次执行此操作),将其放在functions.php中。
此脚本将找到任何帖子,并将一个旧的短代码标记替换为一个新的。
它使用get_shortcode_regex()和has_shortcode来检测需要注册的短代码(通过add_shortcode()添加,参见最后的dummy_shortcode() )。
add_action('init', 'se_40815010');
function se_40815010(){
if(get_option('se_40815010') == true){
return;
}
$old_tag = 'video';
$new_tag = 'jw7-video';
$args = array(
'post_type' =>'post',
'posts_per_page'=> -1,
'post_status' => 'any'
);
$posts = new WP_Query($args);
foreach($posts->posts as $post){
if(has_shortcode($post->post_content, $old_tag)){
$pattern = get_shortcode_regex();
if ( preg_match_all( '/'. $pattern .'/s', $post->post_content, $matches )
&& array_key_exists( 2, $matches )
&& in_array( $old_tag, $matches[2] ) ) {
$new_content = str_replace($matches[2][0], $new_tag, $post->post_content);
$post_update = array(
'ID'=> $post->ID,
'post_content'=> $new_content
);
$update = wp_update_post($post_update, true);
if($update && !is_wp_error($update)){
$result .= 'Post ID '.$post->ID.', gallery shortcode modified.</br>';
}
else{
$error_string = $update->get_error_message();
$result .= '<div id="message" class="error"><p>' . $error_string .'</p></div>';
}
}
}
}
wp_mail(get_option('admin_email'), 'Shortcode replace', $result);
update_option('se_40815010', true);
}
// delete_option('se_40815010');使用has_shortcode()时,需要在add_shortcode()中注册这些短代码才能被识别。我们创建一个假的短代码,我们正在寻找的旧标签将被识别。
if(get_option('se_40815010')!= true){
add_shortcode('fakegallery', 'dummy_shortcode');
}
function dummy_shortcode($content){
return 'hello world';
}希望能帮上忙。
https://stackoverflow.com/questions/40815010
复制相似问题