0

I'm trying to make some of the variables from my plugin available for an instance of the class on my page. But it's not showing up the correct values.

Here's the relevant part of the plugin:

class opi{
    // version
    public $opi_version = "1.0";

    public $mg = 0;

    //constructor
    public function opi(){
        $this->__construct();
    }

    public function __construct(){
        add_action('init', array(&$this, 'o_check'));
        // ...
    }

    function o_check(){
        if (is_user_logged_in()){
            $this->mg = 4;
        }
    }
}
$opi = new opi;

If I put echo $this->mg; just before the closing brace in the o_check() function, then it shows 4. Fine.

Now if I put the following in page.php, and I'm logged into WordPress, I get the result 0.

<?php $pageopi = new opi; ?>
<?php echo $pageopi->mg; ?>

Any ideas where I'm going wrong please?

0

3 Answers 3

2

When you write that code

<?php $pageopi = new opi; ?>
// 1. object created
// 2. __construct is called
// 3. init action hook is set but is never executed so is o_check method
<?php echo $pageopi->mg; ?>

At this stage, init hook was executed long time before $pageopi is created actually, when page is loaded. init action hook is executed even before page.php start to execute.

Solution:

  1. You should use the global $opi; object page.php instead of create new one.

  2. Or create a new global object before the init action hook (in plugins_loaded hook) and use it on page.php

2
  • $opi isn't available on the page, which is why I created $pageopi.
    – user417627
    Jul 31, 2013 at 15:05
  • have you tried global $opi; ?
    – M-R
    Jul 31, 2013 at 15:07
2

Well, you're not executing o_check in your page.php, so the value of mg variable stays at zero.

In your constructor you are adding an action hook, yes, but it will only kick in on the next page refresh. Consider using do_action as well, though I'm not sure if executing one then immediately the other will work.

2
  • Thanks. Putting do_action around the function call to o_check on the page seems to work.
    – user417627
    Jul 31, 2013 at 15:04
  • 2
    Executing 'init' action multiple times may develop new issues. because it is supposed to be only executed by WordPress. Re-executing it means re-initializing everything that bound to 'init' action.
    – M-R
    Jul 31, 2013 at 15:12
0

If you add this part:

<?php $pageopi = new opi; ?>
<?php echo $pageopi->mg; ?>

in a theme template it will initialize the object after the init hook runs and so the o_check method will never run. That is the problem. That is why it works if you initialize beneath the object but not if you initialize in the page template. You need to change hooks, or initialize earlier, or call o_check explicitly.

Initializing your object as a global would work.

class opi{
  // ...
}
global $opi;
$opi = new opi;

Then, in your template you could use:

global $opi;
echo $opi->mg;

Though the way you are using it a static class might as well or better. It is hard to say. I can't tell what the end purpose of the class is.

2
  • I tried several different hooks and init seemed okay because I need access to is_user_logged_in.
    – user417627
    Jul 31, 2013 at 15:06
  • You missed the point. If you initialize your class in the page template, init has already executed thus your o_check method never runs.
    – s_ha_dum
    Jul 31, 2013 at 15:09

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.