PhotoDune

How do you keep your scripts organised in Wordpress?

308 posts
  • Elite Author
  • Exclusive Author
  • Has been a member for 3-4 years
  • New Zealand
  • Referred between 10 and 49 users
  • Bought between 1 and 9 items
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Sold between 50 000 and 100 000 dollars
PixelStores says

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

2507 posts Put a Donk On It
  • Exclusive Author
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Attended a Community Meetup
  • Referred between 100 and 199 users
  • Contributed a Tutorial to a Tuts+ Site
+5 more
ThemeProvince says
wp_deregister_script('jquery'); wp_enqueue_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js', false);

facepalm

308 posts
  • Elite Author
  • Exclusive Author
  • Has been a member for 3-4 years
  • New Zealand
  • Referred between 10 and 49 users
  • Bought between 1 and 9 items
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Sold between 50 000 and 100 000 dollars
PixelStores says

wp_deregister_script('jquery'); wp_enqueue_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js', false);
facepalm

Slightly confused :(

2507 posts Put a Donk On It
  • Exclusive Author
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Attended a Community Meetup
  • Referred between 100 and 199 users
  • Contributed a Tutorial to a Tuts+ Site
+5 more
ThemeProvince says


wp_deregister_script('jquery'); wp_enqueue_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js', false);
facepalm
Slightly confused :(

Dude why don’t you just use: wp_enqueue_script('jquery'); , it does the job.

2955 posts
  • Community Superstar
  • Sold between 1 000 and 5 000 dollars
  • Bought between 10 and 49 items
  • Has been a member for 3-4 years
  • United States
  • Exclusive Author
chrisakelley says

wp_deregister_script('jquery'); wp_enqueue_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js', false);
facepalm
Hero**
1129 posts
  • Has been a member for 5-6 years
  • Sold between 10 000 and 50 000 dollars
  • Exclusive Author
  • Interviewed on the Envato Notes blog
  • Philippines
  • Bought between 10 and 49 items
  • Referred between 200 and 499 users
mabuc says


wp_deregister_script('jquery'); wp_enqueue_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js', false);
facepalm
Slightly confused :(

your using 2 jquery library right? :)

308 posts
  • Elite Author
  • Exclusive Author
  • Has been a member for 3-4 years
  • New Zealand
  • Referred between 10 and 49 users
  • Bought between 1 and 9 items
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Sold between 50 000 and 100 000 dollars
PixelStores says



wp_deregister_script('jquery'); wp_enqueue_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js', false);
facepalm
Slightly confused :(
Dude why don’t you just use: wp_enqueue_script('jquery'); , it does the job.

My bad, rookie mistake. :silly: 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?

2955 posts
  • Community Superstar
  • Sold between 1 000 and 5 000 dollars
  • Bought between 10 and 49 items
  • Has been a member for 3-4 years
  • United States
  • Exclusive Author
chrisakelley says




wp_deregister_script('jquery'); wp_enqueue_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js', false);
facepalm
Slightly confused :(
Dude why don’t you just use: wp_enqueue_script('jquery'); , it does the job.

My bad, rookie mistake. :silly: 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?

You can roll that into 1 function for scripts and 1 for style

308 posts
  • Elite Author
  • Exclusive Author
  • Has been a member for 3-4 years
  • New Zealand
  • Referred between 10 and 49 users
  • Bought between 1 and 9 items
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Sold between 50 000 and 100 000 dollars
PixelStores says





wp_deregister_script('jquery'); wp_enqueue_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js', false);
facepalm
Slightly confused :(
Dude why don’t you just use: wp_enqueue_script('jquery'); , it does the job.

My bad, rookie mistake. :silly: 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?
You can roll that into 1 function for scripts and 1 for style

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 :)

by
by
by
by
by