2011-04-30 10 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')); 

추신. 지금 사용자 정의 필드에 대해 읽는 중입니다. 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/을 권장합니다. :)

+0

감사합니다.하지만 완전히 명확하지는 않습니다. 또한 전체 구성표로 업데이트했습니다. – greenbandit

+0

@greenbandit 그건 완전히 새로운 질문입니다. ;) 단축 코드는 **'wp_head' 이후에 파싱됩니다. 대신 [사용자 정의 필드] (http://wordpress.stackexchange.com/questions/tagged/custom-field)를 사용하십시오. – fuxia

+0

@toscho 짧은 코드에 대한 좋은 점, 주요 아이디어는 [background color = "# 0066ff"bg = "url"] 아이디어처럼 짧은 코드를 통해 사용자 정의 색상과 배경색을 추가합니까? – greenbandit

관련 문제