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.

How to use the check_admin_referer method with oop?
If I use as it follows the function can not be called:

class MyClass{
    function __construct(){ 

        if( isset($_POST['my_nonce_field']) && check_admin_referer('my_nonce_action', 'my_nonce_field')){               
            $this->update_item();
        }

    }
}

$test = new MyClass();

The above leads to the following error message:

Call to undefined function check_admin_referer()
share|improve this question
    
Please post more context. How are you attempting to use this block of code? How do you instantiate the object? –  s_ha_dum Mar 20 '13 at 13:37
    
I should add that I use check_admin_referer in plugins and in Object context. I think the problem is how you using it. In the constructor var_dump(function_exists('check_admin_referer')); does return false, at least in the simple testing I've done. I haven't worked out why. –  s_ha_dum Mar 20 '13 at 14:39

1 Answer 1

up vote 2 down vote accepted

check_admin_referer is a pluggable function which means it is defined after plugins are loaded so call your constructor or instantiate the object after or using plugins_loaded action hook.

ex:

class MyClass{
    function __construct(){ 
        if( isset($_POST['my_nonce_field']) && check_admin_referer('my_nonce_action', 'my_nonce_field'))
            $this->update_item();
    }
}

add_action('plugins_loaded','newobj');
function newobj(){
    $myclass = new MyClass;
}
share|improve this answer
1  
+1. Alternative: hook an additional method to 'plugins_loaded' (or any subsequent action) from within the constructor. –  Johannes Pille Mar 20 '13 at 15:01
    
thanks a lot for your help! –  fefe Mar 20 '13 at 15:06
    
yep that would do the same thing. –  Bainternet Mar 20 '13 at 15:08

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.