Hey!
I’m in the development of creating my theme and a problem has occurred to me that i’ll be using a fair number of scripts for the theme. My question is rather on the PHP side than the folder structure. Many themes have a number of sliders available as a feature. Here is the code for loading my scripts that rely on the custom fields for the different sliders.
function theme_mb_scripts() {
global $theme_mb, $theme_mb_side;
$meta = get_post_meta(get_the_ID(), $theme_mb->get_the_id(), TRUE);
$sidemeta = get_post_meta(get_the_ID(), $theme_mb_side->get_the_id(), TRUE);
if (!is_admin()) {
wp_deregister_script('jquery');
wp_enqueue_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js', false);
wp_enqueue_script('tinycarousel', get_template_directory_uri() . '/assets/js/jquery.tinycarousel.min.js');
wp_enqueue_script( 'vs_custom', get_template_directory_uri() . '/assets/js/jquery.custom.js', array( 'jquery' ) );
wp_enqueue_script('superfish', get_template_directory_uri() . '/assets/js/superfish.min.js');
if(!empty($sidemeta[firefly])) {
wp_enqueue_script('firefly', get_template_directory_uri() . '/assets/js/jquery.firefly.min.js');
wp_enqueue_script( 'fly_custom', get_template_directory_uri() . '/assets/js/jquery.fireflycustom.js', array( 'jquery' ) );
wp_localize_script( 'fly_custom', 'vs_ajax', array( 'spark' => get_template_directory_uri() . '/assets/js/images/spark1.png', 'secondspark' => get_template_directory_uri() . '/assets/js/images/spark2.png' ) );
}
if(!empty($sidemeta[bgimgurl])) {
wp_enqueue_script('flexbg', get_template_directory_uri() . '/assets/js/flexi-background.js');
}
if($meta[slider] == "NivoSlider") {
wp_enqueue_script('jeasing', get_template_directory_uri() . '/assets/js/jquery.easing.1.3.js');
wp_enqueue_script('nivoslider', get_template_directory_uri() . '/includes/sliders/nivoslider/jquery.nivo.slider.pack.js');
}
}
}
function theme_mb_styles() {
global $theme_mb;
$meta = get_post_meta(get_the_ID(), $theme_mb->get_the_id(), TRUE);
if (!is_admin()) {
wp_enqueue_style('googlefont', 'http://fonts.googleapis.com/css?family=PT+Sans');
}
}
add_action('wp_enqueue_scripts', 'theme_mb_scripts');
add_action('wp_enqueue_scripts', 'theme_mb_styles');
function nivoslider_styles() {
global $theme_mb;
$meta = get_post_meta(get_the_ID(), $theme_mb->get_the_id(), TRUE);
if (!is_admin()) {
if($meta[slider] == "NivoSlider") {
wp_enqueue_style('nivoslidercss', get_template_directory_uri(). '/includes/sliders/nivoslider/nivo-slider.css');
}
}
}
function nivoslider_scripts() {
global $post, $theme_mb;
$meta = get_post_meta(get_the_ID(), $theme_mb->get_the_id(), TRUE);
if (!is_admin()) {
if($meta[slider] == "NivoSlider") {
wp_enqueue_script( 'vs_nivoslider', get_template_directory_uri() . '/includes/sliders/nivoslider/jquery.nivosettings.js', array( 'jquery' ) );
wp_localize_script('vs_nivoslider','nivo',
array(
'effect' => $meta[effect],
'slices' => $meta[slices],
'boxCols' => $meta[boxCols],
'boxRows' => $meta[boxRows],
'pauseTime' => $meta[pauseTime],
'startSlide' => $meta[startSlide],
'directionNav' => $meta[directionNav],
'directionNavHide' => $meta[directionNavHide],
'pauseOnHover' => $meta[pauseOnHover],
'manualAdvance' => $meta[manualAdvance],
'controlNav' => $meta[controlNav],
'randomStart' => $meta[randomStart]
)
);
}
}
}
add_action( 'wp_enqueue_scripts', 'nivoslider_scripts' );
add_action( 'wp_enqueue_scripts', 'nivoslider_styles' );
What would you do in this circumstance to reduce the code but load the dependant scripts/styles for the sliders and theme?
Thanks
- Exclusive Author
- Repeatedly Helped protect Envato Marketplaces against copyright violations
- Sold between 250 000 and 1 000 000 dollars
- Author was Featured
- Item was Featured
- Attended a Community Meetup
- Referred between 100 and 199 users
- Contributed a Tutorial to a Tuts+ Site
wp_deregister_script('jquery'); wp_enqueue_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js', false);
facepalm
ThemeProvince said
wp_deregister_script('jquery'); wp_enqueue_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js', false);facepalm
Slightly confused 
- Exclusive Author
- Repeatedly Helped protect Envato Marketplaces against copyright violations
- Sold between 250 000 and 1 000 000 dollars
- Author was Featured
- Item was Featured
- Attended a Community Meetup
- Referred between 100 and 199 users
- Contributed a Tutorial to a Tuts+ Site
PixelStores said
ThemeProvince saidSlightly confused
wp_deregister_script('jquery'); wp_enqueue_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js', false);facepalm![]()
Dude why don’t you just use: wp_enqueue_script('jquery'); , it does the job.
ThemeProvince saidHero**
wp_deregister_script('jquery'); wp_enqueue_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js', false);facepalm
PixelStores said
ThemeProvince saidSlightly confused
wp_deregister_script('jquery'); wp_enqueue_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js', false);facepalm![]()
your using 2 jquery library right? 
ThemeProvince said
PixelStores saidDude why don’t you just use:
ThemeProvince saidSlightly confused
wp_deregister_script('jquery'); wp_enqueue_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js', false);facepalm![]()
wp_enqueue_script('jquery');, it does the job.
My bad, rookie mistake.
Thanks.
Now for the organisation of scripts that are loaded from the values of the custom fields, do you reckon i should use the wp_enqueue_scripts more than once?
PixelStores said
ThemeProvince said
PixelStores saidDude why don’t you just use:
ThemeProvince saidSlightly confused
wp_deregister_script('jquery'); wp_enqueue_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js', false);facepalm![]()
wp_enqueue_script('jquery');, it does the job.My bad, rookie mistake.
Now for the organisation of scripts that are loaded from the values of the custom fields, do you reckon i should use the wp_enqueue_scripts more than once?Thanks.
You can roll that into 1 function for scripts and 1 for style
OrganicBeeMedia said
PixelStores saidYou can roll that into 1 function for scripts and 1 for style
ThemeProvince said
PixelStores saidDude why don’t you just use:
ThemeProvince saidSlightly confused
wp_deregister_script('jquery'); wp_enqueue_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js', false);facepalm![]()
wp_enqueue_script('jquery');, it does the job.My bad, rookie mistake.
Now for the organisation of scripts that are loaded from the values of the custom fields, do you reckon i should use the wp_enqueue_scripts more than once?Thanks.
Yer probably be the best way for it, i’m probably worrying to much about having to much code, especially when i’m adding new sliders to the theme.
Cheers guys 
