admin管理员组文章数量:1122832
The Blank Slate theme (in its functions.php) adds a jQuery script to each page, which I would like to remove:
<?php
/* ... */
add_action( 'wp_footer', 'blankslate_footer' );
function blankslate_footer() {
?>
<script>
jQuery(document).ready(function($) {
var deviceAgent = navigator.userAgent.toLowerCase();
if (deviceAgent.match(/(iphone|ipod|ipad)/)) {
$("html").addClass("ios");
$("html").addClass("mobile");
}
if (deviceAgent.match(/(Android)/)) {
$("html").addClass("android");
$("html").addClass("mobile");
}
if (navigator.userAgent.search("MSIE") >= 0) {
$("html").addClass("ie");
}
else if (navigator.userAgent.search("Chrome") >= 0) {
$("html").addClass("chrome");
}
else if (navigator.userAgent.search("Firefox") >= 0) {
$("html").addClass("firefox");
}
else if (navigator.userAgent.search("Safari") >= 0 && navigator.userAgent.search("Chrome") < 0) {
$("html").addClass("safari");
}
else if (navigator.userAgent.search("Opera") >= 0) {
$("html").addClass("opera");
}
});
</script>
<?php
}
Using remove_action
with the same hook name, same callback name, and same priority (default of 10) is not working. To reproduce:
- Install a new Wordpress site locally.
- Install the Blank Slate theme (linked above).
- Make a child theme that includes the files below this list.
- Set the child theme as the site's theme.
- Go to any page on the site and check the HTML for the above jQuery script.
style.css:
/**
* Theme Name: Blank Slate Child
* Template: blankslate
*/
functions.php:
<?php
// Enqueue child theme
add_action( 'wp_enqueue_scripts', 'my_child_enqueue_styles' );
function my_child_enqueue_styles() {
wp_enqueue_style( 'blankslate-style', get_template_directory_uri() . '/style.css' );
}
// Remove footer script
remove_action( 'wp_footer', 'blankslate_footer' );
I also tried explicitly passing in a priority of 10, with the same result:
remove_action( 'wp_footer', 'blankslate_footer', 10 );
Any thoughts for how to fix this? Thanks so much in advance!
The Blank Slate theme (in its functions.php) adds a jQuery script to each page, which I would like to remove:
<?php
/* ... */
add_action( 'wp_footer', 'blankslate_footer' );
function blankslate_footer() {
?>
<script>
jQuery(document).ready(function($) {
var deviceAgent = navigator.userAgent.toLowerCase();
if (deviceAgent.match(/(iphone|ipod|ipad)/)) {
$("html").addClass("ios");
$("html").addClass("mobile");
}
if (deviceAgent.match(/(Android)/)) {
$("html").addClass("android");
$("html").addClass("mobile");
}
if (navigator.userAgent.search("MSIE") >= 0) {
$("html").addClass("ie");
}
else if (navigator.userAgent.search("Chrome") >= 0) {
$("html").addClass("chrome");
}
else if (navigator.userAgent.search("Firefox") >= 0) {
$("html").addClass("firefox");
}
else if (navigator.userAgent.search("Safari") >= 0 && navigator.userAgent.search("Chrome") < 0) {
$("html").addClass("safari");
}
else if (navigator.userAgent.search("Opera") >= 0) {
$("html").addClass("opera");
}
});
</script>
<?php
}
Using remove_action
with the same hook name, same callback name, and same priority (default of 10) is not working. To reproduce:
- Install a new Wordpress site locally.
- Install the Blank Slate theme (linked above).
- Make a child theme that includes the files below this list.
- Set the child theme as the site's theme.
- Go to any page on the site and check the HTML for the above jQuery script.
style.css:
/**
* Theme Name: Blank Slate Child
* Template: blankslate
*/
functions.php:
<?php
// Enqueue child theme
add_action( 'wp_enqueue_scripts', 'my_child_enqueue_styles' );
function my_child_enqueue_styles() {
wp_enqueue_style( 'blankslate-style', get_template_directory_uri() . '/style.css' );
}
// Remove footer script
remove_action( 'wp_footer', 'blankslate_footer' );
I also tried explicitly passing in a priority of 10, with the same result:
remove_action( 'wp_footer', 'blankslate_footer', 10 );
Any thoughts for how to fix this? Thanks so much in advance!
Share Improve this question asked Oct 8, 2024 at 0:29 The-Coder-Who-Knew-Too-LittleThe-Coder-Who-Knew-Too-Little 1456 bronze badges1 Answer
Reset to default 3The child theme's functions.php
is loaded before the parent theme's functions.php
:
Unlike templates and patterns, the functions.php file of a child theme does not override the functions.php file in the parent theme. In fact, they are both loaded, with the child being loaded immediately before the parent.
Source:
https://developer.wordpress.org/themes/advanced-topics/child-themes/#using-functions-php
This means that when the child theme's remove_action()
is called, there is no action to remove. To circumvent this, try this in the child theme's functions.php
(untested):
add_action( 'wp_footer', static function () {
remove_action( 'wp_footer', 'blankslate_footer', 10 );
}, 9 );
This should work, because it hooks into the same action with an earlier priority, and removes the hook (if it exists).
本文标签: phpUnable to remove action from parent theme via child theme
版权声明:本文标题:php - Unable to remove action from parent theme via child theme 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736283929a1927131.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论