Tabbed Meta Box in WordPress

I’ve updated the code to use the core WordPress UI. You can find it here.

I recently worked on a project that required me to add a custom meta box with tabs. In the spirit of GPL, I would like to share my code. The sample plugin checks the admin theme used by the user and enqueues the appropriate stylesheet to match the admin look and feel. The meta box looks and works fine with JavaScript disabled.

Download: Tabbed Meta Box in WordPress

Blue Theme with JavaScript Enabled

Fresh Theme with JavaScript Enabled

Meta Box in the side with JavaScipt Enabled

JavaScript Disabled

Disable Admin Bar and Hide Preferences

Before you consider removing the admin bar, you should read this.

WordPress 3.1 was released earlier today and one of the key features is the admin bar, which allows you to get to your most-used dashboard pages with a single click. However, the admin bar is not for everyone and you can easily hide it by visiting your profile page.

You can also disable the admin bar for all users on your site by adding the following code to a plugin or the functions file of your theme:

add_filter( 'show_admin_bar', '__return_false' );

This will disable the admin bar for all users on your site but they will still see the admin bar preferences in their profile. You can hide the admin bar preferences using:

add_action( 'admin_print_scripts-profile.php', 'hide_admin_bar_prefs' );
function hide_admin_bar_prefs() { ?>
<style type="text/css">
	.show-admin-bar { display: none; }
</style>
<?php
}

Note: If you are using this code to only hide the preferences without disabling the admin bar, your users can still change the values by manually editing the hidden variables before the page is submitted.

How to Create a WordPress Dashboard Widget

This post is the first in a series that was inspired by my WordPress Chicago presentation titled Beginner’s Plugin Development.

A new Dashboard Widgets API was introduced in WordPress 2.7, which made it easy for developers to create their own widgets for the administration dashboard. Dashboard Widgets are written in PHP and it’s easy to get started if you are familiar with the WordPress Plugin API.

You can add a new widget to the dashboard using the WordPress core function wp_add_dashboard_widget( widget_id, widget_name, callback, control_callback ).

  • widget_id: Unique id for your widget. This will be the css class and the array key for the widgets.
  • widget_name: Name for your widget that will be displayed as the title.
  • callback: The function that will display the content of the widget.
  • control_callback: The function that will display the form elements and handle the form submission. This parameter is optional.

In this post, I’ll create a simple dashboard widget that will display some text:

Let’s create a function that will display the contents of our custom dashboard widget:

function devmind_dashboard_widget() {
	echo '<p><a href="http://wordpress.org/">WordPress</a> is a state-of-the-art publishing platform with a focus on aesthetics, web standards, and usability. WordPress is both free and priceless at the same time.</p>';
}

We need to create another function that will call wp_add_dashboard_widget and register our custom dashboard widget:

function devmind_add_dashboard_widget() {
	wp_add_dashboard_widget( 'devmind-custom-widget', 'About WordPress', 'devmind_dashboard_widget' );
}

Now, we need to hook into dashboard setup process and tell WordPress to load our widget. We can do this by hooking into wp_dashboard_setup and adding our custom widget.

add_action( 'wp_dashboard_setup', 'devmind_add_dashboard_widget' );

You can add this code to your theme’s functions.php file or download it as a plugin here.

Prevent Update Checks for a WordPress Plugin

Earlier today, Jason Gegere asked me how to prevent WordPress from checking for updates on a plugin. This may be helpful if you are running a custom version of a plugin from the repository and you don’t want to accidently update the plugin and lose the customizations, or if you are running a private plugin and don’t want WordPress to check for updates from the plugin repository. WordPress doesn’t know if your plugin is in the WordPress plugins repository and it will request the update check on all installed plugins.

WordPress checks for updates using wp_update_plugins() in wp-includes/update.php but there are no hooks in this function which could be used to exclude your plugin from the update check. However, you can hook into http_request_args and remove your plugin from the query arguments passed in the http request. You can add the following code to your plugin to prevent WordPress from checking for updates for your plugin:

add_filter( 'http_request_args', 'dm_prevent_update_check', 10, 2 );
function dm_prevent_update_check( $r, $url ) {
	if ( 0 === strpos( $url, 'http://api.wordpress.org/plugins/update-check/' ) ) {
		$my_plugin = plugin_basename( __FILE__ );
		$plugins = unserialize( $r['body']['plugins'] );
		unset( $plugins->plugins[$my_plugin] );
		unset( $plugins->active[array_search( $my_plugin, $plugins->active )] );
		$r['body']['plugins'] = serialize( $plugins );
	}
	return $r;
}

How to Tweak the WordPress Generator Meta Tag

WordPress adds a generator meta tag to the head element of every page that includes the version of WordPress you are using. Many people have debated the need for this and recommend removing it especially if you haven’t been upgrading WordPress regularly. A crawler can easily scan this info and look for sites that are running an older version of WordPress with a known exploit. You can add the following code to your theme’s function.php file or in a plugin.

Tweak the WordPress Generator Meta Tag

The following code will remove the version number from the meta tag:

function devmnd_tweak_generator( $generator ) {
        return str_replace( ' ' . get_bloginfo( 'version' ), '', $generator );
}
add_filter( 'the_generator', 'devmnd_tweak_generator' );

Before: <meta name="generator" content="WordPress 3.0-RC1-15112" />
After: <meta name="generator" content="WordPress" />

You can even add your own custom version number in the generator meta tag:

return str_replace( get_bloginfo( 'version' ), '4.0', $generator );

Remove the WordPress Generator Meta Tag:

if ( function_exists( 'wp_generator' ) ) {
        remove_action( 'wp_head', 'wp_generator' );
}