plugin development: problem with functions

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.

Related posts

Leave a Reply

1 comment

  1. 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).