首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将默认播放器更改为JW player

将默认播放器更改为JW player
EN

Stack Overflow用户
提问于 2016-11-26 04:47:07
回答 1查看 1.4K关注 0票数 1

我想播放我所有的预览视频链接,只与JW播放器。我安装jw & jw 7用于wp。但它只能影响到新的帖子!如何用默认的WordPress视频播放器替换它?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-26 09:07:46

这里有一个按需应用的小脚本(取消注释delete_option行再次执行此操作),将其放在functions.php中。

此脚本将找到任何帖子,并将一个旧的短代码标记替换为一个新的。

它使用get_shortcode_regex()has_shortcode来检测需要注册的短代码(通过add_shortcode()添加,参见最后的dummy_shortcode() )。

代码语言:javascript
复制
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()中注册这些短代码才能被识别。我们创建一个假的短代码,我们正在寻找的旧标签将被识别。

代码语言:javascript
复制
if(get_option('se_40815010')!= true){
    add_shortcode('fakegallery', 'dummy_shortcode');
}
function dummy_shortcode($content){
    return 'hello world';
}

希望能帮上忙。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40815010

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档