Category: WordPress

more on single page/post JS

http://scribu.net/wordpress/optimal-script-loading.html

class My_Shortcode {
	static $add_script;

	static function init() {
		add_shortcode('myshortcode', array(__CLASS__, 'handle_shortcode'));

		add_action('init', array(__CLASS__, 'register_script'));
		add_action('wp_footer', array(__CLASS__, 'print_script'));
	}

	static function handle_shortcode($atts) {
		self::$add_script = true;

		// actual shortcode handling here
	}

	static function register_script() {
		wp_register_script('my-script', plugins_url('my-script.js', __FILE__), array('jquery'), '1.0', true);
	}

	static function print_script() {
		if ( ! self::$add_script )
			return;

		wp_print_scripts('my-script');
	}
}

My_Shortcode::init();

Sneaky OrderBy stuff.

When ordering a query by meta_value, if you are using a numeric meta_value, do this:

$args = array( 
	'post_type' => 'msd_contest_entry', 
	'numberposts' => -1, 
	'meta_key' => 'Votes',
	'orderby' => 'meta_value_num'
	);
$items = get_posts($args);

Otherwise:

$args = array( 
	'post_type' => 'msd_contest_entry', 
	'numberposts' => -1, 
	'meta_key' => 'Votes',
	'orderby' => 'meta_value'
	);
$items = get_posts($args);

Javascript on just ONE Admin Page

I ran into this issue recently when I had a setting page that allowed a file upload (image for logo) and the script for it started overriding the general image uploader across the entire admin. Grrf. A better way to hook the .js is found on Justin Tadlock’s blog. (I wasn’t able to do this exactly, but I used the idea.)

add_action( 'admin_menu', 'my_example_settings_page' );

function my_example_settings_page() {
	global $my_settings_page;

	$my_settings_page = add_options_page( 'DevPress Example', 'DevPress Example', 'manage_options', 'my-example-page-name', 'my_callback_function' );

	add_action( 'admin_enqueue_scripts', 'my_admin_enqueue_scripts' );
}

function my_admin_enqueue_scripts( $hook_suffix ) {
	global $my_settings_page;

	if ( $my_settings_page == $hook_suffix )
		wp_enqueue_script( 'my-example' );
}

Widgets Style

Plugin idea: a plugin to add the ability to wrap any widget with a class. Would be great for the custom styled widgets like Cole likes.

Update: http://wordpress.org/extend/plugins/zigwidgetclass/ Perfect.

Clean up WordPress revisions

Salut,

Ce n’est pas suffisant, il y a deux autres tables affectees par l’autosave (wp_term_relationships et wp_postmeta), le code complet serait:

delete x,y,z
from wp_posts x
left join wp_term_relationships y on (x.id = y.object_id)
left join wp_postmeta z on (x.id = z.post_id)
where x.post_type = 'revision'

 

found in the comments over here

Responsive design: adding scripts and styles

add_action('wp_print_scripts', 'themename_add_scripts');
 
function themename_add_scripts() {
	if(!is_admin()){
    	wp_enqueue_script('theme_jquery', get_bloginfo('template_url').'/js/theme-jquery.js', array('jquery'),NULL,TRUE); 
    	if(is_front_page()){
    		wp_enqueue_script('homepage_jquery', get_bloginfo('template_url').'/js/homepage-jquery.js', array('jquery'),NULL,TRUE);    
    	}
       	if(is_mobile()){
       		//change some content with jQuery
       		wp_enqueue_script('mobile_jquery', get_bloginfo('template_url').'/js/mobile-jquery.js', array('jquery'),NULL,TRUE); 
       		//add support for older iphones
			$agent = $_SERVER['HTTP_USER_AGENT'];
			$version = preg_match('/OS\s([\d+_?]*)\slike/i', $agent, $matches);
			$version = str_replace('_', '.', $matches[1]);
			if (!(preg_match('/ip(hone|ad|od)/i', $agent)) || ((preg_match('/ip(hone|ad|od)/i', $agent) && $version < 5))) {
				wp_enqueue_script('ios4_jquery', get_bloginfo('template_url').'/js/ios4-jquery.js', array('jquery'),NULL,TRUE); 
			}
       	}
	}
}
add_action('wp_print_styles', 'themename_add_styles');
 
function themename_add_styles() {
    if(!is_admin()){
        wp_enqueue_style('desktop', get_stylesheet_directory_uri().'/css/960.css', NULL, NULL, 'screen and (min-width: 769px)');   
        wp_enqueue_style('ipad', get_stylesheet_directory_uri().'/css/ipad.css', NULL, NULL, 'only screen and (min-device-width : 768px) and (max-device-width : 1024px)');           
        wp_enqueue_style('mobile', get_stylesheet_directory_uri().'/css/mobile.css', NULL, NULL, 'only screen and (max-width: 768px)');     
    }
}

Adding theme scripts

add_action('wp_print_scripts', 'themename_add_scripts');
 
function themename_add_scripts() {
	if(!is_admin()){
    	wp_enqueue_script('theme_jquery', get_stylesheet_directory_uri().'/js/theme-jquery.js', array('jquery'),NULL,TRUE);    
    	if(is_front_page()){
    		wp_enqueue_script('homepage_jquery', get_stylesheet_directory_uri().'/js/homepage-jquery.js', array('jquery'),NULL,TRUE);    
    	}
	}
}

Those wacky theme paths

  • get_template_directory()

    : Returns the absolute template directory path. (PARENT)

  • get_template_directory_uri()

    : Returns the template directory URI.

  • get_stylesheet_directory()

    : Returns the absolute stylesheet directory path. (CHILD)

  • get_stylesheet_directory_uri()

    : Returns the stylesheet directory URI.

See also: How To Load Files Within WordPress Themes