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

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())

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://.....


$sg = new simpleGallery();
//This does not work and outputs 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
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


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.