How to filter the description of a custom post type

Since WP 3.3, there’s an action to modify a registered post type after registration.

do_action( 'registered_post_type', $post_type, $args );

The $post_type is the post type object and there doesn’t happen much after the post type object gets stuffed in the global and before the callbacks attached to this action execute. In fact it’s (with WP 4.0) just the connection between a post type and its taxonomies that gets set.

As the description is not really any (public) use case, doesn’t get registered as part of the (filterable) labels and doesn’t get stuffed into the DB – only registered during runtime at the global – it’s say it’s pretty safe to just alter the description there. Benefit is that you are just re-setting a single value in a pretty small global array. So there should be not notable performance decrease.

When you look at get_post_type_object(), which is the default entry point to grabbing data from the (C)PT, then it’s also just calling the global:

function get_post_type_object( $post_type ) {
    global $wp_post_types;

    if ( empty($wp_post_types[$post_type]) )
        return null;

    return $wp_post_types[$post_type];
}

I’d suggest the following:

<?php
/** Plugin Name: (#160620) Alter {$CPT}-post type description */
add_action( 'registered_post_type', function( $pt, $args )
{
    if ( ! in_array( $your_cpt, get_post_types( array( '_builtin' => false ) ) ) )
        return;

    $GLOBALS['wp_post_types'][ $pt ]['description'] = 'I changed.';
}, 0, 2 );