I understand that although not used by core in any way you can set a description for a CPT:
$args = array(
[...]
'description' => "My CPT description"
[...]
);
register_post_type( 'my_cpt', $args );
You can then access it like that:
get_post_type_object( 'my_cpt' )->description;
But that leaves us directly accessing a property with no way to filter it. Am I correct that this is in fact the way it is and the only way to have this filterable is to introduce a function with a filter to access it myself?
Aside: I have seen this but it kind of solves the use case but by actually not using the CPT description any more.
EDIT: Okay I need to clarify – I’d like to filter it when requested and not always which is not possible with registered_post_type
as birgire suggested.
I am trying to make the description user editable (also see this as linked above). But if I e.g. store the description as an option I do not want to request this option on each page load if it is only rarely needed.
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 );