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:

  1. Install a new Wordpress site locally.
  2. Install the Blank Slate theme (linked above).
  3. Make a child theme that includes the files below this list.
  4. Set the child theme as the site's theme.
  5. 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:

  1. Install a new Wordpress site locally.
  2. Install the Blank Slate theme (linked above).
  3. Make a child theme that includes the files below this list.
  4. Set the child theme as the site's theme.
  5. 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 badges
Add a comment  | 

1 Answer 1

Reset to default 3

The 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