Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Created October 25, 2016 15:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tommcfarlin/8b6d790e651757f54f7d62ac027846d0 to your computer and use it in GitHub Desktop.
Save tommcfarlin/8b6d790e651757f54f7d62ac027846d0 to your computer and use it in GitHub Desktop.
[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