Remove the Astra Settings Meta Box

Removing the Astra Settings Meta Box from your custom post type

I’ve been developing a WordPress child theme based on Astra for a client recently. This child theme comes with a custom post type, and I ran into a bit of trouble trying to remove the Astra Settings Meta Box (which Astra tags onto every single post type) from my custom post type.

If you’re looking to do the same thing for your custom post type on Astra, you’ll need to hook the remove_astra_settings_meta_box() function below to the do_meta_boxes action hook.

In other words — for those of you who are newer to WordPress scripting — you’ll need to add the following to your plugin file or your theme’s functions.php.

add_action('do_meta_boxes','remove_astra_settings_meta_box');
function remove_astra_settings_meta_box() {
	remove_meta_box( 'astra_settings_meta_box', YOUR_POST_TYPE_SLUG, 'side' );
}

Note that you’ll need to replace YOUR_POST_TYPE_SLUG above with your custom post type’s slug.

Using Gutenberg?

If your custom post type uses Gutenberg, the hook above won’t work for you. You’ll also need to hook the following functions below:

// Remove the scripts that create the Astra Settings box on Gutenberg first.
add_action('after_setup_theme', 'remove_astra_settings_gutenberg');
function remove_astra_settings_gutenberg() {
	$ast_mb = \Astra_Meta_Boxes::get_instance();
	remove_action( 'init', array( $ast_mb, 'register_script' ) );
	remove_action( 'enqueue_block_editor_assets', array( $ast_mb, 'load_scripts' ) );
}

// Re-add them if we are on the correct post type(s).
add_action('admin_enqueue_scripts', 'readd_astra_setings_gutenberg');
function readd_astra_setings_gutenberg() {
	if(get_post_type() !== YOUR_POST_TYPE_SLUG) {
		$ast_mb = \Astra_Meta_Boxes::get_instance();
		$ast_mb->register_script();
		$ast_mb->load_scripts();
	}
}

Remember that you have to replace YOUR_POST_TYPE_SLUG above too.

What we are doing here is unhooking Astra’s Javascript file hooks the Astra Settings window onto Gutenberg, and hooking it back for all post types except for our own custom post type.

It’s not the most elegant solution, since it’s very hacky. If you’ve got any better ideas, do share them with me in the comments below!

There are 2 comments:

    1. Hi A, thank you for sharing the link. Your error might have been caused because you did not replace YOUR_POST_TYPE_SLUG with your own post type slug.

Leave a Reply

Your email address will not be published. Required fields are marked *

Note: You can use Markdown to format your comments.

For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.

I agree to these terms.

This site uses Akismet to reduce spam. Learn how your comment data is processed.