简体   繁体   English

在Wordpress中添加条件脚本

[英]Adding conditional scripts in wordpress

How to add scripts which contains checking inside a plugin for eg 如何添加包含检查插件内部脚本的脚本,例如

<!--[if lt IE 9]> 
<script src="<?php echo plugins_url('/js/html5shiv.js'); ?>" type="text/javascript"></script>
<script src="<?php echo plugins_url('/js/css3-mediaqueries.js'); ?>" type="text/javascript"></script> 
<script src="<?php echo plugins_url('/js/respond.js'); ?>" type="text/javascript"></script>   
<![endif]-->  

using wp_register_script() and wp_enqueue_script('') 使用wp_register_script()和wp_enqueue_script('')

<?php
    global $is_IE;
    if ( $is_IE ) {
        wp_enqueue_script( 'script', plugins_url.'/js/html5shiv.js' );
        wp_enqueue_script( 'script', plugins_url.'/js/css3-mediaqueries.js' );
        wp_enqueue_script( 'script', plugins_url.'/js/respond.js' );
    }
?>

or you can try this : 或者您可以尝试以下方法:

global $wp_scripts;
    wp_register_script( 
    'html5shiv', 
    plugins_url.'/js/html5shiv.js', 
     array(), 
    '1.0'
);
$wp_scripts->add_data( 'html5shiv', 'conditional', 'lt IE 9' );

This post says you can use the ! 这篇文章说你可以使用! (NOT) operator like [if !IE] (NOT)运算符,例如[if !IE]

More info here 更多信息在这里

<?php
global $is_IE;
if ( $is_IE ) {
    wp_enqueue_script( 'script', plugins_url.'/js/html5shiv.js' );
    wp_enqueue_script( 'script', plugins_url.'/js/css3-mediaqueries.js' );
    wp_enqueue_script( 'script', plugins_url.'/js/respond.js' );
}
?>

OR 要么

<!--[if lt IE 9]>
     wp_enqueue_script( 'script', plugins_url.'/js/html5shiv.js' );
        wp_enqueue_script( 'script', plugins_url.'/js/css3-mediaqueries.js' );
        wp_enqueue_script( 'script', plugins_url.'/js/respond.js' );
<![endif]-->

Browser Detection Booleans 浏览器检测布尔值

These globals store data about which browser the user is on. 这些全局变量存储有关用户使用哪个浏览器的数据。

    $is_iphone (boolean) iPhone Safari
    $is_chrome (boolean) Google Chrome
    $is_safari (boolean) Safari
    $is_NS4 (boolean) Netscape 4
    $is_opera (boolean) Opera
    $is_macIE (boolean) Mac Internet Explorer
    $is_winIE (boolean) Windows Internet Explorer
    $is_gecko (boolean) FireFox
    $is_lynx (boolean)
    $is_IE (boolean) Internet Explorer 

Script Logic is a WordPress plugin that gives you full control over all JavaScript and CSS files. Script Logic是一个WordPress插件,可让您完全控制所有JavaScript和CSS文件。 Using this plugin you can conditionally load scripts only on pages where needed. 使用此插件,您可以有条件地仅在需要的页面上加载脚本。

http://wordpress.org/plugins/script-logic/ http://wordpress.org/plugins/script-logic/

The official way since WordPress 4.2 : 自WordPress 4.2以来的官方方式:

function my_enqueue_scripts() {
    wp_enqueue_script( 'html5shiv', plugins_url('/js/html5shiv.js') );
    wp_script_add_data( 'html5shiv', 'conditional', 'lt IE 9' );
    /* repeat for other scripts */
}
add_action( 'wp_enqueue_scripts', 'my_enqueue_scripts' );

See https://developer.wordpress.org/reference/functions/wp_script_add_data/ 参见https://developer.wordpress.org/reference/functions/wp_script_add_data/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM