$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'); ?>array - How should I identify the inline javascript that is the dependent of a wp_enqueue_script?|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)

array - How should I identify the inline javascript that is the dependent of a wp_enqueue_script?

matteradmin8PV0评论

I want to run my own JavaScript on a specific WordPress page but only after a third party's plugin runs as I am trying to slightly modify that plugin. My problem is that my JavaScript is running before, not after, the plugin's script. Is my mistake in how I am referring to the plugin's script (i.e. the dependent script) in the $deps array?

<?php  
// What was originally in the child theme’s function.php file  
add_action( 'wp_enqueue_scripts', 'you_enqueue_styles' );

function you_enqueue_styles() 
{
    wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
}

// Added by me to the child theme’s function.php file
function load_new_js() {
    if( is_page( 692 ) ) {
        wp_enqueue_script('modify_plugin_js', '.js', array('greetingnow', 'jquery'), '',false);
    }
}
// Should I even bother putting in a priority of 100 to try and force this to be run last?
add_action('wp_enqueue_scripts', 'load_new_js', 100);

This is what the script in the HTML looks like:

<html>
<body>

<script>
<!-- This is the dependent code that should run first and is put directly into the HTML file by the third party Plugin provider -->
var greetingnow = "Good Morning!";
alert(greetingnow);
</script>

</body>
</html>

I want to run my own JavaScript on a specific WordPress page but only after a third party's plugin runs as I am trying to slightly modify that plugin. My problem is that my JavaScript is running before, not after, the plugin's script. Is my mistake in how I am referring to the plugin's script (i.e. the dependent script) in the $deps array?

<?php  
// What was originally in the child theme’s function.php file  
add_action( 'wp_enqueue_scripts', 'you_enqueue_styles' );

function you_enqueue_styles() 
{
    wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
}

// Added by me to the child theme’s function.php file
function load_new_js() {
    if( is_page( 692 ) ) {
        wp_enqueue_script('modify_plugin_js', 'https://www.example/mycustomscript.js', array('greetingnow', 'jquery'), '',false);
    }
}
// Should I even bother putting in a priority of 100 to try and force this to be run last?
add_action('wp_enqueue_scripts', 'load_new_js', 100);

This is what the script in the HTML looks like:

<html>
<body>

<script>
<!-- This is the dependent code that should run first and is put directly into the HTML file by the third party Plugin provider -->
var greetingnow = "Good Morning!";
alert(greetingnow);
</script>

</body>
</html>
Share Improve this question edited Nov 27, 2018 at 22:17 Anton Lukin 8875 silver badges17 bronze badges asked Nov 27, 2018 at 19:30 computerusernov2018computerusernov2018 31 bronze badge 5
  • It's okay to add dependent script in your wp_enqueue_script function. – Anton Lukin Commented Nov 27, 2018 at 19:49
  • Thanks for the reply @AntonLukin, I know it is okay but my problem is the dependencies are not working as I expected. – computerusernov2018 Commented Nov 27, 2018 at 20:12
  • I want greetingnow to run first and then mycustomscript.js. – computerusernov2018 Commented Nov 27, 2018 at 20:18
  • Where is greetingnow enqueued? If in another plugin, how is the plugin enqueing the script? – czerspalace Commented Nov 27, 2018 at 20:47
  • greetingnow is not enqueued anywhere. I thought I did not have to enqueue it since it is already a script in the HTML page. Do I still have to include it in the enqueue function? – computerusernov2018 Commented Nov 27, 2018 at 21:47
Add a comment  | 

2 Answers 2

Reset to default -1

Try this...

function load_new_js() {
    if( is_page( 692 ) ) {
        wp_enqueue_script(
            'modify_plugin_js',
            get_stylesheet_directory_uri() . '/mycustomscript.js',
            array('jquery','greetingnow')
        );

    }
}

If that doesn't work, what plugin are you using?

Use wp_add_inline_script(). It lets you add and inline script that's dependent on an enqueued script:

function wpse_320377_register_scripts() {
    wp_register_script( 'my-script-handle', 'https://www.example/mycustomscript.js', [ 'jquery' ], '', true );
    wp_add_inline_script( 'my-script-handle', 'var greetingnow = "Good Morning!"; alert(greetingnow);', 'before' );

    if ( is_page( 692 ) ) {
        wp_enqueue_script( 'my-script-handle' );
    }
}
add_action( 'wp_enqueue_scripts', 'wpse_320377_register_scripts' );

In that example, it registers a custom script, my-script-handle, and then attaches the inline script to it using wp_add_inline_script() so that whenever my-script-handle is enqueued, the inline script will be output right before it. The result will look something like this:

<script>
    var greetingnow = "Good Morning!"; alert(greetingnow);
</script>
<script type="text/javascript" src="https://www.example/mycustomscript.js"></script>
Post a comment

comment list (0)

  1. No comments so far