2011-04-30 87 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