0

I am learning Plugin Development. How to create ShortCode like the image ?

enter image description here

2
  • 1
    Just to warn you, only because you've mentioned repeatedly that you're learning WordPress plugin development, you're focusing on meta boxes and shortcodes but both of these are essentially dead technologies that have no future in WordPress. To learn WordPress development you should focus on learning blocks and the block editor. Commented Jul 18, 2022 at 6:19
  • Thanks @JacobPeattie for your nice and valuable advice. Thanks. Commented Jul 18, 2022 at 8:17

1 Answer 1

2

You need to use hooks to create custom columns and data for a custom post type are manage_{$post_type}posts_columns and manage{$post_type}_posts_custom_column respectively.

{$post_type} is the name of the custom post type.

I have created common function to add column and and their data. You can use below function to add column and it's data.

function cv_add_column_to_admin_dashboard($column_title, $post_type, $cb){
    # Column 
    add_filter( 'manage_' . $post_type . '_posts_columns', function($columns) use ($column_title) {
        $columns[ sanitize_title($column_title) ] = $column_title;
        return $columns;
    } );
    # Column Content
    add_action( 'manage_' . $post_type . '_posts_custom_column' , function( $column, $post_id ) use ($column_title, $cb) {
        if(sanitize_title($column_title) === $column){
            $cb($post_id);
        }
    }, 10, 2 );
}

How to use above function. I have added two columns "Shorcode" and "Template Shortcode" using below function. 'page' is post type, you can replace with your custom post type.

# column for shortcode
cv_add_column_to_admin_dashboard(__('Shortcode'), 'page', function($post_id){
    echo '<code>';
    echo '[logo_showcase id="'.$post_id.'"]';
    echo '</code>';
});

# column for template shortcode
cv_add_column_to_admin_dashboard(__('Template Shortcode'), 'page', function($post_id){
    $code = '<?php echo do_shortcode("[logo_showcase id="'.$post_id.'"]"); ?>';
    echo '<code>'.htmlspecialchars($code).'</code>';
});

Here is a shortcode, you can manage your shortcode output here:

# shortcode
add_shortcode('logo_showcase','logo_showcase_callback');
function logo_showcase_callback($atts)
{
    $html = '';
    $atts = shortcode_atts( array(
        'id' => '',
    ), $atts, 'default' );
    
    $id = $atts['id'];
    if(!empty($id))
    {   
       $html .= get_the_title($id);
       # change CUSTOM_FIELD with your custom field name
       $CUSTOM_FIELD = get_post_meta($id,'CUSTOM_FIELD',true);
     }
    return $html;
}

Please use above code in your plugin. Please check and let me know if this works for you.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.