Jose Lo
Jose Lo

Reputation: 81

class_exists not working WordPress

I have in WordPress functions.php the following function:

if ( ! function_exists( 'mm_single_slider' ) ) :
function mm_single_slider($echo = true){


if ( class_exists( 'RW_Meta_Box' ) ) { 

    //Do Something

} else {

    //Do other things

}

}
add_action('plugins_loaded','mm_single_slider' );

But the class_exists is not working, returns false. This class is in a plugin that is activated and obviously exists.

I have also in my functions.php file this:

require_once ('inc/mm-metabox.php');

this file includes a similar functions where the class_exists is working, returns true.

I have tried with the 'plugins_loaded' hook and without it.

What am I doing wrong? It seems that when the file is required the class_exists is working but not directly in the functions.php file?

Thank you very much for the help.

Upvotes: 0

Views: 3023

Answers (1)

David
David

Reputation: 5937

   function mm_single_slider($echo = true){
       if ( function_exists( 'mm_single_slider' ) ) 
       return;

       if ( class_exists( 'RW_Meta_Box' ) ) { 

//Do Something

       } else {

//Do other things

       }

in your functions file, you should not have any code outside your functions. Also you had a mistake in your if syntax. The correct way for the method you were using is

 if(cond): 
   do something;
 else:
   something else;
 endif;

there is a endif; at the end of that function in 20/14.

Your problem will depend on where you are placing the above code. If it is in a plugin, mm_single_slider may not exist when you are calling the code which is when the plugin code is running. Hence you put your functions exists into into your function and hook it to run later.

If you are placing it in your theme, Plugins run before themes so anything you hook in your theme for plugins loaded will not be called as the hook has passed. see http://codex.wordpress.org/Plugin_API/Action_Reference for the order of loading.

Upvotes: 1

Related Questions