• Resolved David Gewirtz

    (@dgewirtz)


    I’m using the validation hook to process a button input that sets a global to determine if the current page tab is shown or hidden. As you might imagine, once the tab is hidden (programmatically not created based on the global) and the browser attempts to return to that page, a “you don’t have permission for this page” error is displayed.

    I tried putting a wp_redirect inside the validation function and I also tried doing it in the logic that determines whether or not to display the tab. In both cases, the redirect doesn’t happen and I get the “don’t have permission” error.

    Is there a way, as a result of a validation or inside a validation hook, to redirect the user to a different URL? Thanks!

    –David

    P.S. The purpose of this is to allow users to hide tabs they don’t need anymore. It’s a legacy migration and some tabs are just no longer needed once they’ve completed the migration.

    https://www.ads-software.com/plugins/admin-page-framework/

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author miunosoft

    (@miunosoft)

    What about just hide the tab, rather than redirecting the user?

    You can hide the tab visibility by setting the show_in_page_tab argument in the in-page tab definition array. Set false to it.

    array(
        'tab_slug'     => 'my_tab_slug',
        'title'        => __( 'My Tab', 'my-program' ),
        'show_in_page_tab'  => false, // <-- this one
    )
    Thread Starter David Gewirtz

    (@dgewirtz)

    Makes sense, but the user is on the tab page when hiding the tab. Where does the user go once the tab is hidden? Unless sent somewhere else, the user gets an error. Right?

    Plugin Author miunosoft

    (@miunosoft)

    You should be able to use the WordPress core function wp_redirect() in the validation callback method. You said you get a permission error page. I would double-check if the redirecting url is correct.

    Another option is use the load_{page slug}_{tab slug} callback method. You may do something like this.

    public function load_mypage_mytab( $oAdminPage ) {
    
        $_sRedirectingURL    = 'set your url';
        $_bUserSubmittedForm = $this->_getOptionThatTellsWhetherUserSubmittedForm();
        if ( $_bUserSubmittedForm ) {
            wp_safe_redirect( $_sRedirectingURL );
        }
    
    }
        private function _getOptionThatTellsWhetherUserSubmittedForm() {
           // return whether the user has submitted the form in the previous page
           // maybe set an option value in the validation callback method and retrieve it here.
           return true;
        }

    Thread Starter David Gewirtz

    (@dgewirtz)

    Apparently the trick is to put an exit(); after the wp_redirect. Not intuitively obvious, but found by RTFM.

    Plugin Author miunosoft

    (@miunosoft)

    Glad to hear you sorted it!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Redirect from within a validation?’ is closed to new replies.