0

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
}

1 Answer 1

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.