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

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')){               


$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
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.


class MyClass{
    function __construct(){ 
        if( isset($_POST['my_nonce_field']) && check_admin_referer('my_nonce_action', 'my_nonce_field'))

function newobj(){
    $myclass = new MyClass;
share|improve this answer
+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


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.