i'd只想播放我的所有预览视频链接。我安装JW Player&JW播放器7 for WP。但这只会在新帖子中影响!如何用默认的WordPress视频播放器替换它?
在这里有一个小脚本以按需应用(uncomment delete_option Line再次执行此操作),将其放入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';
}
希望它有帮助。