Custom JS text area in customizer is being formatted wrong in document

@sam-skirrow just posting here what we did to fix this… WordPress 4.1 requires us to use a sanitize_callback filter when creating settings for the customizer (with good reason). Since you’re using the kirki framework to create these customizer settings, kirki detects that this is a textarea field and so it automatically applies the esc_textarea filter. … Read more

php variable inside javascript code

This should work: <?php $script = “<script type=”text/javascript”> jQuery( document ).ready( function($) { $(document).ready(function() { $(‘#particles’).particleground({ dotColor: ” . json_encode($dotcolor) . “, lineColor: ‘#5cb9bd’, }); }); } ); </script>”; ?> Note: Its a better practice to enqueue Javascripts with wp_enqueue_script() and include dynamic strings via wp_localize_script(). Docs: https://codex.wordpress.org/Function_Reference/wp_enqueue_script https://codex.wordpress.org/Function_Reference/wp_localize_script

Custom JS on a specific page

First of all, you should declare your scripts using wp_enque_script. Second, jQuery is loaded automatically in WordPress and is set to no conflict mode. See the documentation for how to link a script that depends on jQuery. Lastly, if you want it to appear on just one page you have two options In your functions.php … Read more

Add JavaScript to single post

One option would be to create a shortcode exclusively for that script. Even thought it’s not an elegant solution, it works. In your functions file (functions.php) of your theme add: function custom_script_shortcode(){ $code=”<script>”; $code .= ‘var foo = “bar”;’ $code .= ‘</script>’; return $code; } add_shortcode(“custom_script_shortcode”, “custom_script”); And in your post editor then add the … Read more

Including Styles and JS files to work ON my plugin interface

Well after a bit of better searching i found the answer, function my_enqueue($hook) { if ( ‘settings_page_data-layer-management’ != $hook ) { return; } wp_enqueue_script( ‘my_custom_script’, plugins_url( ‘js/dlm-window.js’, __FILE__ )); } add_action( ‘admin_enqueue_scripts’, ‘my_enqueue’ ); to explain just incase anyone has this issue. what i had earlier was a ‘front-end’ hook which is just wp_enqueue_scripts for … Read more

CDATA removing new line in script tag in wordpress

This is being caused by core bug #33106 which was unfortunately introduced with the security fixes in 4.1.6 (and 4.2.3). New lines are erroneously being stripped from CDATA blocks in this situation. From the bug ticket: As of 4.2.3, depending on how a CDATA block is used, the stripping of new lines in this content … Read more

print script on wordpress header after user registered

There’s quite a few issues here: do_action(‘user_register’, $user_id ); No need to fire the action yourself (especially with an undefined variable $user_id) – the idea of actions & filters is that you “hook” your function to one, and then let WordPress trigger it when applicable. function MY_Callback(){ $data[‘user_id’] = $user_id; My_print_js($data); } Again $user_id is … Read more

Using javascript on my site to create nested tabs

A couple of things before I get into the code: 1) It would probably be a cleaner solution to solve this in the theme via extended functionality (PHP modifications) as you gain more experience with WordPress. 2) If you are able to use numbers (1,2,3) instead of words (one,two,three) you can clean up the javascript … Read more