Support

Account

Home Forums Backend Issues (wp-admin) remove_meta_box not working for 'normal'

Solved

remove_meta_box not working for 'normal'

  • I added an some custom fields to the regular ‘post’ type. But I am trying to remove the metabox now, but it’s not working. (I made an option page to enable/disable the custom fields.) I have done some thing similar on a custom post type, and it’s working fine there.
    I noticed on difference, the metabox on the post type is added as ‘normal’, the one on my custom post type is added as ‘side’. If I change the metabox for the post type to ‘side’ it is working fine too. But I don’t want it on the side of course… Any ideas?

    Not working:

    ...
    'position' => 'normal',
    ...

    remove_meta_box('acf-group_56fe3f88b745d', 'post', 'normal');

    Working:

    ...
    'position' => 'side',
    ...

    remove_meta_box('acf-group_56fe3f88b745d', 'post', 'side');

  • Why are you trying to remove the acf fields using remove_meta_box? A better way to do this would be to add a filter when ACF gets the field group and to alter the location rules so that it’s not shown if your setting says to hide it.

    
    add_filter('acf/get_field_group', 'my_alter_field_group');
    function my_alter_field_group($group) {
      // alter location rules
      return $group;
    }
    

    To see what the location rules should be and how to alter them, output a field group to code and take a look at the location argument array.

  • I’m sorry, but I can’t seem to get the filter working…

    Why is using remove_meta_box such a bad idea anyway? My question is still why this isn’t working when the position is set to something else than ‘side’.

  • More than likely acf is actually adding the field group after you call remove_meta_box(). When are you calling the function? What hook is your function fired on?

  • Currently, like this:

    add_action('admin_menu', 'remove_menus', 99);
    function remove_menus() {
    remove_meta_box('acf-group_56fe3f88b745d', 'post', 'normal');
    }

    I also played with the priority, but that didn’t help.

  • I think that ACF adds meta boxes on the admin_head hook which happens after the admin_menu hook. If I’m right you’re trying to remove it before it’s added, try adding you’re changing your action to the admin_head hook.

  • Hm, thought so too. But same thing. Only works if the metabox is added to the sidebar.

  • Are you going to define the field groups using PHP? If that’s what you’re planning then I would add the field group conditionally based on the options setting. Or possibly creating a custom location rule based on the options https://www.advancedcustomfields.com/resources/custom-location-rules/

    I don’t know where it’s going wrong for you, but like I said in my first post, I think that attempting to remove the meta boxes using the WP function rather than using ACF filters is probably the wrong approach.

  • Yes, that was a the next solution I wanted to look at. Thanks for your help. So it might be the wrong approach but I’m stil wondering why it isn’t working.

  • I don’t know why it’s not working. I’ve done some looking at the ACF code and to be honest I’m not 100% sure when the meta boxes for the field groups get added or why there’s a difference between the normal positioning and the side positioning. It looks like they are all added during the admin_head action so it doesn’t look like there should be any reason why remove meta box works in one place but does not work in another. There’s a long chain of functions and hooks that need to be followed in ACF to get there and it’s not very easy to follow in some places. Given enough time I’m sure I could work it out, but since there are easier routes to follow to accomplish what you want to do there’s really no incentive to dig that deep. I’m also sure that the developer would be able to tell us, but he doesn’t really answer general question like this. I’ll mark this topic for his attention, but I can’t say if he’ll answer or not.

  • So I found a solution that made it work for me. I noticed that all metaboxes are added in ‘high’ priority, except those positioned on the side. Luckily there is filter in ACF to change this. So I changed the priority to ‘core’ and the remove_meta_box started working.

    add_filter('acf/input/meta_box_priority', 'custom_meta_box_priority', 10, 2);
    function custom_meta_box_priority($priority, $field_group) {
    	$priority = 'core';
    	return $priority;
    }
  • I completely missed that changing the priority would have anything to do with it. Good find.

Viewing 12 posts - 1 through 12 (of 12 total)

The topic ‘remove_meta_box not working for 'normal'’ is closed to new replies.