0

I want to generate auto shortcode for custom post type. This is same as the elementor template shortcode.

I have a custom post type named clients and there are many clients. Single client post type is generate with custom template. Now what I want is to generate auto shortcode for every single posts. Like for client1 : [clients name="1"] for client2: [client name="2"]. If I create a new client, the shortcode will be generated automatically and will be shown in a field in the list.

I am stuck and without any idea. Please help.

Thank you

4
  • Hello! Welcome to WPSE, can you explain in different words what you mean by "generate auto shortcode"? This doesn't make much sense to me, and what is the list you're referring to? Can you explain what you want without using shortcodes? What are you trying to implement using shortcodes?
    – Tom J Nowell
    Commented Jan 21, 2020 at 12:02
  • So here is answer to your question @TomJNowell : 1. I want to show custom post as topic in learndash. That's why I am trying to generate shortcode for each post. I have an idea, the posts could be parsed via post id. But I want that shortcode to be generated automatically. And the list I mention is the list of post (custom). Hope this helps. Would love to hear your ideas Commented Jan 22, 2020 at 4:29
  • How are the posts being created?
    – Tom J Nowell
    Commented Jan 22, 2020 at 10:46
  • Can u tell me how to add builder element into the shortcode. I want all the post content from shortcode. Your example displays post title and I am using Avia builder enfold for sections and I want it to load with shortcode. Commented Sep 3, 2020 at 7:58

1 Answer 1

0

There's nothing to "generate". All Elementor is doing is giving an example of how to use their shortcode for a specific item. You only need one shortcode client, that accepts a parameter to determine which client to show:

add_shortcode(
    'client',
    function( $atts ) {
        $atts = shortcode_atts( [ 'id' => null ], $atts, 'client' );

        if ( $atts['id'] ) {
            $post = get_post( $atts['id'] );

            return $post->post_title;
        }
    }
);

So that basic shortcode can be used to output the post title for the given post ID.

That's all you need for the shortcode to work for all your client posts. There's no need to generate something for each post. However, what you might want do do, and what it sounds like Elementor does, is give a preview of how to use the shortcode for each post, to save the user having to write it themselves.

To do this, you can add a column to the posts list table. Assuming your post type is called client, you can add the column with manage_client_posts_columns:

add_filter(
    'manage_client_posts_columns',
    function( $columns ) {
        $columns['shortcode'] = 'Shortcode';

        return $columns;
    },
    100
);

And then you can populate it with manage_client_posts_custom_column. All you need to do is print your shortcode prefilled with the ID of the current post:

add_action(
    'manage_client_posts_custom_column',
    function( $column, $post_id ) {
        if ( 'shortcode' === $column ) {
            echo '[client id="' . $post_id . '"]';
        }
    },
    10,
    2
);
1
  • Thank you Jacob. It worked and thank you for explaining it nicely. Commented Jan 23, 2020 at 3:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.