I’ve spent quite a bit of time programming in WordPress, somewhere around 6 years to be exact. Over the time I’ve developed a lot plugins and themes, ranging from minor theme updates, to plugin customizations, automation, a full fledged CRM, WP-Invoice, WP-Property, etc.
In any case, here are some tips and tricks that I use quite often when it comes to plugin development.
Inserting Scripts and Styles into Backend Pages
This is the method I use to quickly setup a backend page and insert the scripts:
// Call my_class::my_class() on WordPress init hook
add_action("init", create_function('', 'new my_class;'));
class my_class {
/**
* Registers styles and loads my_class::admin_menu() into WordPress' admin_menu hook
*
* @uses wp_register_style()
* @uses wp_register_script()
* @uses add_action()
*/
function my_class() {
// Define path
define('WPP_URL', WP_PLUGIN_URL . '/wp-properties');
// Register styles ands cripts early on so they can be loaded later on specific pages
wp_register_style('jquery-fancybox-css',
WPP_URL. '/third-party/fancybox/jquery.fancybox-1.3.1.css'
);
wp_register_script('jquery-fancybox',
WPP_URL. '/third-party/fancybox/jquery.fancybox-1.3.1.pack.js',
array('jquery'), '1.7.3'
);
// Call my_class::admin_menu() function;
add_action('admin_menu', array('my_class', 'admin_menu'));
}
/**
* Creates a page called "Slideshow" under the gallery and loads scripts and styles.
*
* add_submenu_page() returns a string which is used by WP as a "slug" for the page we added.
* We add an action to "'admin_print_scripts-' . $slideshow_page", which is only called on our slideshow page.
*
* Usually I see developers calling another function that will enqueue scripts and styles, but
* I prefer to keep the function count down whenever possible, and choose to use create_function()
* to return wp_enqueue_script() and wp_enqueue_style() into the header of admin_print_scripts-' . $slideshow_page
*
* @uses add_submenu_page()
* @uses add_action()
*/
function admin_menu() {
// Add submenu page under "Media Galllery"
$slideshow_page = add_submenu_page(
'upload.php',
"Slideshow",
"Slideshow",
10,
'slideshow',
array('wpp_slideshow', 'page'
));
// Load FancyBox JS slideshow page
add_action('admin_print_scripts-' . $slideshow_page, create_function('', "
wp_enqueue_script('jquery-fancybox');
wp_enqueue_script( 'jquery-ui-sortable');
"));
// Load FancyBox CSS slideshow page
add_action('admin_print_styles-' . $slideshow_page, create_function('', "
wp_enqueue_style('jquery-fancybox-css');
"));
}
}
I realize create_function() is slower than using real functions, I haven’t done any benchmarking to see how much slower, but I have a feeling that when used in this manner the performance degradation is nominal.
WordPress Programming Shortcuts and Tricks
I’ve spent quite a bit of time programming in WordPress, somewhere around 6 years to be exact. Over the time I’ve developed a lot plugins and themes, ranging from minor theme updates, to plugin customizations, automation, a full fledged CRM, WP-Invoice, WP-Property, etc.
In any case, here are some tips and tricks that I use quite often when it comes to plugin development.
Inserting Scripts and Styles into Backend Pages
This is the method I use to quickly setup a backend page and insert the scripts:
// Call my_class::my_class() on WordPress init hook add_action("init", create_function('', 'new my_class;')); class my_class { /** * Registers styles and loads my_class::admin_menu() into WordPress' admin_menu hook * * @uses wp_register_style() * @uses wp_register_script() * @uses add_action() */ function my_class() { // Define path define('WPP_URL', WP_PLUGIN_URL . '/wp-properties'); // Register styles ands cripts early on so they can be loaded later on specific pages wp_register_style('jquery-fancybox-css', WPP_URL. '/third-party/fancybox/jquery.fancybox-1.3.1.css' ); wp_register_script('jquery-fancybox', WPP_URL. '/third-party/fancybox/jquery.fancybox-1.3.1.pack.js', array('jquery'), '1.7.3' ); // Call my_class::admin_menu() function; add_action('admin_menu', array('my_class', 'admin_menu')); } /** * Creates a page called "Slideshow" under the gallery and loads scripts and styles. * * add_submenu_page() returns a string which is used by WP as a "slug" for the page we added. * We add an action to "'admin_print_scripts-' . $slideshow_page", which is only called on our slideshow page. * * Usually I see developers calling another function that will enqueue scripts and styles, but * I prefer to keep the function count down whenever possible, and choose to use create_function() * to return wp_enqueue_script() and wp_enqueue_style() into the header of admin_print_scripts-' . $slideshow_page * * @uses add_submenu_page() * @uses add_action() */ function admin_menu() { // Add submenu page under "Media Galllery" $slideshow_page = add_submenu_page( 'upload.php', "Slideshow", "Slideshow", 10, 'slideshow', array('wpp_slideshow', 'page' )); // Load FancyBox JS slideshow page add_action('admin_print_scripts-' . $slideshow_page, create_function('', " wp_enqueue_script('jquery-fancybox'); wp_enqueue_script( 'jquery-ui-sortable'); ")); // Load FancyBox CSS slideshow page add_action('admin_print_styles-' . $slideshow_page, create_function('', " wp_enqueue_style('jquery-fancybox-css'); ")); } }I realize create_function() is slower than using real functions, I haven’t done any benchmarking to see how much slower, but I have a feeling that when used in this manner the performance degradation is nominal.