What I'm already able to achieve: using register_post_type() to create a new Custom Post Type. It works perfectly.
But between all the arguments, there is this argument for registering a post type I can't handle: description.
In the WordPress documentation, we can find:
description. string
A short descriptive summary of what the post type is.
What I would like to do: I would like to retrieve this description and to display it this in my Dashboard below the label name of the Custom Post Type, or if it's not possible below the main title, like in this screenshot:
It would be pretty handy for users to display it for example below the label name. But it doesn't seem to be a default behaviour. The only way to display it (says WordPress) is:
$obj = get_post_type_object( 'your_post_type_name' );
echo esc_html( $obj->description );
But how to use it in my code? I don't find any more precise explanation.
What I'm already able to achieve: using register_post_type() to create a new Custom Post Type. It works perfectly.
But between all the arguments, there is this argument for registering a post type I can't handle: description.
In the WordPress documentation, we can find:
description. string
A short descriptive summary of what the post type is.
What I would like to do: I would like to retrieve this description and to display it this in my Dashboard below the label name of the Custom Post Type, or if it's not possible below the main title, like in this screenshot:
It would be pretty handy for users to display it for example below the label name. But it doesn't seem to be a default behaviour. The only way to display it (says WordPress) is:
$obj = get_post_type_object( 'your_post_type_name' );
echo esc_html( $obj->description );
But how to use it in my code? I don't find any more precise explanation.
Share Improve this question edited Apr 20 at 16:12 PhpDoe asked Apr 20 at 11:59 PhpDoePhpDoe 3112 silver badges12 bronze badges 4 |1 Answer
Reset to default 0this answer is for showing the description in the menu. if you want show it somewhere else, you should create a new question.
I often try to propose solutions that don't use javascript if not necessary. but for this customisation, there is not practical hooks to do that then I think it will be no very reliable to use output buffering.
in the following code, put the slug of the CPT, the URL of the javascript file and replace MY_PLUGIN
by the slug of your plugin to limit interference with other plugins.
code to put in menu.js
"use strict";
document.addEventListener("DOMContentLoaded", e => {
const menu_entry = document.querySelector(`#menu-posts-${MY_PLUGIN["cpt_slug"]} a.wp-has-submenu`);
const description = document.createElement("div");
description["textContent"] = MY_PLUGIN["description"];
description["classList"].add("cpt_description");
menu_entry.insertAdjacentElement("afterend", description);
});
loading of the javascript code
add_action("adminmenu", function () {
wp_enqueue_script(
"MY_PLUGIN/menu"
, "menu.js" // URL of the file
, []
, 123 // version of the file menu.js
);
$cpt_slug = "tache_et_procedure"; // CPT slug to correct here
$obj = get_post_type_object($cpt_slug);
wp_localize_script(
"MY_PLUGIN/menu"
, "MY_PLUGIN"
, [
"cpt_slug" => $cpt_slug,
"description" => $obj->description,
]
);
});
get_post_type_labels()
). – PhpDoe Commented Apr 20 at 14:15