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.
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:
On another note, instead of hard-coding styles in the
<head>
, you may prefer to customize the body classes instead. The filterbody_class
is available for that (related example).