最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

remove_action from parent theme using child theme functions.php

programmeradmin1浏览0评论

I am trying to remove an action from the parent theme header but I am still not getting any success, here is the class

<?php

class theme_Header_Layout{
    public function __construct() {

        add_action('theme_header_layout_1_branding', array( $this, 'get_site_branding' ), 10 );

        add_action('theme_header_layout_1_navigation', array( $this, 'get_site_navigation' ), 10 );

        add_action('theme_site_header_icon', array( $this, 'get_site_header_icon' ), 10 );

        add_action('theme_site_header', array( $this, 'site_skip_to_content' ), 5 );

         *
         *
         *
        add_action('theme_site_header', array( $this, 'site_hero_sections' ), 9999 );
    }
    *
    *
    *
    public function site_hero_sections(){...}
    function hero_block_heading() {...}
    private function alowed_tags(){...}
}

$theme_header_layout = new theme_Header_Layout();

And this is what I have on my theme-child functions.php

<?php

    add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' );
    function theme_enqueue_styles() {
        wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
    }

    add_action( 'wp_head', 'remove_site_hero_sections');
    function remove_site_hero_sections() {
        global $theme_header_layout;
        remove_action('theme_site_header', array($theme_header_layout, 'site_hero_sections'));
    }

I've been trying changing the priority inside add_action or remove_action, I've also tried changing the add_action('wp_head'...) to 'theme_site_header' but I am still getting the site_hero_sections on my child theme, what am I doing wrong?

I am trying to remove an action from the parent theme header but I am still not getting any success, here is the class

<?php

class theme_Header_Layout{
    public function __construct() {

        add_action('theme_header_layout_1_branding', array( $this, 'get_site_branding' ), 10 );

        add_action('theme_header_layout_1_navigation', array( $this, 'get_site_navigation' ), 10 );

        add_action('theme_site_header_icon', array( $this, 'get_site_header_icon' ), 10 );

        add_action('theme_site_header', array( $this, 'site_skip_to_content' ), 5 );

         *
         *
         *
        add_action('theme_site_header', array( $this, 'site_hero_sections' ), 9999 );
    }
    *
    *
    *
    public function site_hero_sections(){...}
    function hero_block_heading() {...}
    private function alowed_tags(){...}
}

$theme_header_layout = new theme_Header_Layout();

And this is what I have on my theme-child functions.php

<?php

    add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' );
    function theme_enqueue_styles() {
        wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
    }

    add_action( 'wp_head', 'remove_site_hero_sections');
    function remove_site_hero_sections() {
        global $theme_header_layout;
        remove_action('theme_site_header', array($theme_header_layout, 'site_hero_sections'));
    }

I've been trying changing the priority inside add_action or remove_action, I've also tried changing the add_action('wp_head'...) to 'theme_site_header' but I am still getting the site_hero_sections on my child theme, what am I doing wrong?

Share Improve this question edited Feb 17, 2021 at 3:00 AdrobaT asked Feb 16, 2021 at 19:35 AdrobaTAdrobaT 12 bronze badges 2
  • 1 "I've been trying changing the priority inside" - so with the remove_action(), did you try with 9999? Because when removing an action, the priority needs to match the one used when adding the action. – Sally CJ Commented Feb 17, 2021 at 2:09
  • 1 Oh wow that's it I guess I probably missed or added an additional "9" but I just copied and pasted the exact same number and now it is working lol, sorry for bothering you, it is now working as expected, thanks! – AdrobaT Commented Feb 17, 2021 at 3:12
Add a comment  | 

1 Answer 1

Reset to default 0

Well, all I needed to fix the issue was to add the exact same priority to the remove_action and it worked pretty well, thanks to Sally CJ answer

add_action( 'wp_head', 'remove_site_hero_sections');
    function remove_site_hero_sections() {
        global $theme_header_layout;
        remove_action('theme_site_header', array($theme_header_layout, 'site_hero_sections'), 9999);
    }
发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>