post

How to Get All Users of a Specific Role

WordPress Users
Recently, I needed to get all users of a certain role to modify a Comments Widget to remove those. Since WordPress 3.1, WordPress has made this quite easy to do now.

function wps_get_users_of_role( $role ) {
	$wp_user_search = new WP_User_Query( array( 'role' => $role ) );
	return $wp_user_search->get_results();
}

This will give you an array that contains the following in an object:

<pre>stdClass Object
        (
            [ID] => 1
            [user_login] => username
            [user_pass] => serialized PASSWORD
            [user_nicename] => mynicename
            [user_email] => my@email.com
            [user_url] => http://mydomain.com
            [user_registered] => GMT Time Stamp
            [user_activation_key] => Something funky!
            [user_status] => 0
            [display_name] => My Display Name
        )</pre>

You can even set a predefined array and get only necessary information.

$args = array();
$args[0] = 'user_login';
$args[1] = 'user_nicename';
$args[2] = 'user_email';
$args[3] = 'user_url';
$wp_user_search = new WP_User_Query( array( 'role' => 'editor', 'fields' => $args ) );
$editors = $wp_user_search->get_results();

Here’s the result:

<pre>stdClass Object
        (
            [user_login] => wpsmith
            [user_nicename] => wpsmith
            [user_email] => travis@wpsmith.net
            [user_url] =>
         )</pre>
post

How to Add a Body Class to the WordPress Activation Page for Targeted CSS Styling

So the activation page (for a multisite network) can be a bit difficult to style for a full-width site that has a background on the body and a background on the #content-sidebar-wrap. Since this section is not part of Genesis (or any theme/framework for that matter) and is part of WordPress (thus no real hooks within the page), you need to add a body class to be able to target style.

add_action( 'activate_header' , 'wps_add_activate_class' );
/*
 * Call body_class filter on activate_header action hook that only appears on wp-activate.php
 * @author Travis Smith
 */
function wps_add_activate_class() {
	add_filter( 'body_class' , 'wps_activate_class' );
}

/*
 * Add specific CSS class by filter
 * @author Travis Smith
 */
function wps_activate_class( $classes ) {

	// add 'class-name' to the $classes array
	$classes[] = 'activation-page';

	// return the $classes array
	return $classes;
}
post

Speaking at WordCamp Atlanta

Speaking at WordCamp AtlantaSo, I have been rather lazy announcing this, but I am speaking at WordCamp Atlanta coming up on February 4th at 11am on Professional Rapid Web Development Using the Genesis Framework.

During this seminar I plan to do the following:

  1. Introduce Genesis 1.8 and the new built-in Genesis Admin Class
  2. Introduce/Discuss 3 Other Classes including an Admin Builder Class (by Nick Croft), the Custom Metabox Class (by Jared Atchinson, Bill Erickson, & Andrew Norcross), and the Plugin Activation Class (by Thomas Griffin and Gary Jones) for rapid development
  3. Introduce my Genesis Starter Theme, WPS Starter Flex, which includes nearly everything a Genesis developer will need
  4. Discuss the Core Functionality Plugin approach to building websites
  5. Discuss the various resources available to a Genesis developer including the very active community

Beyond all these things, StudioPress has graciously donated some WordPress swag to give away during the presentation including 1 ProPlus Package & 3 Theme Packages (e.g., AgentPress).

Did I miss anything? Is there anything else that I should include? Please let me know!! I’ll see you there!

post

How to Add a Custom Screen Icon for Genesis Admin Pages

In a previous post I discussed how to create a child theme admin page, and talked about a few of the new features of Genesis 1.8, . However, one thing I didn’t discuss in the creation of the new admin pages is setting a custom screen icon.

While this may or may not make Genesis core, it is a simple addition. So in your __construct() method with $page_ops, enter a screen icon of ‘custom’.

/**
 * Create an admin menu item and settings page.
 *
 * @since 1.0.0
 */
function __construct() {

	// Specify a unique page ID.
	$page_id = 'child';

	// Set it as a child to genesis, and define the menu and page titles
	$menu_ops = array(
		'submenu' => array(
			'parent_slug' => 'genesis',
			'page_title'  => 'Genesis - WPS Starter Settings',
			'menu_title'  => 'WPS Starter Settings',
			'capability' => 'manage_options',
		)
	);

	// Set up page options. These are optional, so only uncomment if you want to change the defaults
	$page_ops = array(
		'screen_icon'       => 'custom', // see $this->screen_icon() below
	);		

	// Give it a unique settings field.
	// You'll access them from genesis_get_option( 'option_name', CHILD_SETTINGS_FIELD );
	$settings_field = CHILD_SETTINGS_FIELD;

	// Set the default values
	$default_settings = array(
		'phone'   => '',
		'address' => '',
	);

	// Create the Admin Page
	$this->create( $page_id, $menu_ops, $page_ops, $settings_field, $default_settings );

	// Initialize the Sanitization Filter
	add_action( 'genesis_settings_sanitizer_init', array( $this, 'sanitization_filters' ) );
}

Next you will need a method to output the necessary CSS to display your icon.

/**
 * Create an admin menu item and settings page.
 *
 * @since 1.0.0
 */
function __construct() {

	// Specify a unique page ID.
	$page_id = 'child';

	// Set it as a child to genesis, and define the menu and page titles
	$menu_ops = array(
		'submenu' => array(
			'parent_slug' => 'genesis',
			'page_title'  => 'Genesis - WPS Starter Settings',
			'menu_title'  => 'WPS Starter Settings',
			'capability' => 'manage_options',
		)
	);

	// Set up page options. These are optional, so only uncomment if you want to change the defaults
	$page_ops = array(
		'screen_icon'       => 'custom', // see $this->screen_icon() below
	);		

	// Give it a unique settings field.
	// You'll access them from genesis_get_option( 'option_name', CHILD_SETTINGS_FIELD );
	$settings_field = CHILD_SETTINGS_FIELD;

	// Set the default values
	$default_settings = array(
		'phone'   => '',
		'address' => '',
	);

	// Create the Admin Page
	$this->create( $page_id, $menu_ops, $page_ops, $settings_field, $default_settings );

	// Initialize the Sanitization Filter
	add_action( 'genesis_settings_sanitizer_init', array( $this, 'sanitization_filters' ) );

	// Add custom screen icon
	add_action( 'admin_head' , array( $this, 'screen_icon' ) );
}

Now you will need to create the method within the class extension.


/**
 * Custom Admin screen icon
 *
 * See /lib/classes/sanitization.php for all available filters.
 *
 * @since 1.0.0
 */
public function screen_icon() { ?>
<style>
	#icon-custom { background-image: url('<?php echo WPS_ADMIN_IMAGES . '/staff_32x32.png'; ?>'); background-repeat: no-repeat; }
</style>
<?php
}
post

How to Add an Avatar To WordPress Defaults, Remove Default Avatars, & Set Your Default

With WordPress and Genesis, Avatars are used as identification (like the Twitter icon), and with Gravatar it is in the comment area and the User Profile Widget.

Default Avatars

Click for larger image

WordPress allows the user to set a default or generated Avatar, if the commentator doesn’t have one. However, I find these options rather ugly or not fitting to the theme most of the time.

Add an Avatar to WordPress Defaults

So I’d like to show you how to add an avatar to the WordPress defaults.

// Add Custom Avatar (Discussion Settings)
add_filter( 'avatar_defaults' , 'wps_new_avatar' );
function wps_new_avatar( $avatar_defaults ){
	$new_avatar = get_stylesheet_directory_uri() . '/images/genesis-48x48.png';
	$avatar_defaults[$new_avatar] = "Genesis";

	return $avatar_defaults;
}
Add New Avatar

Click for larger image

Remove Default Avatars from WordPress Defaults

For those of us who don’t care for some of the WordPress defaults, within that function you can easily remove them at your will:

// Add Custom Avatar (Discussion Settings)
add_filter( 'avatar_defaults' , 'wps_new_avatar' );
function wps_new_avatar( $avatar_defaults ){
	// Get Avatar from child theme images folder
	$new_avatar = get_stylesheet_directory_uri() . '/images/genesis-48x48.png';
	$avatar_defaults[$new_avatar] = "Genesis";

	// Remove default avatars
	unset ( $avatar_defaults['mystery'] );
	//unset ( $avatar_defaults['blank'] );
	//unset ( $avatar_defaults['gravatar_default'] );
	//unset ( $avatar_defaults['identicon'] );
	//unset ( $avatar_defaults['wavatar'] );
	//unset ( $avatar_defaults['monsterid'] );
	//unset ( $avatar_defaults['retro'] );

	return $avatar_defaults;
}
Remove Avatars

Click for larger image

Set Your Default

Now, WordPress gladly will set your default avatar to the well-known, popular Mystery Man. However, instead of navigating to Settings > Discussion, you can easily set your default as well.

// Set new avatar to be default
add_action ( 'admin_init' , 'wps_avatar_default');
function wps_avatar_default () {
	$default = get_option('avatar_default');
	if ( ( empty( $default ) ) || ( $default == 'mystery' ) )
		$default = get_stylesheet_directory_uri() . '/images/genesis-48x48.png';
	update_option ( 'avatar_default' , $default );
}

The known downside of this is that now, Mystery Man can never be chosen so long as this code is active (so it would work well to remove Mystery Man (unset ( $avatar_defaults['mystery'] );) above. This way you can easily control the avatars on your site.

Final Avatar

Click for larger image

post

Developing a Custom Post Type and a Custom Taxonomy in Genesis with Custom Pages, Part 5

Now, in Developing a Custom Post Type and a Custom Taxonomy in Genesis with Custom Pages, Part 1, we have the registration of the custom post type, custom taxonomy and the addition of the metaboxes. In Part 2, we discussed the Single Page Template for the custom post type. In Part 3, we discussed the Taxonomy Template for the custom taxonomy. In Part 4, we discussed the a page template for the custom post type.

Now this tutorial will discuss Creating Columns in the Edit Custom Post Page. Justin Tadlock has an excellent post on this and is a must read: Custom columns for custom post types. For those, however, who don’t like coding, there is a good plugin that will do this work for you. Pippin’s Post Type Column Editor from Code Canyon (affil link) is only $12 and worth the money, if you just lovehate coding.

Column Editor

Before, I move forward to how to do this via php and functions.php, let me highlight Pippin’s plugin (so if you care about the code skip this and the pictures). With post Type Column Editor you can easily customize the dashboard columns for all your post types. So if you wanted to add thumbnails and excerpts to the columns of posts and/or pages (if pages supports excerpts), then you can easily add it with a few clicks. This plugin gives you a really easy to use way to modify and manage the table columns for your post types. You can display post type entry titles, categories, tags, excerpts, authors, custom meta fields, thumbnails, and custom taxonomies. You can customize the columns for each built-in and custom post type separately with a straight forward drag-and-drop interface.

So when you first register a custom post type, here is what you’ll see:

Custom Post Type with No Columns

Initial Custom Post Type

Then when you use the Post Type Column Editor, you see your various options.

Post Type Column Editor Options

Post Type Column Editor Options

For a demonstration check out this Screenr:

This obviously works well with his Easy Content Types plugin which outputs PHP code for you to embed into your plugin and/or theme.

Sometimes it becomes necessary to display more than the usual Title and Date of our post types. In this case, we want to add a new column to the edit post page to allows us to easily see which axle size our disc brakes belong to. There are some great examples of how to do that but below is an example of how we can add our axle size to the page.

First we must add the column. Use the code below and add it to our wps-admin-functions.php file.

// Add Columns for Disc Brakes Edit Posts Page
add_filter( 'manage_edit-rdwd_discbrakes_columns', 'wps_discbrakes_columns' ) ;
function wps_discbrakes_columns($column) {
	$column = array(
		'cb' => '<input type="checkbox" />',
		'title' => __( 'Disc Brakes' ),
		'axlesizes' => __( 'Axle Sizes' ),
		'date' => __( 'Date' )
	);
	return $column;
}

If we save this file and upload it, we will now see a new column called Axle Sizes. This is great but there is no content in this column. Now we need to go get the content and display here. Use this code below to do just that.

// Gets the Taxonomy Terms and retunrs them on the Disc Brakes Edit Posts Page
add_action( 'manage_wps_discbrakes_posts_custom_column', 'manage_wps_discbrakes_columns', 10, 2 );
function manage_wps_discbrakes_columns( $column, $post_id ) {
	global $post;
	switch( $column ) {
		case 'axlesizes' :
			$terms = get_the_terms( $post_id, 'wps_axlesizes' );
			if ( !empty( $terms ) ) {
				$out = array();
				foreach ( $terms as $term ) {
					$out[] = sprintf( '<a href="%s">%s</a>', esc_url( add_query_arg( array( 'post_type' => $post->post_type, 'wps_axlesizes' => $term->slug ), 'edit.php' ) ), esc_html( sanitize_term_field( 'name', $term->name, $term->term_id, 'wps_axlesizes', 'display' ) ) );
				}

				echo join( ', ', $out );
			} else {
				_e( 'No Axle Sizes' );
			}               

			break;

		default :
			break;
	}
}

The code above allows us to select one of the axle sizes and it will only display the disc brakes for that size.
Custom Column for Custom Post Type

post

How to Determine Child/Ancestor with is_child() and is_ancestor()

I found this great piece of code at Codebyte.
Put the following in your functions.php file:

// Check if page is direct child
function wps_is_child( $page_id ) {
    global $post;
    if( is_page() && ( $post->post_parent == $page_id ) ) {
       return true;
    } else {
       return false;
    }
}
// Check if page is an ancestor
function wps_is_ancestor( $post_id ) {
    global $wp_query;
    $ancestors = $wp_query->post->ancestors;
    if ( in_array( $post_id, $ancestors ) ) {
        return true;
    } else {
        return false;
    }
}