开发者

WordPress plugin and wp_head

I've re-edited this question: is possible to before to show the output in point 2 pass a variable to global color (point 3) like a global开发者_Python百科 variable or something?

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

             }

             public function shortcode( )
             {
                 // *point2
             } 

             function globalcolor($color)


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

add_action( 'wphead', array( 'myclass', 'globalcolor' ) ); 

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

PS. right now im reading about custom fields.enter code here


do_action() is called by WordPress, you want add_action().

The action init comes way too early. You call the class now even for the backend, for AJAX requests etc. Use the hook template_redirect which is called on the frontend only.

You cannot send the color value the way you tried. See the sample code for a working example.

Sample code:

class My_Plugin {

    /**
     * Container for your color value.
     * @var string
     */
    static $color;

    public static function init()
    {
        // Set the color value as a class member.
        self::$color = '#345';

        // Class methods are addressed with an array of the object or the
        // class name and the function name.
        add_action( 'wp_head', array ( __CLASS__, 'print_color' ) );
    }

    public static function print_color() 
    {
        // In action you have to print/echo to get an output.
        print '<style>body{color:' . self::$color . '}</style>';
    }
}
add_action( 'template_redirect', array ( 'My_Plugin', 'init' ) );

I strongly recommend https://wordpress.stackexchange.com/ to ask more questions on WordPress. :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜