Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[WordPress] Redirect WordPress Admin to a Custom Admin Page
<?php
/**
* Redirect to the page from which we came (which should always be the
* admin page. If the referred isn't set, then we redirect the user to
* the login page).
*
* @access private
* @param bool $success Optional If success is false, then the information can't be saved.
*/
private function redirect( $success = true ) {
// To make the Coding Standards happy, we have to initialize this.
if ( ! isset( $_POST['_wp_http_referer'] ) ) { // Input var okay.
$_POST['_wp_http_referer'] = wp_login_url();
}
// Sanitize the value of the $_POST collection for the Coding Standards.
$url = sanitize_text_field(
wp_unslash( $_POST['_wp_http_referer'] ) // Input var okay.
);
$url = ( ! $success ) ?
esc_url( remove_query_arg( 'settings-saved', $url ) ) :
add_query_arg( 'settings-saved', 'true', $url );
wp_safe_redirect( urldecode( $url ) );
exit;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment