WordPress Updated today!!

Oh yea!! WordPress has just updated today to version 4.5 code name “COLEMAN”.  Apart from the editing (TinyMec/WYSIWYG editor) and customization (ability to view your site from perspective of desktop, tablet or phone’s browser) changes. We have also been introduced with a few developer friendly features. They are,

  1. the introduction  of wp_add_inline_script() and
  2. some awesome changes in the embed feature which was introduced in WordPress version 4.4

I will not talk about the editing and customization features and the embed feature update today. I am going to keep those for some later date. I am the most excited about wp_add_inline_script().  I’ll talk about only this today. Because it is exciting. Ha ha ha….

wp_add_inline_script() lets you add inline scripts, well to be exact, JavaScript! Doesn’t it feel good! Before this update we had to add inline scripts via custom functions and add that action to wp_head() or wp_footer() depending on the necessity. Now, we can directly add inline scrips to our theme without any hassle and with much less code. let me provide you an example. Lets assume, we have an awesome javascript file named ‘awesome-js.min.js’ and we enqueue the file to our beautiful upcoming theme. But, it wont do anything if it doesn’t know which DOM element it has to work with. We have to put a inline script that will let this know which element to use.

function add_awesoem_script() {
wp_enqueue_script( 'awesoem_script', 'https://thedomain.com/awesome-js.min.js', array( 'jquery' ), '1.0.1', true );
wp_add_inline_script( 'awesoem_script', 'jQuery(document).ready(function($) { $(".awesome_div").awesomeJs({ do_awesome_thing: 6 }); });' );
add_action( 'wp_enqueue_scripts', 'add_awesoem_script' );

This is it the output will be somewhat like the following.

<script type='text/javascript' src='https://thedomain.com/awesome-js.min.js'></script>
<script type='text/javascript'>
jQuery(document).ready(function($) {
$(".awesome_div").awesomeJs({ do_awesome_thing: 6 });

There you go. I will try to write as soon as I get some time. By the way, rss ticker plugin is almost finished. I am going through a very strict scheduled now, so its taking some time. But as soon as I am done with it, will publish it on the wp’s plugin repository. Cheers!!

Leave a Reply