Jun 25, 2012 - Javascript, WordPress    Comments Off on Javascript on just ONE Admin Page

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' );
}
Jun 11, 2012 - Javascript, jQuery    Comments Off on Making URL fields idiot resistant.

Making URL fields idiot resistant.

	jQuery(document).ready(function($) {
	    $('#url_field_id').blur(function(){
		    var myval = $(this).val().toLowerCase().replace(/[^a-z0-9]+/g,'');
			$(this).val(myval);
		    });
	});
Jun 5, 2012 - WordPress    Comments Off on Widgets Style

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.

May 31, 2012 - WordPress    Comments Off on Clean up WordPress revisions

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

Apr 17, 2012 - PHP    Comments Off on Require Dir

Require Dir

if(!function_exists('requireDir')){
function requireDir($dir){
	$dh = @opendir($dir);

	if (!$dh) {
		throw new Exception("Cannot open directory $dir");
	} else {
		while (($file = readdir($dh)) !== false) {
			if ($file != '.' && $file != '..') {
				$requiredFile = $dir . DIRECTORY_SEPARATOR . $file;
				if ('.php' === substr($file, strlen($file) - 4)) {
					require_once $requiredFile;
				} elseif (is_dir($requiredFile)) {
					requireDir($requiredFile);
				}
			}
		}
	closedir($dh);
	}
	unset($dh, $dir, $file, $requiredFile);
}
}
Apr 13, 2012 - Genesis    Comments Off on Adding Meta to the Theme Stettings

Adding Meta to the Theme Stettings

Something changed in 1.8+ and the old hook no longer works. To make the hook work again, and keep things backwards compatible, try something like this:

add_action( 'admin_menu', 'childtheme_add_footer_tag_settings_box', 11 );
/**
 * Add new box to the Genesis->Theme Settings page.
 *
 */
function childtheme_add_footer_tag_settings_box() {
	global $_genesis_theme_settings_pagehook;
	if(!$_genesis_theme_settings_pagehook){
		$_genesis_theme_settings_pagehook = 'toplevel_page_genesis';
	}
	add_meta_box( 'genesis-theme-settings-style', __( 'Meta Box title', 'childtheme' ), 'childtheme_theme_settings_footer_tag_box', $_genesis_theme_settings_pagehook, 'column2' );
}
Mar 22, 2012 - Mobile, WordPress    Comments Off on Responsive design: adding scripts and styles

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)');     
    }
}
Mar 22, 2012 - WordPress    Comments Off on Adding theme scripts

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);    
    	}
	}
}

Feb 26, 2012 - CSS, jQuery    Comments Off on jQuery add classes to lists and tables

jQuery add classes to lists and tables

jQuery(document).ready(function($) {	
	$('ul li:first-child').addClass('first-child');
	$('ul li:last-child').addClass('last-child');
	$('ul li:nth-child(even)').addClass('even');
	$('ul li:nth-child(odd)').addClass('odd');
	$('table tr:first-child').addClass('first-child');
	$('table tr:last-child').addClass('last-child');
	$('table tr:nth-child(even)').addClass('even');
	$('table tr:nth-child(odd)').addClass('odd');
	$('tr td:first-child').addClass('first-child');
	$('tr td:last-child').addClass('last-child');
	$('tr td:nth-child(even)').addClass('even');
	$('tr td:nth-child(odd)').addClass('odd');
});

Or better yet:

jQuery(document).ready(function($) {	
	$('*:first-child').addClass('first-child');
	$('*:last-child').addClass('last-child');
	$('*:nth-child(even)').addClass('even');
	$('*:nth-child(odd)').addClass('odd');
});