Javascript 子主题的wp_dequeue_脚本以替换脚本
我有一个子主题,我可以添加我想要用来替换一些主题函数和替换一些按钮的脚本 但我无法移除旧按钮,因此它们都显示在彼此的顶部。如何删除父js脚本 下面是我的Javascript 子主题的wp_dequeue_脚本以替换脚本,javascript,php,wordpress,parent-child,Javascript,Php,Wordpress,Parent Child,我有一个子主题,我可以添加我想要用来替换一些主题函数和替换一些按钮的脚本 但我无法移除旧按钮,因此它们都显示在彼此的顶部。如何删除父js脚本 下面是我的function.php子主题 function replace_scroll(){ // Remove the Default JavaScript wp_dequeue_script( 'dp-js' ); // Add your own script $js_url = get_bloginfo
function.php
子主题
function replace_scroll(){
// Remove the Default JavaScript
wp_dequeue_script( 'dp-js' );
// Add your own script
$js_url = get_bloginfo('stylesheet_directory') . '/js';
wp_enqueue_script('dp',"$js_url/dp1.scripts.js");
}
add_action( 'wp_enqueue_scripts', 'replace_scroll' );
您需要找出父主题在何处生成按钮 如果它们是由javascript生成的,则可以将创建按钮的脚本出列。请小心,因为该文件中的任何其他javascript也将被删除。如果这是一个问题,最好的方法是将js脚本复制到主题文件夹中,删除不需要的部分,将原始脚本出列,并将修改后的脚本放入队列 如何做到这一点。 要将脚本出列,您需要知道它的句柄。关于如何获取脚本句柄的精彩教程,非常方便。总结一下,如果链路断开: 添加到functions.php文件中
function wpcustom_inspect_scripts_and_styles() {
global $wp_scripts;
global $wp_styles;
// Runs through the queue scripts
foreach( $wp_scripts->queue as $handle ) :
$scripts_list .= $handle . ' | ';
endforeach;
// Runs through the queue styles
foreach( $wp_styles->queue as $handle ) :
$styles_list .= $handle . ' | ';
endforeach;
printf('Scripts: %1$s Styles: %2$s',
$scripts_list,
$styles_list);
}
add_action( 'wp_print_scripts', 'wpcustom_inspect_scripts_and_styles' );
这将在页面顶部打印一个列表,列出加载到页面中的所有js和css。如果您在确定什么是什么方面遇到困难,您可以始终使用dom检查器,例如在Chrome或firefox上。在chrome上-右键单击-检查元素-资源-框架-站点url-脚本
这将为您提供所有脚本及其位置的列表
一旦你找到了你需要的脚本,把它复制到你的主题中,然后删除添加按钮的部分
然后使用前面找到的句柄将不需要的脚本出列,并将新脚本入列
function wpcustom_deregister_scripts_and_styles(){
wp_deregister_script('my-script-handle');
wp_enqueue_script( 'my-new-script', get_template_directory_uri() . '/js/my-new-script.js', array(), '1.0', true );
}
add_action( 'wp_print_styles', 'wpcustom_deregister_scripts_and_styles', 100 );
如果按钮是由php生成的,则需要在父主题中找到生成html的文件,将其复制到子主题中,并删除有问题的行 使用父主题进行测试,其functions.php
文件中包含以下内容:
function twentyfourteen_scripts() {
// other enqueues
wp_enqueue_script( 'twentyfourteen-script', get_template_directory_uri() . '/js/functions.js', array( 'jquery' ), '20131209', true );
}
add_action( 'wp_enqueue_scripts', 'twentyfourteen_scripts' );
function remove_twentyfourteen_scripts()
{
wp_dequeue_script( 'twentyfourteen-script' );
}
add_action( 'wp_enqueue_scripts', 'remove_twentyfourteen_scripts', 11 ); // <-- HERE
操作wp\u enqueue\u脚本
没有声明的优先级,因此它使用默认的10
。
为了使出列工作正常,我们必须在子主题functions.php
文件中添加一个较低的优先级:
function twentyfourteen_scripts() {
// other enqueues
wp_enqueue_script( 'twentyfourteen-script', get_template_directory_uri() . '/js/functions.js', array( 'jquery' ), '20131209', true );
}
add_action( 'wp_enqueue_scripts', 'twentyfourteen_scripts' );
function remove_twentyfourteen_scripts()
{
wp_dequeue_script( 'twentyfourteen-script' );
}
add_action( 'wp_enqueue_scripts', 'remove_twentyfourteen_scripts', 11 ); // <-- HERE
function remove_twenty144; u scripts()
{
wp_dequeue_脚本(‘2144脚本’);
}
添加动作('wp_enqueue_scripts','remove_TwentyTeven_scripts',11);// 我想补充一点,我们需要在添加替换脚本时使用get\u stylesheet\u directory\u uri()
,因为get\u template\u directory\u uri()
返回父主题目录
请参阅WordPress文档这里的一些问题
- 对于子主题,您应该使用
get\u stylesheet\u directory\u uri()
,对于父主题,您应该使用get\u template\u directory\u uri()
而不是get\u bloginfo()
函数。后者速度较慢,使用前两个函数
- 脚本和样式应取消注册和,并退出队列,以将它们完全从队列中删除
- 优先权很重要。您需要稍后钩住您的函数,以确保在取消注册样式和脚本之前已注册它们,否则它将无法工作
解决方案:
将父js文件复制到您的子主题并打开它,然后进行必要的更改。保存文件
现在,您需要将父js文件出列并取消注册,然后将新的子主题js文件入列
范例
/*
* Use any number above 10 for priority as the default is 10
* any number after 10 will load after
*/
add_action( 'wp_enqueue_scripts', 'my_custom_scripts', 100 );
function my_custom_scripts()
{
wp_dequeue_script( 'parent-script-handle' );
wp_deregister_script( 'parent-script-handle' );
// Now the parent script is completely removed
/*
* Now enqueue you child js file, no need to register if you are not
* doing conditional loading
*/
wp_enqueue_script( 'child-script-handle', get_stylesheet_directory_uri() . '/child-script.js' );
//Now we have done it correctly
}
在典型的wordpress流中,插件加载在主题之前,如果是子主题,子主题加载在父主题之前,具体来说,子主题的functions.php
文件包含在父主题的functions.php之前。因此,wp\u enqueue\u脚本
动作被堆叠起来,随后按该顺序执行,除非动作的优先级被不同地定义为默认值(10)。这里的问题是,您正在尝试将尚未排队的脚本出列
要删除父级添加的脚本,请将子主题的wp\u enqueue\u scripts
操作的优先级提高到高于父级主题操作中定义的值
// get_template_directory_uri(), if used in child theme, the parent theme directory URI will be returned,
// use get_stylesheet_directory_uri() to get the child's theme directory uri
add_action( 'wp_enqueue_scripts', 'child_theme_enqueue_scripts', 11 );
function child_theme_enqueue_scripts()
{
wp_dequeue_script( 'parent-theme-script' );
wp_enqueue_script( 'child-theme-script', get_stylesheet_directory_uri() . '/js/script.js' );
}
例如,要替换脚本,您需要进行一些更改并保留现有的依赖项和本地化,有两种方法
使用具有相同句柄
名称的wp\u enqueue\u脚本
,因为如果您尝试注册或将已注册的句柄排入队列,新参数将被忽略,如果脚本稍后由父级本地化以保留本地化数据,则新参数非常有用
// default priority 10
add_action( 'wp_enqueue_scripts', 'child_theme_enqueue_scripts' );
function child_theme_enqueue_scripts()
{
wp_enqueue_script( 'parent-theme-script', get_stylesheet_directory_uri() . '/js/script.js' );
}
使用全局$wp_scripts
对象仅修改所需的属性,例如,仅更改已排队或已注册脚本的src
属性
// priority 11
add_action( 'wp_enqueue_scripts', 'child_theme_enqueue_scripts', 11 );
function child_theme_enqueue_scripts()
{
global $wp_scripts;
$wp_scripts->registered[ 'parent-theme-script' ]->src = get_stylesheet_directory_uri() . '/js/script.js';
}
首先加载子主题这一事实是非常实际的,因为子主题的使用不是为了替换,而是作为主主题的补充。感谢提供更多信息。虽然使用PHP_INT_MAX看起来很不错,但可能是的,但我喜欢在子主题中使用它,以确保我的函数最后被钩住。这纯粹是你自己的喜好。让它发挥作用的关键是确保你与父母主题中的add\u action
之后使用的内容相匹配。有时使用wp\u-enqueue\u脚本
,有时使用wp\u-footer
,具体取决于文件和主题。@fidev使用wp\u-footer()
添加脚本(这也适用于wp\u-head()
)是糟糕的编码。您应该始终使用wp\u排队\u脚本
wp\u enqueue\u script()
确实可以在footer@PieterGoosen非常正确,但并非所有主题都符合这一点-这就是为什么您必须检查父主题中使用的内容,并相应地进行匹配,以便正确地出列/注销。不必要地编辑父主题文件