0

Don't know what I'm doing wrong but, I have the following code:

class the_shortcode {

    //Define Class Variables

    private $var;

    public function __construct()
    {
            add_shortcode( 'the_single', array( $this, 'shortcode_2' ));

    }


    function shortcode_1() {

        $this->var = 'World';

    }

     function shortcode_2() {

         echo $this->var;

    }

}
$the_shortcode = new the_shortcode();

I was expecting that shortcode_1() would define the $var and that shortcode_2(), when called in the shortcode action, to display it resulting in "World" - but instead absolutely nothing is displayed.

On the post, I have the shortcode: [the_single]

Any thoughts of what's wrong?

Thanks!

  • You don't call the method shortcode_1(). The setting of the var with the value Ẁorld` was not called. You must call this inside the constructor or the shortcode_2() method. – bueltge Dec 7 '14 at 10:30
0

Thanks to the response by @bueltge :

Added extra line in the __construct() function:

public function __construct()
{
      $this->var = $this->shortcode_1(); //added
      add_shortcode( 'the_single', array( $this, 'shortcode_2' ));

}

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.