I am creating a custom page template that would include this jQuery multi-selector on it, but I cannot seem to make it work no matter what I do.
Both CSS and JS files seem to get added to the theme header file but this is what I keep seeing when I paste the selector code:
My template file code:
<select id='pre-selected-options' multiple='multiple'>
<option value='elem_1' selected>elem 1</option>
<option value='elem_2'>elem 2</option>
<option value='elem_3'>elem 3</option>
<option value='elem_4' selected>elem 4</option>
<option value='elem_100'>elem 100</option>
</select>
My functions.php file:
function wptuts_scripts_load_cdn()
{
wp_register_script( 'multi-select', '.multi-select.js', array(), null, false );
wp_register_script( 'bs4', '.0.0-alpha/js/bootstrap.min.js', array(), null, false );
}
add_action( 'wp_enqueue_scripts', 'wptuts_scripts_load_cdn' );
function wptuts_styles_with_the_lot()
{
// Register the style like this for a theme:
wp_register_style( 'custom-style', '.css', array(), '20120208', 'all' );
}
add_action( 'wp_enqueue_scripts', 'wptuts_styles_with_the_lot' );
I'm using Avada theme if that matters.
What might be the problem here?
I am creating a custom page template that would include this jQuery multi-selector on it, but I cannot seem to make it work no matter what I do.
Both CSS and JS files seem to get added to the theme header file but this is what I keep seeing when I paste the selector code:
My template file code:
<select id='pre-selected-options' multiple='multiple'>
<option value='elem_1' selected>elem 1</option>
<option value='elem_2'>elem 2</option>
<option value='elem_3'>elem 3</option>
<option value='elem_4' selected>elem 4</option>
<option value='elem_100'>elem 100</option>
</select>
My functions.php file:
function wptuts_scripts_load_cdn()
{
wp_register_script( 'multi-select', 'https://www.mywebsite.com/selector/js/jquery.multi-select.js', array(), null, false );
wp_register_script( 'bs4', 'https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/js/bootstrap.min.js', array(), null, false );
}
add_action( 'wp_enqueue_scripts', 'wptuts_scripts_load_cdn' );
function wptuts_styles_with_the_lot()
{
// Register the style like this for a theme:
wp_register_style( 'custom-style', 'https://www.mywebsite.com/selector/css/multi-select.css', array(), '20120208', 'all' );
}
add_action( 'wp_enqueue_scripts', 'wptuts_styles_with_the_lot' );
I'm using Avada theme if that matters.
What might be the problem here?
Here's a tweaked version of the original code. get_template_directory_uri()
is used instead of hard coding the URLs, the scripts and styles are enqueud rather than just being registered, and the dependencies are specified.
<?php
function wptuts_scripts_load_cdn() {
wp_enqueue_script( 'multi-select', get_template_directory_uri() . '/selector/js/jquery.multi-select.js', array( 'jquery' ), null, false );
// JavaScript for theme. Presumably where you'd want to initialize the multi-select element.
//wp_enqueue_script( 'theme-scripts', get_template_directory_uri() . '/js/theme-scripts.js', array( 'jquery', 'multi-select' ), null, false );
wp_enqueue_script( 'bs4', 'https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/js/bootstrap.min.js', array(), null, false );
}
add_action( 'wp_enqueue_scripts', 'wptuts_scripts_load_cdn' );
function wptuts_styles_with_the_lot() {
// Enqueue the style like this for a theme:
wp_enqueue_style( 'custom-style', get_template_directory_uri() . '/selector/css/multi-select.css', array(), '20120208' );
}
add_action( 'wp_enqueue_scripts', 'wptuts_styles_with_the_lot' );
$('#pre-selected-options').multiSelect();
I see that you're including the main library, but you need to load the JS (and specify dependencies, 'jquery' and 'multi-select' ) that attaches the Multiselect library to your select element. BTW, the 'multi-select' script should specifyjquery
as a dependency. – Dave Romsey Commented Sep 26, 2016 at 23:04wp_enqueue_script
, particularly the$deps
argument. Also, replace the use ofwp_register_script
withwp_enqueue_script
and usewp_enqueue_style
instead ofwp_register_style
. Registering a script or stylesheet does not actually enqueue it. – Dave Romsey Commented Sep 26, 2016 at 23:20