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;
}