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.

I'm developing a plugin and I try to use a class (following the method of another plugin). But I don't understand why my constants are not globally available. This is my code:

/*
Plugin Name: Some simple plugin
*/

  if (!session_id())
    session_start();

class myPluginClass {

  function __construct() {
    /* Set the constants needed by the plugin. */
    add_action( 'plugins_loaded', array( &$this, 'constants' ), 1 );
  }


  function constants() {
    $upload_dir = wp_upload_dir();

    define( 'SIMPLE_GALLERY_DIR', trailingslashit( str_replace('\\', '/', plugin_dir_path( __FILE__ )) ) );

    //This works and outputs http://.....
    echo SIMPLE_GALLERY_URL;
  }

}

$sg = new simpleGallery();
//This does not work and outputs SIMPLE_GALLERY_URL.
echo SIMPLE_GALLERY_URL;

The result of the above two is: SIMPLE_GALLERY_URLhttp://.....

This means that my last echo is executed before the class. Or is it?

constants() is attached to the plugins_loaded hook - could it be that this is executed after page is loaded?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Your constant is defined in the "plugins_loaded" action, but you are trying to access it before that action gets to be executed.

share|improve this answer
    
I thought so. What would be the correct hook? –  Steven Apr 13 '12 at 11:23
    
Why do you need to define that constant within that action anyway? You can just do it inside the constructor –  onetrickpony Apr 13 '12 at 11:31
    
Good point :) I was too focused on following the coding style of another plugin. –  Steven Apr 13 '12 at 11:34

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.