WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#37425 closed defect (bug) (worksforme)

Passing variables withing the previewURL get lost on server side

Reported by: virgodesign Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.5.3
Component: Customize Keywords:
Focuses: Cc:

Description

I'm experiencing the following problem and I don't know exactly if it's a bug or a normal behavior.
I'm trying to pass a custom variable within the previewUrl using the wp.customize javascript object as following:

wp.customize.previewer.previewUrl( 'http://www.example.com/?custom_var=1' );

and then call the refresh method to launch the ajax request

wp.customize.previewer.refresh();

To customize the preview that will return back to the customizer we need to perform a check as following:

function load_custom_template( $template ) {
    
    	if( is_customize_preview() && isset($_GET['custom_var']) ) {
    		
    			$new_template = locate_template( array( 'custom_template.php' ) );
    			if ( '' != $new_template ) {
    				return $new_template ;
    			}
    		
    		}
    	}
    	
    	return $template;
    
    }
    add_filter( 'template_include', 'load_custom_template', 99 );

But when we try to retrieve the $_GET variable, this is missing.
The REQUEST_URI seems to have no get variables.

Change History (6)

#1 @westonruter
4 years ago

@virgodesign calling wp.customize.previewer.refresh() should be unnecessary because merely changing the previewUrl should trigger a refresh with the newly-set URL.

#2 @virgodesign
4 years ago

@westonruter you are right. Without forcing refresh, variables will be sended. But everthing should works as expected even with the refresh method triggered. Anyway thanks for the suggestion. If this is not a defect/bug we can close the ticket.

Last edited 4 years ago by virgodesign (previous) (diff)

#3 @westonruter
4 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to worksforme
  • Status changed from new to closed

@virgodesign I can't reproduce the issue. I added this bit to my theme's header.php:

<?php echo esc_html( wp_json_encode( wp_unslash( $_GET ) ) ) ?>

And then I was able to enter this into the browser console:

var urlParser = document.createElement( 'a' );
urlParser.href = wp.customize.settings.url.home
urlParser.search = 'custom_var=1';
wp.customize.previewer.previewUrl( urlParser.href );

And the header showed the expected value:

{"custom_var":"1"}

Your issue may be that you tried to enter the wrong domain when setting the URL.

#4 @virgodesign
4 years ago

Could you try adding the refresh method, as following:

wp.customize.previewer.previewUrl( url );
wp.customize.previewer.refresh();

Without refresh there is no issue.
I can exclude that url entered is wrong.

#5 @westonruter
4 years ago

@virgodesign I tried with both, and there was no difference. Both worked as expected.

#6 @virgodesign
4 years ago

@westonruter, I'm not able to reproduce the error anymore. I'm supposing that could be a problem related to the expired session or an invalid nonce. Sometimes only after I refresh properly an admin page the system will prompt me with the message "Are you sure you want to do this?" or a login screen.

Note: See TracTickets for help on using tickets.