Skip to content

Instantly share code, notes, and snippets.

@apatton-cnet
Last active December 3, 2022 07:36
Show Gist options
  • Save apatton-cnet/9ffae20f0cf7c644e1f8c3c97a0336fa to your computer and use it in GitHub Desktop.
Save apatton-cnet/9ffae20f0cf7c644e1f8c3c97a0336fa to your computer and use it in GitHub Desktop.
Wordpress settings page meta box problem. Works on one site, not on the other - same code, same WP Version
<?php
add_action( 'admin_menu' , 'my_plugin_register_plugin_settings', 10 );
function my_plugin_register_plugin_settings()
add_menu_page(
__( 'My Plugin Title', 'my_plugin' ),
__( 'My Plugin Menu Text', 'my_plugin' ),
'manage_options',
'my-plugin-menu',
'my_plugin_render_admin_menu_cb',
'dashicons-location',
60
);
add_submenu_page(
'my-plugin-menu',
__( 'My Plugin Subpage Title', 'my_plugin' ),
__( 'Settings', 'my_plugin' ),
'manage_options',
'my_plugin_settings_page',
'my_plugin_settings_page_cb'
);
}
//main menu page render callback
function my_plugin_render_admin_menu_cb( ) {
//not relevant, different page
}
//submenu page render callback
function my_plugin_settings_page_cb() {
do_action( 'add_meta_boxes', 'my_plugin_settings_page' );
?>
<div class="wrap">
<h2><?php _e( 'My Plugin Settings', 'my_plugin' ); ?></h2>
<form id="my_plugin_settings_form" method="post" action="admin.php?page=my_plugin_settings_page">
<?php wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false ); ?>
<?php wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false ); ?>
<?php wp_nonce_field( 'my_form_nonce', 'my_form_nonce', false ); ?>
<div id="poststuff">
<div id="post-body" class="metabox-holder columns-1">
<div id="postbox-container-1" class="postbox-container">
<?php $result = do_meta_boxes( 'my_plugin_settings_page', 'normal', null ); ?>
</div>
</div>
</div>
</form>
</div>
<?php
}
add_action( 'add_meta_boxes', 'my_plugin_add_settings_meta_boxes' );
function my_plugin_add_settings_meta_boxes() {
/** Set up Info meta box */
add_meta_box(
'my_plugin_meta_box_settings_info',
__( 'Info', 'my_plugin' ),
'my_plugin_show_meta_box_info',
'my_plugin_settings_page',
'normal',
'default'
);
/** Set up Format Settings meta box */
add_meta_box(
'my_plugin_meta_box_settings_format',
__( 'Output Formatting', 'my_plugin' ),
'my_plugin_show_meta_box_settings_format',
'my_plugin_settings_page',
'normal',
'default'
);
}
function my_plugin_show_meta_box_info() {
echo "info box";
}
function my_plugin_show_meta_box_settings_format() {
echo "format settings box";
}
add_action( 'admin_footer', 'my_plugin_settings_page_footer' );
function my_plugin_settings_page_footer(){
if ( $_GET['page'] == 'my_plugin_settings_page' ) {
?>
<script type="text/javascript">
//<![CDATA[
jQuery(document).ready( function($) {
// toggle
$('.if-js-closed').removeClass('if-js-closed').addClass('closed');
postboxes.add_postbox_toggles( '<?php echo 'my_plugin_settings_page'; ?>' );
});
//]]>
</script>
<?php
}
}
?>
<?php //second plugin file
//this is not removing the metabox like i am expecting it should
//this works on one site, but not the other - using the same code base, same wordpress version
add_action( 'add_meta_boxes', 'my_plugin_do_settings_meta_boxes', 11 );
function my_plugin_do_settings_meta_boxes( $page ) {
global $my_plugin_status;
if ( $my_plugin_status === true ) {
if ( $page == 'my_plugin_settings_page' ) {
remove_meta_box(
'my_plugin_meta_box_settings_info',
'my_plugin_settings_page',
'default'
);
}
}
} ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment