Adding shortcode in WordPress is very simple. You can do it from functions.php of your child theme:

 // function that runs when shortcode is called
 function wpb_demo_shortcode() { 
 
    // Things that you want to do. 
    $message = 'Hello world!'; 
    // Output needs to be return
    return $message;
} 
    // register shortcode
add_shortcode('greeting', 'wpb_demo_shortcode'); 

To display shortcode you can create a text block and insert [greeting] as its value.
You can also place a shortcode in PHP using the following code:


// Use shortcode in a PHP file (outside the post editor).
echo do_shortcode( '' );

// In case there is opening and closing shortcode.
echo do_shortcode( '[iscorrect]' . $text_to_be_wrapped_in_shortcode . '[/iscorrect]' );

// Enable the use of shortcodes in text widgets.
add_filter( 'widget_text', 'do_shortcode' );

// Use shortcodes in form like Landing Page Template.
echo do_shortcode( '[contact-form-7 id="91" title="quote"]' );

// Store the short code in a variable.
$var = do_shortcode( '' );
echo $var;

WordPress – Add a shortcode from a class method:

/**
     * This function register shortcode for given class method
     *
     * @return void
     */
    public function registerShortcodes()
    {

        add_shortcode('showbooks', [$this, 'showBooks']);
    }

Note that the output should be returned by the function. So if you need to print multiline sentence or html you would simple save output to variable and return the entire output at once. Do not use echo here.

For example:

<?php

/**
 * @package WordPress
 */

namespace Plugin\MyPlugin\Admin\CustomPost;

use Plugin\MyPlugin\Interfaces\Admin\CustomPostTypeInterface;

class ExamplePostType implements CustomPostTypeInterface
{
    /**
     * class constructor.
     */
    public function __construct(string $customPostTypeName = "book")
    {
        $this->customPostTypeName = $customPostTypeName;
        add_action('init', [$this, 'registerPostType']);
        $this->registerShortcodes();
    }

    public function registerPostType()
    {
        $postTypeNameSingular = $this->customPostTypeName;
        register_post_type($postTypeNameSingular, [
            'public' => true,
            'label' => ucfirst($postTypeNameSingular . 's'),
        ]);
    }

    public function showBooks()
    {
        $books = get_posts([
            'post_type' => $this->customPostTypeName, // "book"
            'num_posts' => -1,
        ]);
        $output = "Books List: <br>";
        foreach ($books as $book) {
            $output .= $book->ID . "<br>";
            $output .= $book->post_title . "<br>";
            $output .= $book->post_excerpt . "<br>";
            $output .= $book->post_content . "<br>";
        }

        return $output;

    }

    /**
     * This function register shortcode for given class method
     *
     * @return void
     */
    public function registerShortcodes()
    {

        add_shortcode('showbooks', [$this, 'showBooks']);
    }


}

https://developer.wordpress.org/reference/functions/do_shortcode/
https://developer.wordpress.org/plugins/shortcodes/shortcodes-with-parameters/

0
Would love your thoughts, please comment.x
()
x