【问题标题】:How to handle with Wordpress scripts actions mess?如何处理 Wordpress 脚本操作混乱?
【发布时间】:2012-11-17 14:37:44
【问题描述】:

在开发 Wordpress + BuddyPress 子主题期间,我遇到了脚本问题。 我想将它们移动到 BODY 页脚(Modernizr 除外),但一些 BuddyPress 脚本留在顶部。我做到了:

// remove unused head entries
remove_action('wp_head',        'rsd_link');
//...
remove_action('wp_head',        'adjacent_posts_rel_link');

// move sctipts to footer
remove_action('wp_head',        'wp_print_scripts');
remove_action('wp_head',        'wp_print_head_scripts');
remove_action('wp_head',        'wp_enqueue_scripts');
remove_action('wp_head',        'bp_core_confirmation_js');
remove_action('wp_head',        'bp_core_add_ajax_url_js');

add_action('wp_head',           'wp_head_scripts',      9);
add_action('wp_footer',         'wp_enqueue_scripts',           20);
add_action('wp_footer',         'bp_core_confirmation_js',      21);
add_action('wp_footer',         'bp_core_add_ajax_url_js',      22);

我对动作顺序感到困惑,它看起来并不漂亮。

是否可以让它更漂亮(更自动化)?我在考虑这样的数组:

$head_scripts = array(
    'modernizr'         => 'libs/modernizr-2.6.1.js',
);
$footer_scripts = array(
    'jquery'            => 'libs/jquery-1.8.2.js',
    'jquery.mansonry'   => 'libs/jquery.masonry.min.js',
    'fb_api'            => 'libs/fb_api.js',
    'scripts'           => 'script.js'
);

还有一个函数(动作),它首先在头部添加,另一个在页脚和下一个 admin_bar 中,如有必要,还可以添加 buddypress 脚本。

【问题讨论】:

    标签: php wordpress wordpress-theming


    【解决方案1】:

    为什么不跟踪所有这些脚本wp_enqueue_script 文件并将in_footer 参数更改为true。您可能知道 wp_enqueue_script 方法具有最后一个参数,您可以指示该脚本是否将放置在页脚中。默认为 false 将被放入标题中

     wp_enqueue_script( 
            $handle
           ,$src
           ,$deps
           ,$ver
           ,$in_footer  // make this TRUE to put the script in the footer
    );
    

    更多信息请访问文档http://codex.wordpress.org/Function_Reference/wp_enqueue_script

    【讨论】:

    • 好的。我知道,但 BuddyPress 和其他插件 sripts 留在脑海中。如何在一个地方控制所有这些脚本。 F.e.用于在生产版本中将其连接/压缩为一个。我是 WP 钩子的新手。
    猜你喜欢
    • 2011-07-19
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 2016-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多