0

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.

5

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.

  • 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 '14 at 18:13

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.