#nyc12: I Want to Learn WordPress

This morning, I gave the first of three CMA NYC sessions I’m leading this week:

So, you’ve heard of WordPress before, possibly used it for your personal portfolio, and want to learn everything there is to know about it. Well, everything we can cover in a hour at least :) Join Daniel Bachhuber, code wrangler for Automattic’s WordPress.com VIP, as we cover the WordPress interface, key concepts like themes, plugins, PHP and MySQL, and how to choose a good web host and design for your site. This session will be geared towards those with limited familiarity who want to learn more.

It was a quick introduction to the WordPress project, key terminology you’d hear, and then a tour through the WordPress admin. The room was packed with maybe 30-40 people which was sweet. Tomorrow is “Hacking WordPress in the Newsroom” and Tuesday is “Making the Switch to WordPress.”

Co-Authors Plus v2.6.2: Enhancements and bug fixes

Co-Authors Plus makes it easy to add multiple bylines to a given post, and has full support for custom post types. Out just a moment ago, v2.6.2 has the following improvements:

  • AJAX user search matches against first name, last name, and nickname fields too, in addition to display name, user login, and email address.
  • Comment moderation and approved notifications are properly sent to all co-authors with the correct capabilities.
  • Filter required capability for user to be returned in an AJAX search with ‘coauthors_edit_author_cap’. This defaults to ‘edit_posts’
  • Filter out administrators and other non-authors from AJAX search with ‘coauthors_edit_ignored_authors’
  • Automatically adds co-authors to Edit Flow’s story budget and calendar views.
  • Bug fix: Don’t set post_author value to current user when quick editing a post. This doesn’t appear in the UI anywhere, but added the post to the current user’s list of posts. See related forum conversation.
  • Bug fix: Properly cc other co-authors on new comment email notifications
  • Bug fix: If a user has already been added as an author to a post, don’t show them in the AJAX search again.
  • Bug fix: Allow output constants to be defined in a theme’s functions.php file and include filters you can use instead.

Please post any questions, bug reports, feature requests, etc. in the WordPress.org forums. If you want to contribute code, I’m eyeballing co-author management in Quick Edit and guest author functionality for v2.7.

For WordPress.com VIPs, this update has already been deployed to the shared plugins repo.

#wcphx: Five tenets to mastering WordPress development

Enlightenment is knowing what your code is doing and why. Thankfully, instead of having to depend on your inner calm, there are a number of tools and strategies you can use to better see what’s going on. We’ll survey a range of topics you should explore to turn your frustration into bliss.

Feeling better already? In this session, we’ll touch on everything from debugging to best practices to coding standards to version control to performance and optimization. You’ll hear the insights WordPress.com VIP shares every day.

Session notes are below the slides.

Continue reading

Pre-flighting your WXR files

Status

Thanks to a bunch of hard work by Thorsten and others, the open source CLI scripts we have for exporting and importing WordPress sites are getting better and better.

When exporting, the script gives you a summary of what will be included in your export file.

For importing, it would be nice if the script pre-flighted your data and told you how many total posts were to be imported, whether new tags were going to be created, identified the custom post types in your export file that were to be rejected by the site, etc.

Also, we should figure out a better way to make sure all of our internal scripts are regularly open sourced (and synced to most recent versions).

Include posts by matching authors in your search results

Out of the box, WordPress’ search isn’t that great. It only returns posts based on a LIKE query against the post title or post content. Often, you’ll want it to match against other data associated with your post, possibly including the author, tags, post meta fields, etc. These queries can get complex to perform on the fly, however.

The following code snippet allows you to include posts by matching authors in your search results. We’re modifying any search queries to also include all posts whose author display name or user login matches the query. You can change this to be the author’s first name, last name, or other fields.

Updated May 17, 2012: Improved the user search so we don’t query for all users and search with PHP; instead, search against the users table.

/**
 * Include posts from authors in the search results where
 * either their display name or user login matches the query string
 *
 * @author danielbachhuber
 */
add_filter( 'posts_search', 'db_filter_authors_search' );
function db_filter_authors_search( $posts_search ) {

	// Don't modify the query at all if we're not on the search template
	// or if the LIKE is empty
	if ( !is_search() || empty( $posts_search ) )
		return $posts_search;

	global $wpdb;
	// Get all of the users of the blog and see if the search query matches either
	// the display name or the user login
	add_filter( 'pre_user_query', 'db_filter_user_query' );
	$search = sanitize_text_field( get_query_var( 's' ) );
	$args = array(
		'count_total' => false,
		'search' => sprintf( '*%s*', $search ),
		'search_fields' => array(
			'display_name',
			'user_login',
		),
		'fields' => 'ID',
	);
	$matching_users = get_users( $args );
	remove_filter( 'pre_user_query', 'db_filter_user_query' );
	// Don't modify the query if there aren't any matching users
	if ( empty( $matching_users ) )
		return $posts_search;
	// Take a slightly different approach than core where we want all of the posts from these authors
	$posts_search = str_replace( ')))', ")) OR ( {$wpdb->posts}.post_author IN (" . implode( ',', array_map( 'absint', $matching_users ) ) . ")))", $posts_search );
	error_log( $posts_search );
	return $posts_search;
}
/**
 * Modify get_users() to search display_name instead of user_nicename
 */
function db_filter_user_query( &$user_query ) {

	if ( is_object( $user_query ) )
		$user_query->query_where = str_replace( "user_nicename LIKE", "display_name LIKE", $user_query->query_where );
	return $user_query;
}

Allowing authors to set co-authors

In the WordPress.org forums, whoaloic asks:

I have a site with multiple authors.
I would like to allow authors who create a post or a page to give rights to other authors.
For now, only administrator and editor can do that.
Is there a solution?

Yep, totally doable. By default, Co-Authors Plus defaults to ‘edit_others_posts’ as the required cap for changing co-authors. With the following code snippet in your theme’s functions.php file, you can make that cap ‘publish_posts’ instead (which authors and above usually have).

/**
 * Filter the Co-Authors Plus current_user_can_set_authors() function
 * so that users with 'publish_posts' can set Co-Authors
 *
 * @author danielbachhuber
 *
 * @see https://github.com/danielbachhuber/Co-Authors-Plus/issues/8
 * @see http://wordpress.org/support/topic/plugin-co-authors-plus-allow-authors-post-to-give-access-to-other-authors
 */
add_filter( 'coauthors_plus_edit_authors', 'db_filter_coauthors_edit_cap' );
function db_filter_coauthors_edit_cap( $cap_result ) {
	global $coauthors_plus;

	$post_type = $coauthors_plus->get_current_post_type();
	if( ! $post_type ) return false;
	
	$post_type_object = get_post_type_object( $post_type );
	return current_user_can( $post_type_object->cap->publish_posts );
}

In preparing this snippet, I also opened a couple of issues in Github:

New York Times releases code to help journalists collaborate on WordPress, other platforms

Aside

New York Times releases code to help journalists collaborate on WordPress, other platforms. Track changes within the WordPress editor. Code is available on Github; it would be awesome to see this support realtime collaborative editing too. (via Steve Myers)

Show biographies for co-authors at the end of your post

In the WordPress.org forums, doubleedesign says:

I want to add the authors’ biographies to the end of each post.

Awesome… it’s pretty simple to do. Conceptually, what we need to do is load our co-authors, and then loop through printing the relevant information for each one.

You’ll want to put the following code snippet within The Loop in any template you’d like the bios to appear.

/**
 * Show multiple Co-Author biography fields at the bottom of a single post 
 * This snippet should be placed within The Loop
 */
if ( class_exists( 'coauthors_plus' ) ) {
	// Get the Co-Authors for the post
	$co_authors = get_coauthors();
	// For each Co-Author, echo a wrapper div, their name, and their bio if they have one
	foreach ( $co_authors as $key => $co_author ) {
		$co_author_classes = array(
			'co-author-wrap',
			'co-author-number-' . ( $key + 1 ),
		);
		echo '<div class="' . implode( ' ', $co_author_classes ) . '">';
		echo '<h4 class="co-author-display-name">' . $co_author->display_name . '</h4>';
		// Only print the description if the description exists
		if ( $description = get_the_author_meta( 'description', $co_author->ID ) )
			echo '<p class="co-author-bio">' . $description . '</p>';
		echo '</div>';
	}
}

If you’d like other co-author details to appear as well, like their avatar for instance, you can modify the output within the foreach loop.