JiveDig
2/13/2017 - 6:06 PM

WordPress shortcode to Genesis author boxes by role or user ID(s)

WordPress shortcode to Genesis author boxes by role or user ID(s)

/**
 * Shortcode to get author box(es) by user IDs or roles.
 * Originally taken from genesis_author_box()
 *
 * [author_box]                                      will display the author box of the current post author.
 * [author_box users="12,4,20"]                      will display author boxes of users 12, 4, and 20.
 * [author_box users="12,4,20"]                      will display author boxes of users 12, 4, and 20.
 * [author_box roles="author"]                       will display author boxes of all authors.
 * [author_box roles="author, editor"]               will display author boxes of all authors and contributors.
 * [author_box roles="author, editor" exclude="24"]  will display author boxes of all authors and contributors, excluding user 24.
 *
 * @version  3.0.0
 *
 * @author   Mike Hemberger <mike@bizbudding.com>
 *
 * @link     https://gist.github.com/JiveDig/27131779b244cc1668ef4bded85d1c80
 *
 * @param    string  $users    Comma-separated list of user IDs.
 * @param    string  $roles    Comma-separated list of user roles.
 * @param    string  $orderby  Orderby to be used in WP_User_Query. Accepts 'rand' as well.
 *
 * @return   string HTML       Author boxes.
 */
add_shortcode( 'author_box', function( $atts ) {

	// Shortcode attributes.
	$atts = shortcode_atts( array(
		'users'    => '',
		'roles'    => '',
		'exclude'  => '',
		'orderby'  => 'meta_value',
		'meta_key' => 'first_name',
	), $atts, 'author_box' );

	// Sanitize attributes.
	$atts = array(
		'users'    => $atts['users'] ? array_map( 'trim', explode( ',', $atts['users'] ) ) : '',
		'roles'    => $atts['roles'] ? array_map( 'trim', explode( ',', $atts['roles'] ) ) : '',
		'exclude'  => $atts['exclude'] ? array_map( 'trim', explode( ',', $atts['exclude'] ) ) : '',
		'orderby'  => sanitize_key( $atts['orderby'] ),
		'meta_key' => sanitize_key( $atts['meta_key'] ),
	);

	// Start args.
	$args = array(
		'fields' => 'all',
	);

	// Maybe get the author ID.
	if ( empty( $atts['users'] ) && empty( $atts['roles'] ) ) {
		$args['users'] = get_post_field( 'post_author', get_the_ID() );
	}
	// Maybe set IDs.
	if ( ! empty( $atts['users'] ) ) {
		$args['include'] = $atts['users'];
	}
	// Maybe set roles.
	if ( ! empty( $atts['roles'] ) ) {
		$args['role__in'] = $atts['roles'];
	}
	// Maybe set exclude.
	if ( ! empty( $atts['exclude'] ) ) {
		$args['exclude'] = $atts['exclude'];
	}
	// Maybe set orderby.
	if ( ! empty( $atts['orderby'] ) ) {
		$args['orderby'] = $atts['orderby'];
		if ( 'meta_value' === $atts['orderby'] ) {
			if ( ! empty( $atts['meta_key'] ) ) {
				$args['meta_key'] = $atts['meta_key'];
			}
		}
	}

	// Get users.
	$users = get_users( $args );

	if ( ! $users ) {
		return '';
	}

	// Function to remove 'About' from the author box title.
	$author_title_function = function( $title, $context, $user_id ) {
		$title = str_replace( 'About', '', $title );
		$title = trim( $title );
		return $title;
	};

	$html = '';

	foreach ( $users as $user ) {

		$post_count = count_user_posts( $user->ID, 'post' );

		if ( ! $post_count ) {
			continue;
		}

		// Get the author box.
		add_filter( 'genesis_author_box_title', $author_title_function, 10, 3 );
		$html .= genesis_get_author_box_by_user( $user->ID );
		remove_filter( 'genesis_author_box_title', $author_title_function, 10, 3 );
	}

	return $html;
});


// Allow rand ordering of users.
add_action( 'pre_user_query', function( $query ) {
	if ( 'rand' !== $query->query_vars['orderby'] ) {
		return;
	}
	$query->query_orderby = str_replace( 'user_login', 'RAND()', $query->query_orderby );
});