• Hi all,

    once again I wish to go back to the functions “wp_enqueue_script” and “wp_register_script” (or style).

    In several publications I read that recently is no longer needed to register a new code, and that the “enqueue” method is enough for the whole job:

    actually from https://developer.www.ads-software.com/reference/functions/wp_enqueue_script/ it seems that it is correct to add parameters (handle, src, array, etc.) directly to “wp_enqueue_ ..” function.

    But in other publications (f.e. https://gloriathemes.com/wordpress-enqueue-scripts-and-styles/) it is stated that “to use the hook “wp_enqueue_scripts” it is correct to use “enqueue” method only after “registered” the code: it could be that they are old publications (in fact I dislike when a publisher doesn’t put a date) but on the web there is too much controversial information on this issue.

    In child theme functions.php I tested both ways, and both work fine for me, but still not clear which one is the appropriate method today.

    For temporary the solutions which I have decided are as follows:

    1. To override previous parent theme scripts (replacement with same file name) I deregister > register new one > then enqueue (register with the five arguments and enqueue just with file name as handle)
    2. For my new codes valid for whole website I register them and then enqueue
    3. For my new codes for specific pages (enqueued conditionally) I directly enqueue them (with handle , path)
    4. In general I enqueue / register multiple codes per single function, keeping them separately when I need to give some priority to the “add_action(hook)”.

    I kindly ask if my above methods (from 1 to 4) are the appropriate ones according to recent standards of WP: in my opinion it is useful to clarify for everyone (thanks in advance).

    (PS: I also repeat that, even if recently applied the deregister/register method, if you change a theme script is assets/js using the same script name, the parent’s script is automatically overridden by the child’s script .. hence it results to me that using parallel paths with same structure the deregistration process is automatic)

    Mauro

    • This topic was modified 1 year, 6 months ago by Mauro Vicariotto. Reason: addition of Post Scriptum
Viewing 2 replies - 1 through 2 (of 2 total)
  • Hello Mauro!

    To override previous parent theme scripts (replacement with same file name) I deregister > register new one > then enqueue (register with the five arguments and enqueue just with file name as handle)

    This sounds like the correct approach to me. Please keep in mind that you may remove some functionality inherited from the parent theme by removing scripts it enqeues.

    For my new codes valid for whole website I register them and then enqueue

    It’s not necessary to call both functions in this case. It’s perfectly fine (and simpler) to use wp_enqueue_script and provide all of the parameters. Behind the scenes, it will register the script and enqueue it.

    For my new codes for specific pages (enqueued conditionally) I directly enqueue them (with handle , path)

    If you need to separate the process of registration and enqueuing, you can use wp_register_script to register and then call wp_enqueue_script later using only the handle parameter that was passed to wp_register_script

    For example

    function register_some_scripts() {
    	// Register the first script.
    	wp_register_script(
    		'my-script-one',
    		'{PATH_TO}/script-one.js',
    		array(),
    		'1.0'
    	);
    
    	// Register the second script.
    	wp_register_script(
    		'my-script-two',
    		'{PATH_TO}/script-two.js',
    		array(),
    		'1.0'
    	);
    }
    add_action( 'wp_enqueue_scripts', 'register_some_scripts' );

    Then call the following to enqueuing the script. This would appear in your conditional statement or template.

    wp_enqueue_script( 'my-script-one' );

    In general I enqueue / register multiple codes per single function, keeping them separately when I need to give some priority to the “add_action(hook)”.

    This sounds OK to me. If you’re using priority to control the order scripts are being loaded, you may want to consider using the deps parameter for both functions instead.

    I’ll drop the link to the documentation for these functions here for reference:

    I hope this helps!

    Thread Starter Mauro Vicariotto

    (@mrosfy)

    Thank you Ryan .. will follow

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘wp_register wp_enqueue’ is closed to new replies.