2

I want to change the return URL after someone cancels or closes out of their changes when using the Apperance -> Customize option within Wordpress. In the wp-admin/customize.php file here is the code to control the return URL:

wp_reset_vars( array( 'url', 'return' ) );
$url = wp_unslash( $url );
$url = wp_validate_redirect( $url, home_url( '/' ) );
if ( $return ) {
    $return = wp_unslash( $return );
    $return = wp_validate_redirect( $return );
}
if ( ! $return ) {
    if ( $url ) {
        $return = $url;
    } elseif ( current_user_can( 'edit_theme_options' ) || current_user_can( 'switch_themes' ) ) {
        $return = admin_url( 'themes.php' );
    } else {
        $return = admin_url();
    }
}

The URL is output to the screen with the following code:

<a class="customize-controls-close" href="<?php echo esc_url( $return ); ?>">
    <span class="screen-reader-text"><?php _e( 'Cancel' ); ?></span>
</a> 

Is there a way to control the return URL wihtout modifying the core wp-admin/customize.php file?

2
  • It would depend on what you want it to changed to. But I think the best would be to pass the url you want if user cancels or closes their changes. That would mean that you do your changes if users cancels theirs. Feb 16, 2015 at 1:42
  • @Reigel How would I pass that without modifying the core files? Feb 16, 2015 at 1:45

1 Answer 1

1

Yes, this is possible. On the link, there you create for the customizing view can you add a return parameter with the url, for this option.

I mean, you must change the the link for the Customize Link in the Admin Menu. The follow demonstrate this. I create at fist a link inside the "Appearance" Menu and parse this slug to change the url to custom url, include two parameters.

  1. url - defined for load a specific page in the customizer
  2. return - defined the url for close the customizer, the return url

The important part is the function add_query_arg() to add this parameters to the url.

$login_url = wp_login_url();
$url       = add_query_arg(
    array(
        'url'    => urlencode( $login_url ),
        'return' => admin_url( 'themes.php' ),
    ),
    admin_url( 'customize.php' )
);

The follow source and doing the completely job, include add menu item and change his link. You must load, include this in your theme, plugin.

    <?php # -*- coding: utf-8 -*-

    namespace CustomizeLogin\Admin;

    \add_action( 'admin_menu', '\CustomizeLogin\Admin\add_menu' );
    function add_menu() {

        if ( ! current_user_can( 'customize' ) ) {
            return NULL;
        }

        add_theme_page(
            esc_attr__( 'Customize the Login screen', 'customize-login' ),
            esc_attr__( 'Customize Login', 'customize-login' ),
            'manage_options',
            'customize-login',
            '__return_null()'
        );
    }

    \add_action( 'admin_menu', '\CustomizeLogin\Admin\change_menu_url', 99 );
    function change_menu_url() {

        global $submenu;

        $parent = 'themes.php';
        $page   = 'customize-login';

        // Create specific url for login view
        $login_url = wp_login_url();
        $url       = add_query_arg(
            array(
                'url'    => urlencode( $login_url ),
                'return' => admin_url( 'themes.php' ),
            ),
            admin_url( 'customize.php' )
        );

        // If is Not Design Menu, return
        if ( ! isset( $submenu[ $parent ] ) ) {
            return NULL;
        }

        foreach ( $submenu[ $parent ] as $key => $value ) {
            // Set new URL for menu item
            if ( $page === $value[ 2 ] ) {
                $submenu[ $parent ][ $key ][ 2 ] = $url;
                break;
            }
        }
    }
4
  • I want to put this in my child theme. How can I add this to my functions.php file? Jul 22, 2015 at 18:31
  • No the functions.php, the template file, where you will see the login link, like header.php
    – bueltge
    Jul 22, 2015 at 18:38
  • Won't that get overwritten if I do theme update or a wordpress update then? Jul 22, 2015 at 21:50
  • Then use a child theme or the hook, there I describe on the answer.
    – bueltge
    Jul 23, 2015 at 6:15

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.