$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - Remove specific javascript when viewing page in Customizer?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Remove specific javascript when viewing page in Customizer?

matteradmin6PV0评论

I have a javascript which reloads the page when content change is detected. This is the behaviour I want on the main site. But when I use the Wordpress Customizer I don't want this behaviour as it makes it very hard to work and Customize the page (the page tries to reload).

I tried deregistering the script and create a copy of the script without the reload function. This is how I did it:

add_action( 'wp_enqueue_scripts', 'customize_enqueue_appc_js' );
function customize_enqueue_appc_js() {
   if ( is_customize_preview() ) {
        wp_deregister_script( 'app-js' );
        wp_enqueue_script( 'appc-js', get_template_directory_uri() . '/javascripts/appc.js', array('jquery'), false, true );
   }
}

My idea here is to detect if you are on customizer with if ( is_customize_preview() ) { and if true, deregister the javascript called app-js, and enqueue the script appc-js which hasn't got the reload function.

But this doesn't seem to work. Is it because the Customizer views the page without modifications? Are you able to do something like this? Modifying the page functions when viewed in Customizer?

Is there any other approach to this? Like doing this in jQuery?

I have a javascript which reloads the page when content change is detected. This is the behaviour I want on the main site. But when I use the Wordpress Customizer I don't want this behaviour as it makes it very hard to work and Customize the page (the page tries to reload).

I tried deregistering the script and create a copy of the script without the reload function. This is how I did it:

add_action( 'wp_enqueue_scripts', 'customize_enqueue_appc_js' );
function customize_enqueue_appc_js() {
   if ( is_customize_preview() ) {
        wp_deregister_script( 'app-js' );
        wp_enqueue_script( 'appc-js', get_template_directory_uri() . '/javascripts/appc.js', array('jquery'), false, true );
   }
}

My idea here is to detect if you are on customizer with if ( is_customize_preview() ) { and if true, deregister the javascript called app-js, and enqueue the script appc-js which hasn't got the reload function.

But this doesn't seem to work. Is it because the Customizer views the page without modifications? Are you able to do something like this? Modifying the page functions when viewed in Customizer?

Is there any other approach to this? Like doing this in jQuery?

Share Improve this question asked Nov 14, 2018 at 9:53 joq3joq3 3813 silver badges21 bronze badges 2
  • 1 What you've done should work. How is the original script enqueued? Make sure your hook that deregisters the script runs after it's been registered by running it at a later priority. – Jacob Peattie Commented Nov 14, 2018 at 9:55
  • The priority might be the problem! I just changed it to 999 and it seems to work. Will be doing some testing! Thank you @JacobPeattie – joq3 Commented Nov 14, 2018 at 9:58
Add a comment  | 

1 Answer 1

Reset to default 0

add_action have 4 parameters, the third one being priority ($priority). Usually plugins and child themes load their scripts after a parent theme to ensure that it doesn't get overrun later on. So it is wise and good practice to run your action dead last. To to this, you will need a very low priority, ie very high number.

add_action( 'wp_enqueue_scripts', 'jquery_loadup', 999 );

Post a comment

comment list (0)

  1. No comments so far