$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Hide admin menu on update_option|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Hide admin menu on update_option

matteradmin9PV0评论

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
}

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
}
Share Improve this question edited Jan 14, 2019 at 13:58 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Jan 14, 2019 at 13:41 wpdevwpdev 5492 gold badges13 silver badges28 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

OK, so you put your form in manu page callback and the code that is processing that form is also in that same function.

And you register your manu pages on admin_menu hook.

All of that means that when you process your form and change the value of your option, the menu is already registered. So it won't change without refreshing the site.

To solve that problem, you'll have to process your form before registering your menu.

And to do it in a way that respects best practices, you should also use admin_post_ hook to process your form. It could look something like this:

function test_sub_display() {
    ?>    
    <form method="POST" action="<?php echo esc_attr( admin_url('admin-post.php') ); ?>">
        <input type="hidden" name="action" value="my_test_sub_save" />
        <input type="text" name="test" />
        <?php submit_button(); ?>
    </form>
    <?php
}

function my_test_sub_save_callback() {
    if ( isset( $_POST[ 'test' ] ) ) {
        $value = $_POST[ 'test' ];

        if( '1' == $value ) {
            update_option( 'test_option', true );
        } else {
            update_option( 'test_option', false );
        }
    }

    wp_redirect( '<URL>' ); // change <URL> to the URL of your admin page
    exit;
}
add_action( 'admin_post_my_test_sub_save', 'my_test_sub_save_callback' );

PS. I'm not sure if it's by design, but you show the form on submenu page, but that page won't be shown if the option is set, so you won't be able to change that option, if you hide the submenu.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far