Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End WP – How to add a script to a specific page Reply To: WP – How to add a script to a specific page

#182152
Alen
Participant

add_action( 'wp_enqueue_scripts', 'init_theme_scripts_styles' );
function init_theme_scripts_styles()
{

  global $wp_styles;

  # scripts
  # following code echos jQuery
  wp_enqueue_script( 'jquery' );

  # wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer );
  # Following code loads master.min.js from the js folder in your theme
  # see: http://codex.wordpress.org/Function_Reference/wp_enqueue_script
  wp_enqueue_script( 'main', get_template_directory_uri() . '/js/master.min.js', ['jquery'], '1.0', true );

  # this is a conditional to check if we are on certain page, then we echo our whatever script
  # you can do these conditionals for both, the CSS/JS within this code
  if ( is_page( 'ENTER-SLUG-HERE' ) ) {
    wp_enqueue_script( 'NAME', get_template_directory_uri() . '/js/NAME.js', ['jquery'], '', true );
  }

  # styles
  # wp_enqueue_style( $handle, $src, $deps, $ver, $media );
  # see: http://codex.wordpress.org/Function_Reference/wp_enqueue_style
  # following code loads master.min.css file from the css folder in your theme
  wp_enqueue_style( 'main', get_template_directory_uri() . '/css/master.min.css', [], '1.0', 'all' );
}