Get an excerpt for a post outside of the loop

The WordPress get_the_excerpt() function does not behave well outside of a loop.

This will do so nicely:

/**
 * Get the excerpt for a post for use outside of the loop.
 *
 * @param int|WP_Post $post ID or WP_Post object.
 * @param int         Optional excerpt length.
 * @return string Excerpt.
 */
function prefix_get_the_excerpt( $post, $excerpt_length = null ) {

	$excerpt = get_post_field( 'post_excerpt', $post );

	if ( '' === $excerpt ) {

		$content = get_post_field( 'post_content', $post );

		// An empty string will make wp_trim_excerpt do stuff we do not want.
		if ( '' !== $content ) {

			$excerpt = strip_shortcodes( $content );

			/** This filter is documented in wp-includes/post-template.php */
			$excerpt = apply_filters( 'the_content', $excerpt );
			$excerpt = str_replace( ']]>', ']]>', $excerpt );

			if ( ! $excerpt_length ) {
				/** This filter is documented in wp-includes/formatting.php */
				$excerpt_length = apply_filters( 'excerpt_length', 55 );
			}

			/** This filter is documented in wp-includes/formatting.php */
			$excerpt_more = apply_filters( 'excerpt_more', ' ' . '[…]' );

			$excerpt = wp_trim_words( $excerpt, $excerpt_length, $excerpt_more );
		}
	}

	return apply_filters( 'the_excerpt', $excerpt );
}

/**
 * The excerpt for a post for use outside of the loop.
 *
 * @param int|WP_Post $post ID or WP_Post object.
 * @param int         Optional excerpt length.
 */
function prefix_the_excerpt( $post, $excerpt_length = null ) {

	echo wp_kses_post( prefix_get_the_excerpt( $post, $excerpt_length ) );
}

Set “Envelope-From” header for wp_mail()

WordPress emails were not being sent out by the server. But why?
In this case the site is hosted by Goneo.
As it appears Goneo requires:

  1. Emails to be sent from an address registered with them
  2. The ‘Envelope-From’ email header set to that address

It’s not a bug, it’s a feature!

Set the thing…

WordPress sends its emails via the wp_mail() function. In turn, this function uses the PHPMailer library to generate and send the actual email. Just before sending the mail, wp_mail() calls the helpful phpmailer_init action which gives us access to the PHPMailer instance. This is where we can use the setfrom() method to set the Envelope From header:

add_action( 'phpmailer_init', 'prefix_add_phpmailer_setfrom' );

/**
 * Add setFrom for hosts that insist on making life hard.
 *
 * @param array $phpmailer The PHPMailer instance, passed by reference.
 */
function prefix_add_phpmailer_setfrom( $phpmailer ) {

	$phpmailer->setFrom(
		get_option( 'admin_email' ), // From email address.
		'WordPress' // From name.
	);
}

Rejoice!

Remove all terms from a post

If you want to remove all taxonomy terms related to a post, wp_remove_object_terms() seems like the way to go. That function however expects the IDs of the terms to remove, there is no way to tell it to remove all terms at once without knowing the IDs beforehand.

Passing an empty $terms array or false or null to wp_set_object_terms() results in removing all terms associated to the post.

Fun with WordPress PHP Coding Standards in Sublime Text

Writing WordPress plugins and themes is my kind of fun, really. Adhering to the WordPress PHP Coding Standards while doing that is not only even more fun but also helps “…maintain a consistent style so the code can become clean and easy to read at a glance”.

Here is how I managed to get automated tests against the WordPress coding standards up and running in Sublime Text on a Mac using Homebrew and Git. Continue reading “Fun with WordPress PHP Coding Standards in Sublime Text”