Usage of wp_add_inline_style and wp_add_inline_script in WordPress

3 regular usages of wp_add_inline_style and wp_add_inline_script

Add CSS Style without dependency

wp_register_style( 'wp-juzhax-css', false );
wp_enqueue_style( 'wp-juzhax-css' );
wp_add_inline_style( 'wp-juzhax-css', "body *{ box-sizing: inherit; }");

Add JavaScript Code without dependency

wp_register_script( 'wp-juzhax-js-header', '',);
wp_enqueue_script( 'wp-juzhax-js-header' );
wp_add_inline_script( wp-juzhax-js-header', "console.log('loaded in header');");

Add JavaScript Code with jQuery dependency

wp_register_script( 'wp-juzhax-js-footer', '', array("jquery"), '', true );
wp_enqueue_script( 'wp-juzhax-js-footer'  );
wp_add_inline_script( 'wp-juzhax-js-footer', "console.log('loaded in footer');");

Tags:

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.