Shortcode Inside Class Not Working

Working on my first WordPress plugin and using OOP. I'm trying to create a shortcode that works within a class and confused about how to set it up.

With my current setup, the shortcode only prints out the shortcode name on the front end. Here's what I have now inside the class php file:

if ( !class_exists( 'month_snapshot' ) ) {

class month_snapshot
{
    private $grand_total;

    public function __construct()
    {
      //code here to create grand_total
      $this-grand_total = $grand_total;
    }

    public function get_grand_total() {
        return $this-grand_total;
    }

    public function display_summary() {
        return "Grand Total is " . $this-grand_total . "br/";
    }

    //month_summary shortcode
    public function month_summary_shortcode($atts = [], $content = null) {
        $content = $data-display_summary;          
        return $content;
    }

} //close class

} //close if not exists

//Shortcodes
add_shortcode( 'month_summary', array( 'month_snapshot', 'month_summary_shortcode' ) );

ETA: Thank you everyone for your help.

I cut out some code on my original post to make it easier to read - probably shouldn't have as it added to some confusion with undeclared variables, etc.


RESOLUTION: For those looking for a similar solution I ended up changing my strategy a bit based on Jess Pinkman's clarification (thank you). My class ended up looking like this:

if ( !class_exists( 'month_snapshot' ) ) {

class month_snapshot
{
    public $grand_total;

    public function __construct()
    {
      //I omitted code here which got a grand_total, let's pretend it resulted in 1
      $grand_total = 1;
      $this-grand_total = $grand_total;
    }

} //close class

} //close if not exists

Then, I declared my shortcode and corresponding function externally. It called upon the class to get the $grand_total result I was looking to display.

add_shortcode('month_summary', 'month_summary_shortcode');

function month_summary_shortcode() {
  $month_data = new month_snapshot;
  $monthly_total = "Grand Total is " . $month_data-grand_total . "br/";
  return $monthly_total;
}

Topic oop shortcode php Wordpress

Category Web


if you use any hooks, filters or shortcodes with class methods, you have three options.

either declare it in the context of an instance, in which case, you can refer to the instance of the class.

class myClass {
    function __construct()
    {
        add_shortcode('my_shortcode_name', [ $this, 'shortcode' ]);
    }
    
    function shortcode()
    {
        //your code here
        //properties of the instance are available
        return 'a string';
    }
}

or you declare it outside of an instance, then you must use a public static method.

add_shortcode('my_shortcode_name', [myClass::class, 'shortcode']);

class myClass {
    
    
    public static function shortcode()
    {
        //your code here
        //you only have access to static properties
        return 'a string';
    }
    
}

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.