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 have this very basic scheme to build my first plugin, but now I have one problem: when I put a hook to add_action on point 1 works, but i need to get the color from shortcode function, so, when i put the code at point 2 don't works.

        class myclass
        {
             public function init()
             {
                  global $shortcode_tags;
                      add_shortcode( MYSHORTCODE, array( 'myclass', 'shortcode' ) );
                              // * point 1
                  return;

             }

             public function shortcode( )
             {
                 // want to run add_action here
                             // i need to get the color from shortcode that part is done
                             // question is how i can run add_action from here and pass
                             // the variable to function globalcolor (point 3), then run it in wp_head?
                             // * point 2
             } 

             function globalcolor($color)
             {
                             echo '<style>body{color:' .$color . '}</style>' . "\n";
                             // * point 3
             }
        }

        add_action( 'init', array( 'myclass', 'init', ) );            


the code to add: add_action( 'wp_head', array( 'myclass', 'globalcolor' ) );

Any ideas? and sorry for my bad english.

share|improve this question
    
For reference: See the same question on StackOverflow. –  toscho May 2 '11 at 9:55

1 Answer 1

up vote 2 down vote accepted

The main problem is you are using static callbacks while you should be passing on the object so you have access to your class properties.

You can fire off the initialization like this:

class Myclass {

  public function __construct() {
    // Object method call, instead of a static one
    add_action('init', array($this, 'init'));
  }

  public function init() {
    // add_shortcode(...);
  }

}

// Trigger constructor
new Myclass;

On another note, instead of hard-coding styles in the <head>, you may prefer to customize the body classes instead. The filter body_class is available for that (related example).

share|improve this answer

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.