Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I tried to add a shortcode this way

class MyPlugin {

    function __construct() {
        $this->make_shortcode();
    }

    function ShowMsg($cls, $lst4) {
        $data = shortcode_atts(array('phn' => '', 'msg' => ''), $atts);
        return 'You sent '.$data['msg '] .' from '.$data['phn'] ;
    }

    function make_shortcode() {
        add_shortcode('ShowMsg', 'ShowMsg');
    }

}
new MyPlugin;

And [ShowMsg phn="123456" msg="Test Message"] isn't working, it's returning full shortcode instead of desired text.

I need your advice to fix.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

That is not the way you add an object method as a callback.

function make_shortcode() {
    add_shortcode('ShowMsg', array($this,'ShowMsg'));
}

This is explained in the Codex as it pertains to actions and filters, but the principle is the same.

I should add that anonymous classes make for painful debugging. Instantiate that class to a variable. It will save you headaches.

share|improve this answer
    
Agreed. How Wordpress instantiates class in this regard has a few eccentricities that can cause headaches. Better to use a wrapper function to instantiate the class and be done with it. –  Hybrid Web Dev May 21 at 18:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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