WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I tried to add a shortcode this way

class MyPlugin {

    function __construct() {

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

Your Answer


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.