从wordpress视频URL字段(在JW播放器脚本中)获得Javascript中的值

Get the value in Javascript from wordpress video URL field (in JW Player script)

本文关键字:脚本 获得 Javascript 播放器 JW 视频 wordpress URL 字段      更新时间:2023-09-26

我有一个名为Aruna的Wordpress主题(主要用于视频)。在帖子编辑器中,一旦我想制作一个视频,我只需将YouTube URL粘贴到该字段并发布它。

我想集成JW播放器(而不是YouTube播放器)自动在我所有的帖子已经发布。

如果将此代码放在post文本内容中,则加载视频没有问题:

<div id="myElement"></div>
<script>
    jwplayer("myElement").setup({
        file: "http://www.youtube.com/watch?v=8CjdLYBDUqw",
        width: 640,
        height: 360
    });
</script>

代码一切正常。它用JW播放器加载YouTube视频。

我想编辑content-video.php(这是该帖子的视频部分),并使其能够从我通常使用的相同字段中选择url。

使用Firebug,我可以看到编辑器中字段的名称:

<input type="text" value="http://www.youtube.com/watch?v=E8k20kHZjug" id="_video_externalvideo" name="_video_externalvideo" class="cmb_oembed">

在默认的content-video.php中,它通过php获取URL:

<?php } 
            if( (isset($externalvideo) && $externalvideo != '') || (isset($localvideo) && $localvideo != '') ) { ?>
                <div class="video-wrap">
                    <?php 
                    if(isset($externalvideo) && $externalvideo != '') {
                        if(isset($GLOBALS['wp_embed'])) {
                            $embed = $GLOBALS['wp_embed']->autoembed($externalvideo);
                            $embed = str_replace("feature=oembed", "feature=oembed&amp;wmode=transparent", $embed);
                            echo $embed;
                        }
                    }
                    elseif(isset($localvideo) && $localvideo != '') { 
                        echo apply_filters('the_content', '[video width="650" height="360" src="' . $localvideo . '"]');
                    }
                    ?>

我想我应该把php代码去掉,改成:

<script>
    jwplayer("myElement").setup({
        file: "http://www.youtube.com/watch?v=8CjdLYBDUqw",
        width: 640,
        height: 360
    });
</script>

我的问题是:我如何在部分编辑脚本:

file: "http://www.youtube.com/watch?v=E8k20kHZjug"

变成自动能够从相同的字段,我在一个默认的视频帖子中使用的URL ?

我应该在脚本内使用php函数还是只是javascript函数?

我什么都试过了,到目前为止都没有效果。我不太了解phpjavascript

我希望你们中的一个能帮助我。

我觉得这样更接近解决方案:

<script>
    jwplayer("myElement").setup({
        file: function 
      .getElementById("_video_externalvideo"),
        width: 640,
        height: 360
    });
</script>

提前谢谢你。

亚历克斯

$externalvideocontent-video.php中的定义:

<?php global $redux_demo; $localvideo = get_post_meta($post->ID, '_video_localvideo', true); $externalvideo = get_post_meta($post->ID, '_video_externalvideo', true); $nsfw = get_post_meta($post->ID, '_video_nsfw', true); if(is_user_logged_in() ) { $userid = wp_get_current_user(); if(get_user_meta($userid->ID, '_nsfw', true) != 1) { $nsfw = 2; } } $class = 'main-post'; if(is_single() ) { $class .= ' post-page'; } ?> 

更新:感谢@SilverKenn的有用答案,我还没有解决这个问题,但我们离解决办法越来越近了。我把php代码放在我的模板中,但在最后它被读成这样:

<script type="text/rocketscript" data-rocketoptimized="true">jwplayer("player-viralizzato").setup({
    file:http://www.youtube.com/watch?v=txPQC8NB_-M,
    width: 640,
    height: 360
});</script>

代替:

<script>jwplayer("player-viralizzato").setup({
    file:"http://www.youtube.com/watch?v=txPQC8NB_-M",
    width: 640,
    height: 360
});</script>

如果我在Firebug中编辑代码:<script type="text/rocketscript" data-rocketoptimized="true"><script>file:http://www.youtube.com/watch?v=txPQC8NB_-M, ~ file:"http://www.youtube.com/watch?v=txPQC8NB_-M",它会正常工作的,知道吗?再次感谢

如果视频url已经在你想要显示播放器的页面上,

你可以简单地像这样使用Jquery,

(function($){ 
    // Get the video URL
    var video_uri = $('#_video_externalvideo').val();
    jwplayer('myElement').setup({
        file: '"' +video_uri + '"',
        width: 640,
        height: 360
    });
})(jQuery)

或在模板中使用PHP

$externalvideo = get_post_meta($post->ID, '_video_externalvideo', true);
echo '<script>'."'n";
echo "'t".'jwplayer("myElement").setup({'."'n"; 
echo "'t't".'file:"'.$externalvideo.'",'."'n";   
echo "'t't".'width: 640,'."'n"; 
echo "'t't".'height: 360'."'n"; 
echo "'t".'});'."'n";
echo '</script>'."'n";