Hide admin menu on update_option

I want to show custom sub menu page, if get_option is true else just hide sub page.

My function must do, if input value posted as 1, show sub page else hide sub page. But problem is, when i post 1 value sub menu doesn’t hide. I must refresh page for hiding it.

I make custom parent and sub page with this.

function create_test_parent() {
$page_title = __( ‘Test Parent Menu’, ‘test’ );
$menu_title = __( ‘Test Parent Page’, ‘test’ );
$capability = ‘manage_options’;
$menu_slug = ‘test-parent’;
$function = ‘test_parent_display’;
$icon_url = plugin_dir_url( dirname( __FILE__ ) ) . ‘menu_icon.png’;
$position = 2;

add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position );
}

add_action( ‘admin_menu’, ‘create_test_parent’ );

function create_test_sub() {
if( !get_option( ‘test_option’, true ) )
return;

$parent_slug = ‘test-parent’;
$page_title = __( ‘Test Sub Menu’, ‘test’ );
$menu_title = __( ‘Test Sub Menu’, ‘test’ );
$capability = ‘manage_options’;
$menu_slug = ‘test-menu’;
$function = ‘test_sub_display’;

add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function );
}

add_action( ‘admin_menu’, ‘create_test_sub’ );

Here is admin display function

function test_sub_display() {
if( isset( $_POST[ ‘test’ ] ) ) {
$value = $_POST[ ‘test’ ];

if( $value == ‘1’ ) {
update_option( ‘test_option’, true );
} else {
update_option( ‘test_option’, false );
}
}
?>

<form method=”POST”>
<input type=”text” name=”test”>
<?php submit_button(); ?>
</form>
<?php
}

Read more here:: Hide admin menu on update_option

Leave a Reply

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