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!

How To: Load User Info Using the Admin Email in WordPress

Today’s handy WordPress code snippet is a simple way to retrieve user data based on the administrator email in WordPress. The email account I am referring to is the one listed under Settings > General and is the main admin email for your website.

$admin_email = get_option('admin_email');
$admin_user_id = get_user_id_from_string($admin_email);
$user_info = get_userdata($admin_user_id);

The above code example first loads the admin email from the WordPress options. Next it determines that user’s ID based off of their email address using the get_user_id_from_string() function. Finally we use get_userdata() to load all user data for that user ID.

Currently the get_user_id_from_string() function is only available in WordPress MU. I have confirmed however that this function does exist in WordPress 3.0. That means after the merge this function will be available to all sites running WordPress.

We can also use the get_user_by_email() function included since WordPress 2.5 to accomplish the same task. Thanks to Mo Jangda for pointing that out in the comments. Below is an example using this method:

$admin_email = get_option('admin_email');
$user_info = get_user_by_email($admin_email);

This is actually a more efficient method as we don’t need to call the function to retrieve the user ID first. In the world of WordPress you learn something everyday. Thanks Mo Jangda!

Custom Post Type UI Plugin for WordPress

A few days ago I released a new plugin for WordPress called Custom Post Type UI. This plugin allows you to easily create custom post types in WordPress without writing a single line of code!

Using custom post types is the future of WordPress and really opens the software up to be a full fledged Content Management System (CMS). For example if you are building a website for a car dealer you could create a post type for Cars. A business directory could have a post type for Businesses. A movie database could have a post type for Movies, Actors, and Directors. I think you get the idea. Post types are a very powerful feature in WordPress and my new plugin helps you take advantage of the power!

Below are some screenshots showing the plugin in action:


Easily create new custom post types


New custom post type is automatically added to your admin menu


Easily view and edit existing custom post types

This plugin is a little different than my other plugins because it is primary built for WordPress 3.0, which hasn’t been officially released yet. You can however install WordPress 3.0-alpha very easily using the WordPress Beta Tester plugin created by Peter Westwood.

All custom post types are saved as a single WordPress option in the database keeping this plugin very lightweight. A future version will include creating custom taxonomies for custom post types. I hope this plugin will be rolled into the Core WordPress software at some point, but until then the Custom Post Type UI does the trick!