Monday, July 18, 2022
HomeWordPress Developmentplugin growth - How you can create ShortCode

plugin growth – How you can create ShortCode


You might want to use hooks to create customized columns and information for a customized publish kind are manage_{$post_type}posts_columns and handle{$post_type}_posts_custom_column respectively.

{$post_type} is the title of the customized publish kind.

I’ve created frequent operate so as to add column and and their information. You need to use beneath operate so as to add column and it is information.

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

How you can use above operate. I’ve added two columns “Shorcode” and “Template Shortcode” utilizing beneath operate. ‘web page’ is publish kind, you’ll be able to substitute together with your customized publish kind.

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

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

Here’s a shortcode, you’ll be able to handle your shortcode output right here:

# shortcode
add_shortcode('logo_showcase','logo_showcase_callback');
operate 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 together with your customized discipline title
       $CUSTOM_FIELD = get_post_meta($id,'CUSTOM_FIELD',true);
     }
    return $html;
}

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

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments