Skip to content

Instantly share code, notes, and snippets.

@fumikito
Created August 23, 2010 10:04
Show Gist options
  • Save fumikito/545180 to your computer and use it in GitHub Desktop.
Save fumikito/545180 to your computer and use it in GitHub Desktop.
Sample of loading assets in WordPress
<?php
/*
* @pakage WordPress
* This file resolve external js and css loading and their chaching.
* Put this file in your theme foloer and
* write the line below in your theme's "functions.php"
*
* require_once(TEMPLATEPATH."/wp-assets.php");
*
*/
// define version
define("MY_THEME_VERSION", "1.0");
/**
* Hook on "init" action.
*/
function my_thmeme_init(){
//If this loaded page is not admin panel
if(!is_admin()){
add_action("wp_enqueue_scripts", "my_enqueue_script");
add_action("wp_enqueue_styles", "my_enqueue_style");
}
}
add_action("init", "my_theme_init");
/**
* Load CSS. Add "wp_enqueuew_style" function as you like.
*/
function my_enqueue_style(){
/*
* About wp_enqueue_style, see below
* http://codex.wordpress.org/Function_Reference/wp_enqueue_style
*/
wp_enqueue_style(
"my-theme",
get_bloginfo("stylesheet_url"),
array(),
MY_THEME_VERSION
"screen"
);
//This "if" block is valid only in Internet Explorer.
global $is_winIE;
if($is_winIE){
wp_enqueue_style(){
"ie6",
get_bloginfo("template_directory")."/js/ie6.css"
}
}
}
/**
* Load JS
*/
function my_enqueue_script(){
/*
* About wp_enqueue_script, see below
* http://codex.wordpress.org/Function_Reference/wp_enqueue_script
*/
wp_enqueue_script(
"my-script",
get_bloginfo("template_directory")."/js/my.js",
array("jquery"),
MY_THEME_VERSION,
true //If true, <script>tag will be written in wp_footer
);
if(is_singular()){ //You can use any conditional tags.
wp_enqueue_script(
"my-single",
get_bloginfo("template_directory")."/js/single.js",
array("prototype"),
MY_THEME_VERSION,
true
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment