2011-04-30 48 views
0

我已经重新编辑了这个问题:是否有可能在显示点2的输出之前将一个变量传递给全局颜色(点3)像全局变量或其他?WordPress插件和wp_head

 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。现在即时阅读有关自定义字段。 enter code here

回答

1

do_action()被WordPress调用,你想要add_action()

行动init来得太早。你现在甚至为后端调用这个类,用于AJAX请求等。使用仅在前端调用的钩子template_redirect

您无法按照您尝试的方式发送颜色值。查看示例代码以获取工作示例。

示例代码:

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')); 

我强烈建议https://wordpress.stackexchange.com/问WordPress的更多的问题。 :)

+0

谢谢,但即时通讯不是很清楚,我也更新了我的完整方案。 – greenbandit 2011-05-02 05:15:11

+0

@greenbandit这是一个完整的新问题。 ;)在**'wp_head'之后,短代码被解析**。改为使用[自定义字段](http://wordpress.stackexchange.com/questions/tagged/custom-field)。 – fuxia 2011-05-02 09:28:45

+0

@toscho关于短代码的好处,主要想法是通过短代码像[背景颜色=“#0066ff”bg =“url”]添加自定义颜色和背景任何想法? – greenbandit 2011-05-02 18:11:45