Disable post revisions for all post types

WordPress has a handy post revision feature which saves earlier states of a post as you make edits. Not everyone wants or needs this feature, however, and it’s handy to be able to turn it off. Plugins exist for this, but I prefer code solutions that I can put in my themes or site-specific plugins. Here’s a simple method to disable post revisions without installing a plugin:

/**
 * Disable revisions for all post types.
 */
function my_disable_post_revisions() {
	foreach ( get_post_types() as $post_type ) {
		remove_post_type_support( $post_type, 'revisions' );
	}
}
add_action( 'init', 'my_disable_post_revisions', 999 );

If you’d like to disable post revisions for only specific set of built-in or custom post types instead of targeting all types, you can do that with an array:

/**
 * Disable revisions for all post types.
 */
function my_disable_post_revisions() {
	$types = array( 'post', 'my-custom-type' );
	foreach ( $types as $post_type ) {
		remove_post_type_support( $post_type, 'revisions' );
	}
}
add_action( 'init', 'my_disable_post_revisions', 999 );

The high priority, 999, means it’s almost certain to execute after any other code which adds revision support.

The ability to disable revisions is particularly helpful when moving a site from another system to WordPress. Frequent post editing and other operations may result in a large number of revisions saved in the database.

If you want to start saving revisions again, simply remove this code from your theme or plugin.

Mine your site search data for content ideas

In the past, you could see what search terms brought people to your site. You could comb through that data to discover how people found your site. Perhaps more importantly, you could discover what searched for and did not find. That data allowed you to identify ideas that were relevant to your existing content and served a need that wasn’t already being met on your site.

Unfortunately for you, the writer, that data is no longer easily available in Google Analytics. Google began obscuring search terms from referrer data because they wanted to protect their user’s privacy. Or, if you’re cynical, the data was too valuable to give away for free. (it’s totally available if you set up another product, Google Search Console.) All of the other major search engines followed suit, making it more challenging to mine this search data to improve your site.

Using your own site search

But activities on your own site are your own business, and Google Analytics’ Site Search makes it pretty easy for you to track what people are searching for on your site. Utilizing data about what people are searching for on your site, you can identify areas of strong interest where you aren’t writing enough, or haven’t written at all. And if you’ve set up Site Search tracking and aren’t seeing much search traffic, that may also be instructive. You might consider updating your site to make search a more prominent feature.

Site search tracking for WordPress with Google Analytics

With just a little setup, Google analytics can track keywords and phrases entered into your WordPress site’s search box. By default, your search results will show up as regular page views which look something like /?s=Search Term&submit=Search. Using GA’s Site Search Tracking feature, however, you can filter these terms into the Behavior > Site Search section of the reporting interface in order to gain deeper insight into what your visitors search for on your site.

Set it up

Screen Shot 2016-09-06 at 5.43.29 PMTo configure site search tracking for a WordPress site:

  1. Log in to Google Analytics and click on Admin.
  2. Select the appropriate Account, Property, and View for your site at the head of their respective columns in the Admin interface.
  3. In the View column, select View Settings.
  4. Enable the Site Search Tracking option with the toggle switch.
  5. Set the Site Search Parameter field to “s” which is the parameter WordPress uses for search terms.
  6. Optionally, check “Strip query parameters out of URL.” This will make all searches show up in analytics with the same URL (“/?s=Search Term&submit=Search” by default,) making it easier to track total search volume in your other reporting views.
  7. Click Save to finish!

Now search data will be parsed out into the Site Search interface for all future traffic.

Advanced use

If you have a custom search form which filters using default or custom taxonomies, you can get further insight into visitors’ search habits on your site using Site Search Categories. In the same interface, enable Site Search Categories and enter a comma separated list of any taxonomy identifiers which may appear in your search queries. These identifiers will be the query_var for the taxonomy. For a custom taxonomy this is likely the taxonomy name, unless you’ve specifically replaced it. For tags and Categories it will be “tag” and “category_name” respectively. Again, you can optionally check “Strip category parameters out of URL” to keep all of your searches tracking as a single URL.

Now that you’re tracking your search terms, check out Google’s advice for interpreting it. To paraphrase my buddy Gahlord, analytics data is only useful if you’re using it make decisions.

Remove the tag cloud from the taxonomy edit screen

Screen Shot 2016-03-25 at 12.24.45 PM

In the WordPress admin taxonomy edit screen, Tags and any hierarchical custom taxonomies include a tag cloud of “Popular Items” in the left column above the Add Term form. I find this feature useless in most cases, particularly on a large site that may have hundreds of terms in a taxonomy.

Strangely, the official way to disable this tag cloud is to set the popular_items label to null when registering your taxonomy. Indeed, this is the only place the label appears to be used at all. If you’ve registered your own taxonomies and you can override that label in your code, go ahead and do that to clear up the problem. If you have taxonomies registered by a plugin or another method that is outside of your direct control, you can remove it by filtering the taxonomy arguments to unset the label. Here’s how:

/**
 * Remove tag cloud from taxonomy edit screen.
 */
function my_remove_popular_term_cloud( $args ) {
	$args['labels']['popular_items'] = null;
	return $args;
}
add_filter( 'register_taxonomy_args', 'my_remove_popular_term_cloud' );

If you want to target specific taxonomies, you can check which taxonomy you’re working with by passing additional parameters to the filter:

/**
 * Remove tag cloud from taxonomy edit screen.
 */
function my_remove_popular_term_cloud( $args, $taxonomy ) {
	if ( 'post_tag' === $taxonomy ) {
		$args['labels']['popular_items'] = null;
	}
	return $args;
};
add_filter( 'register_taxonomy_args', 'my_remove_popular_term_cloud', 10, 2 );

Now your taxonomy edit screens will be nice and tidy!

Find the top level parent term

Here’s a quick helper function to find the top level ancestor of a given term. If you like to organize your categories in nested fashion, this function will find the very top level parent no matter how deep down the term your working with is nested.

/**
 * Get the top level parent of a given term.
 * @param WP_Term|int The term who's ancestors we'll be tracing.
 * @param string Name of taxonomy to search. Required only if $term is an ID instead of a WP_Term object.
 * @return WP_Term|bool The top level parent of $term. If $term has no parent, return false.
 */
function get_term_progenitor( $term, $tax = 'category' ) {
	if ( is_int( $term ) ) {
		$term = get_term_by( 'id', $term, $tax );
	}

	if ( 0 == $term->parent || ! $term instanceof WP_Term ) {
		return false;
	}

	while ( $term instanceof WP_Term && 0 != $term->parent ) {
		$term = get_term_by( 'id', $term->parent, $term->taxonomy );
	}
	return $term;
}

To use it feed in a term object or a term id and taxonomy combo to get the original ancestor of your term. If your term has no parent, it will return false. This is helpful if you need to apply a class to a whole tree of terms, for example. If your

It’s worth noting that this function will call get_term_by() multiple times, and it is known to create slower database queries. If you have nested your terms more than a few levels it may be worth storing the results of this function in a persistent object cache to speed up performance.