How to display the description of a custom post type in the dashboard?

admin2025-05-31  1

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
  • You’ve got code there that outputs it. What else do you need to know? That’s the code. – Jacob Peattie Commented Apr 20 at 13:34
  • Hmm, where you should I put this code ? Maybe I understand it the wrong way, but this code is made to be used to display this description in the front-end. And I want it in my dashboard exactly like I would do with labels (and get_post_type_labels()). – PhpDoe Commented Apr 20 at 14:15
  • the default behaviour of creating a custom post type is to add a new item in the front office menu. that add nothing in the dashboard then edit your question to explain exactly where you want to display the description. – mmm Commented Apr 20 at 15:56
  • I've updated my question with more details and a screenshot of I want to achieve. I dont see the point with this description argument. What is its purpose exactly ? I dont find any tutorial on the web, so I'm guessing it's not really commonly used. – PhpDoe Commented Apr 20 at 16:13
Add a comment  | 

1 Answer 1

Reset to default 0

this 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,
        ]
    );
    
    
});
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748639761a313708.html

最新回复(0)