Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove custom meta boxes not working

what I was trying to do here is to remove some custom fields that I created when a template is selected, aka when I select certain template I want to hide or show specific metaboxes.

The code I have is the following but it isn't working at all (thats to say that it doesn't remove any metaboxes) and I would like help to see what's wrong with it or if what I'm trying to do it's just not posible.

    add_action('admin_init','my_meta_init');
    function my_meta_init(){
    $template_file = get_post_meta(get_the_ID(), '_wp_page_template', TRUE);

if (($template_file == 'thanks-template.php') || ($template_file == 'view-template.php')) 
{
    remove_meta_box('my_meta_box_id','page','normal'); 
    remove_meta_box('my_meta_box_id_2','page','side'); 
    remove_meta_box('my_meta_box_id_3','page','side');
    remove_meta_box('dynamic_sectionid','page','normal');     
} else
{
    remove_meta_box('my_meta_box_id_4','page','normal'); 
}
    }
like image 410
Miriam Villalon Avatar asked Jun 02 '26 13:06

Miriam Villalon


1 Answers

Thanks you for the comments and answer, everyone helped. The problem was on the hook I was using, I changed it and now it's working perfectly :

  add_action('admin_head','my_meta_init');
like image 66
Miriam Villalon Avatar answered Jun 04 '26 09:06

Miriam Villalon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!