javascriptwordpressdeferred

How to add defer or async attribute to wp_add_inline_script?


First I enqueue a script using:

wp_enqueue_script( "script", plugins_url( "/test/js/script.js", PATH ), array("jquery"), VERSION, true );

Then I'm inserting an inline script after "script".

wp_add_inline_script( "script", "console.log('hello world');" );

Now I need to add defer or async attribute to my inline script, is there a way to do this to a script embedded by wp_add_inline_script() ?


Solution

  • wp_enqueue_script( "script", plugins_url( "/test/js/script.js", PATH ), array("jquery"), VERSION, true );

    wp_script_add_data( 'script', 'async/defer' , true );

    see more