How To: Create a Network Only Plugin in WordPress

If you’ve ever created a custom plugin in WordPress, you should be familiar with the standard plugin header code. The information set in your plugin header is displayed under the Plugins admin menu in WordPress shown below:

wordpress-plugin-header-example

There is a lesser known piece of information that can be defined in your plugin header that tells WordPress to only show your plugin on the WordPress Multisite Network Plugins page. This will hide your plugin from being displayed and activated at the site level in WordPress Multisite. To only allow your plugin to show at the Network Plugins level, simply add this one line to your plugin header code:

Network: true

Easy, right? Let’s look at an example comparing a standard plugin header with a network only plugin header.

Standard Plugin Header:

/*
Plugin Name: My Awesome Plugin
Plugin URI: http://webdevstudios.com/support/wordpress-plugins/
Description: This is my extremely awesome WordPress plugin
Version: 1.0
Author: Brad Williams
Author URI: http://webdevstudios.com
License: GPLv2
*/

Network Only Plugin Header:

/*
Plugin Name: My Awesome Plugin
Plugin URI: http://webdevstudios.com/support/wordpress-plugins/
Description: This is my extremely awesome WordPress plugin
Version: 1.0
Author: Brad Williams
Author URI: http://webdevstudios.com
License: GPLv2
Network: True
*/

Using this method you can be sure your WordPress plugin will only be network activated in Multisite.

How To: Get the Current Logged in User ID in WordPress

When developing custom themes and plugins for WordPress there are times you will need to get the logged in user’s ID. There are a few different ways to accomplish this, but I’m going to show you the easiest method. To get the user’s ID you’ll use the get_current_user_id() function like so:

$current_user_id = get_current_user_id();
echo 'Your User ID is: ' .$current_user_id;

The get_current_user_id() function will return the currently logged in user’s ID, or 0 if a user is not logged in. Another popular method, which requires a bit more code, is the get_currentuserinfo() function. The method I described above actually uses the wp_get_current_user() function, which is a wrapper for get_currentuserinfo(). So using get_current_user_id() is just a faster method for retrieving the same data. This function was added in WordPress 3.0 as part of the Multisite code merge into WordPress.

To learn more about the get_current_user_id() function check out the Codex article or consult the WordPress core.

How To: Add and Remove Allowed File Types in WordPress

Recently I had a client request that we block BMP files from being an allowed file type for uploads in WordPress. As always WordPress provides a very simple way to set what file extensions are allowed to be uploaded. To do this we’ll use the upload_mimes action filter. Add the below code example to your theme’s functions.php file or a custom plugin:

add_filter( 'upload_mimes', 'brad_mime_types' );

function brad_mime_types( $mime_types ){

	//remove bmp from allowed types
    unset( $mime_types['bmp'] );
	
	//return the array of allowed types
    return $mime_types;
	
}

As you can see we use the add_filter() function to hook the upload_mimes filter. The filter passes a single variable, which is an array of all allowed file types in WordPress. To remove a type we simply use the unset() PHP function to remove the ‘bmp’ value from the array. The final step is to return the array of allowed file types back to WordPress.

That’s it! Using the above code BMP files are no longer allowed for upload in WordPress.

How To: Display Gravity Form Error Messages in a JavaScript Popup

Gravity Forms is an extremely popular contact form plugin for WordPress. One of the reasons Gravity Forms is so popular is the ease in which you can customize it. Much like WordPress, Gravity Forms features various action and filter hooks for developers to use to easily tweak how GF functions.

Working on a client site recently I needed to customize how error messages were displayed in Gravity Forms. By default GF will display an error message above the form if any field values aren’t validated (ie a required field is left empty). On this particular website I wanted the error message to display in a simple JavaScript popup, instead of on the page. Below is the code I used to do just that:

add_filter( 'gform_validation_message', 'sw_gf_validation_message', 10, 2 );

function sw_gf_validation_message( $validation_message ) {

	//display error JS popup
	add_action( 'wp_footer', 'sw_gf_js_error' );

}

function sw_gf_js_error() {
	?>
	<script type="text/javascript">
		alert( "Please fill out all required fields indicated by a *" );
	</script>
	<?php
}

As you can see I’m using the gform_validation_message filter to customize how the error message is processed. The PHP variable $validation_message stores the original error message GF was going to display. In this example I didn’t use the original error message, but you could easily pass that to your JS popup if needed.

This is a pretty simple example of customizing the Gravity Forms error message. Enjoy!

How To: Add A Link to the WordPress Multisite Network Admin Sites List

The other day I was working on a plugin for a client when I needed to add a link to the WordPress Multisite Network Admin Sites list. This is the list of sites in your WordPress Multisite network. The links I am referring to are the action links that appear when you hover over a site in the list as shown below.

WordPress Multisite Network Sites ListTo add a link, or modify any of the existing action links, we’re going to use the manage_sites_action_links action filter in WordPress. This filter will allow us to modify the action links before they are displayed on the screen. This means you can add, or remove, any links you want.

Let’s look at the code:

add_filter( 'manage_sites_action_links', 'my_plugin_network_list_action', null, 2 );

function my_plugin_network_list_action( $actions, $blog_id ) {

    $actions = array_merge( $actions, array(
	'custom_link' => '<a href="'. network_admin_url( 'sites.php' ).'">My Custom Link</a>'
    ));

    return $actions;

}

First we call the manage_sites_action_links filter hook which executes our custom function my_plugin_network_list_action(). Our function accepts two parameters: The $actions array which contains all action links and the $blog_id which stores the site ID of the site we are hovering in the list.

To add a link we are going to use the PHP function array_merge() to merge our link into the array of existing links. In this example I added a link named “My Custom Link” which links to the Network Admin sites list. The final step is to return the $actions variable. Simple as that!

For more awesome WordPress plugin goodies check out my new book: Professional WordPress Plugin Development

How To: Create Facebook Like Username URLs in WordPress

One of the great things about working with WordPress on a daily basis is I get to figure out how to do all sorts of fun things in WordPress! The latest challenge was figuring out how to setup Facebook like username URLs in WordPress. For example I wanted http://example.com/username to load the Author’s profile page. This was surprisingly much easier to accomplish than I assumed it would be. To do this visit Settings > Permalinks in the admin dashboard of WordPress and set your permalink custom structure to:

/%author%/%postname%/

That’s all there is to it! Now the user URL will be http://example.com/username just like in Facebook! To create a custom author page design simply edit (or create if it doesn’t exist) the author.php template file in your theme directory. Below is a screenshot of the custom permalink structure:

Facebook User URLs in WordPress

How To: Remove Default Profile Fields in WordPress

A few days ago I came across an interesting challenge in WordPress. I wanted to hide some of the default profile fields from being displayed to the users in WordPress. Specifically I wanted to hide the AIM, Yahoo IM, and Jabber / Google Talk fields. It took a bit of digging but I found the below function buried in the WordPress.org support forums. Just place the below code in your themes functions.php file to remove these fields:

<?php
add_filter('user_contactmethods','hide_profile_fields',10,1);

function hide_profile_fields( $contactmethods ) {
  unset($contactmethods['aim']);
  unset($contactmethods['jabber']);
  unset($contactmethods['yim']);
  return $contactmethods;
}
?>

That’s it! As you can see below the three fields are removed from the Profile page on the WordPress admin side. This makes it much less confusing for users since they don’t see fields that we aren’t using on the website.

How To: Hide an Admin Menu in WordPress

Have you ever needed to hide a specific admin menu from other users in WordPress? Maybe you want to hide the Plugins and Appearance menus to keep your users out of trouble. Just place the below code in your themes functions.php file to hide the Plugins menu from all users except for admin:

<?php
add_action('admin_head', 'hide_menus');

function hide_menus() {
	global $current_user;
	get_currentuserinfo();
	
	If($current_user->user_login != 'admin') {
		?>
		<style>
		   #menu-plugins{
				display:none;
			}
		</style>
		<?php
	}
}
?>

As another example lets say we want to hide the Links menu from all users that aren’t administrators in WordPress. The below code would do just that:

<?php
add_action('admin_head', 'hide_menus');

function hide_menus() {
	if ( !current_user_can('manage_options') ) {
		?>
		<style>
		   #menu-links{
				display:none;
			}
		</style>
		<?php
	}
}
?>

This is a pretty simple method of hiding menus in the WordPress admin dashboard. Enjoy!