1

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;
}
  • There are some issues here but it could help if you looked further into static vs instantiated class. Avoid using undefined variables in your methods. What about starting with a class that has only a single (static?) method, make that work, and then expand in smaller steps. – birgire Oct 3 at 15:02
  • You're referring to variables that aren't defined or don't exist, such as $data->display_summary, and there's no initial value for $grant_total either. You will be seeing PHP warnings and notices in your error log for these – Tom J Nowell Oct 3 at 15:09
1

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

either declare it INSIDE an instance of a class, 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, and then you must use a public static method.

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

class myClass {


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

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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