Adding jquery and custom scripts to a Wordpress theme -



Adding jquery and custom scripts to a Wordpress theme -

doing searching through google, came across same bit of code on , on adding jquery from-scratch wordpress theme.

if (!is_admin()) add_action("wp_enqueue_scripts", "my_jquery_enqueue", 11); function my_jquery_enqueue() { wp_deregister_script('jquery'); wp_register_script('jquery', "http" . ($_server['server_port'] == 443 ? "s" : "") . "://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js", false, null); wp_enqueue_script('jquery'); wp_enqueue_script('my_script', get_bloginfo('template_url') . '/js/theme.js', array('jquery'), '1.0', true); }

i've added code functions.php file, , created js folder , made theme.js file using next syntax in it:

jquery(function ($) { /* can safely utilize $ in code block reference jquery */ });

when refresh wp site, theme.js file doesn't seem called. in chrome dev tools, it's not listed among js files beingness rendered.

am using outdated approach? how can create /js/theme.js file, using jquery, work theme?

first, wp_enqueue_scripts runs on frontend, don't need is_admin() check.

second, de-register default jquery (bundled wp) if know doing. in example, loading outdated version google (current 1.8.3, not 1.7.1). also, see: don’t dequeue wordpress’ jquery

third, should using get_stylesheet_directory_uri, right function count parent , kid theme folders.

finally, code works ok in /themes/my-theme/functions.php:

add_action( "wp_enqueue_scripts", "my_js_so_14864221", 11 ); function my_js_so_14864221() { wp_enqueue_script( 'my_script', get_stylesheet_directory_uri() . '/js/theme.js', array( 'jquery' ), '1.0', true ); }

and jquery code in theme.js should encapsulated like:

jquery(document).ready(function($) { // $('#your-stuff').animate().hide().whatever(); });

jquery wordpress wordpress-theming

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -