Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

In theme development, what is the best way to register and enqueue JavaScript files to avoid problems with plugins?

I want to create a theme which uses scripts like jQuery, Masonry and so on. I downloaded the scripts and added them via the reqister and enqueue function to avoid problems with Wordpress Updates.

function my_scripts() {
  // Register scripts
  wp_register_script( 'my_masonry', get_template_directory_uri() . '/js/masonry.pkgd.min.js', array(), '1.0.0', true ); 
  // Add scripts before closing body tag.
  wp_enqueue_script( 'my_masonry', true );
}

add_action( 'wp_enqueue_scripts', 'my_scripts' );

The other way would be to use the built-in Wordpress scripts to may avoid problems with plug-ins.

function my_scripts() {
  // Add scripts before closing body tag.
  wp_enqueue_script( 'masonry', true );
}

add_action( 'wp_enqueue_scripts', 'my_scripts' );

What is the better approach?

share|improve this question
1  
Always use core-bundled scripts, rather than custom versions of those scripts. Core, Themes, and Plugins all rely on the assumption that the core-bundled version of a script will be registered and available to be enqueued. –  Chip Bennett Apr 18 at 12:40
add comment

2 Answers 2

up vote 2 down vote accepted

I would use jquery-masonry included in Wordpress core:

function my_scripts() {
    wp_enqueue_script( 'jquery-masonry', true );
}
add_action( 'wp_enqueue_scripts', 'my_scripts' );

If you really need the standalone masonry library, you should use your "first way" but I would not use "my_masonry" as handle for the script, I would use "masonry" if you have not modified the code. Also, use the correct version number of the library (I think your are not really using 1.0.0).

share|improve this answer
add comment

function theme_name_scripts() { wp_enqueue_style( 'style- name', get_stylesheet_uri() ); wp_enqueue_script( 'script- name', get_template_directory_uri () . '/js/example.js', array(), '1.0.0', true ); } add_action( 'wp_enqueue_scripts', 'theme_name_scripts' );

add to tour function php.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.