wp_add_inline_script without dependency

wp_add_inline_style() – without dependency

The wp_add_inline_style() can be used without a source file dependency.

Here’s an example from @Flix:

wp_register_style( 'dummy-handle', false );
wp_enqueue_style( 'dummy-handle' );
wp_add_inline_style( 'dummy-handle', '* { color: red; }' );

where we would hook this into the wp_enqueue_scripts action.

wp_add_inline_script() – without dependency

According to ticket #43565, similar will be supported for wp_add_inline_script() in version 4.9.9 5.0 (thanks to @MarcioDuarte, @dev101 and @DaveRomsey for the verification in comments):

wp_register_script( 'dummy-handle-header', '' );
wp_enqueue_script( 'dummy-handle-header' );
wp_add_inline_script( 'dummy-handle-header', 'console.log( "header" );' );

that will display the following in the header, i.e.between the <head>...</head> tags:

<script type="text/javascript">
console.log( "header" );
</script>

To display it in the footer:

wp_register_script( 'dummy-handle-footer', '', [], '', true );
wp_enqueue_script( 'dummy-handle-footer'  );
wp_add_inline_script( 'dummy-handle-footer', 'console.log( "footer" );' );

The default of $position input argument in wp_add_inline_script() is 'after'. The 'before' value prints it above 'after'.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)