2013-11-14 7 views
1

문서 및이 작업을 수행하는 방법에 대한 예제를 살펴 보았지만 아래 코드에서 문제점을 확인할 수 없습니다. 내 자식 테마의 함수가 호출되지 않습니다. ... 아마 눈부시게 분명하지만, 난 그냥 환영 어떤 포인터를 참조를 볼 수 없습니다Wordpress는 자식 테마의 필터를 추가/필터를 추가합니다.

부모 테마의 functions.php 자식 테마

add_action('init', 'st_header_scripts'); 
    function st_header_scripts() { 
     $javascripts = wp_enqueue_script('jquery'); 
     $javascripts .= wp_enqueue_script('custom',get_bloginfo('template_url') ."/javascripts/app.js",array('jquery'),'1.2.3',true); 
     $javascripts .= wp_enqueue_script('superfish',get_bloginfo('template_url') ."/javascripts/superfish.js",array('jquery'),'1.2.3',true); 
     $javascripts .= wp_enqueue_script('formalize',get_bloginfo('template_url') ."/javascripts/jquery.formalize.min.js",array('jquery'),'1.2.3',true); 

     echo apply_filters ('child_add_javascripts',$javascripts); 
    } 

...

function child_add_javascripts($javascripts) { 
     $javascripts = "test"; 
     echo "test"; die; 
     return $javascripts; 
    } 

    add_filter('st_header_scripts','child_add_javascripts'); 

답변

2

여기 몇 가지 잘못된 점이 있습니다. wp_enqueue_script는 return 함수가 아니므로 변수에 설정해야 할 이유가 없습니다. 그것이 무엇을하는 wp_head 한 번에 필요한 모든 스크립트 태그를 생성()이다

이 두 번째로, 문제가 add_filter 및 apply_filter의 사용에서 유래 header.php 내에서 호출된다. 그러나 나는 우리가 실제 차이는 행동과 필터 사이에 무슨 이상 이동해야한다고 생각 (당신은 아마 알고있을, 그러나 다른 사람은 그렇지 않을 수 있습니다) :

조치가
필터를 수신하는 데이터에 따라 물건을 수행에 물건을 할 데이터가 수신하고

do_action()apply_filter() 당신이 그것을 2-n 번째 인수의로 콜백에 전달하려는 트리거의 첫 번째 매개 변수로 트리거 이름에 걸릴 기능 및 인수입니다 반환합니다.

add_action()add_filter()은 첫 번째 인수에서 정의 된 이름을 찾고 두 번째 인수에서 정의 된 콜백 함수를 실행하는 리스너입니다.

여기에 사례가 있으면 액션 후크의 3 번째 매개 변수를 사용하여 액션 후크의 우선 순위를 지정하는 것이 좋습니다.

상위 주제 :

add_action('wp_enqueue_scripts', 'st_header_scripts'); 
function st_header_scripts() { 
    wp_enqueue_script('jquery'); 
    wp_enqueue_script('custom',get_bloginfo('template_url') ."/javascripts/app.js",array('jquery'),'1.2.3',true); 
    wp_enqueue_script('superfish',get_bloginfo('template_url') ."/javascripts/superfish.js",array('jquery'),'1.2.3',true); 
    wp_enqueue_script('formalize',get_bloginfo('template_url') ."/javascripts/jquery.formalize.min.js",array('jquery'),'1.2.3',true); 
} 

아이 테마 :

add_action('wp_enqueue_scripts','child_add_javascripts',20); //This will execute the child_add_javascripts callback after the st_header_scripts callback 
function child_add_javascripts(){ 
    wp_enqueue_script('child_javascript',get_bloginfo('stylesheet_directory') ."/javascripts/child_js.js",array('jquery'),'1.2.3',true); //This looks in the CHLID theme's directory while template_url looks in the parent theme's directory 
} 

그것은 모든 다른 핵심 행동과 필터에 대한 탄탄한 이해를 얻기 위해 나에게 조금했다 , 일단 당신이 그것에 익숙해지고 당신의 주제의 모든 필요에 그것을 활용하면, 그들은 매우 힘이됩니다. 난 도구.

는이

0

그래서, 당신은 단지 JS 파일을 추가하여 JS 파일을 후크와 자식 테마 기능을 가지고 부모 테마에서 원하는 도움이되는지 알려주세요?

코드가 다소 엉성합니다.나는

  1. 스크립트, wp_enqueue_scripts에 부착해야한다하지 init
  2. 그래서 변수에 할당 값 (심지어 NULL이 아닌 - D)를 반환하지 않습니다 wp_enqueue_script 쓸모
  3. 에주의입니다에게 설명하여 자신의 첫 번째 매개 변수는

동일하게 그리고 여기에 코드해야하므로 필터의 이름, 나는 부모 테마에,이, 당신이 원하는, 가정 것을, 함께 add_filterapply_filters "일을"기능 자바 스크립트 파일 배열을 설정하여 대기열에 넣고 대기열에 넣기 만하면됩니다.

function st_header_scripts() { 

    /** 
    * 'jquery' has FALSE value, since it is registered in wordpress and you do not need an url 
    */ 
    $js_files = array(
     'jquery' => false, 
     'custom' => get_bloginfo('template_url') ."/javascripts/app.js", 
     'superfish' => get_bloginfo('template_url') ."/javascripts/superfish.js", 
     'formalize' =>get_bloginfo('template_url') ."/javascripts/jquery.formalize.min.js" 
    ); 

    /** 
    * Here you create a variable with a possibility to be filtered 
    * The first argument is your custom name of your filter 
    * The second argument is a variable which might be modified with filter and assigned to the $javascripts variable 
    */ 
    $javascripts = apply_filters('header_javascripts', $js_files); 

    /** 
    * Here you just enqueue your scripts 
    */ 
    if(false != $javascripts) 
     foreach($javascripts as $name => $uri) 
      if(!$uri) 
       wp_enqueue_script($name); 
      else 
       wp_enqueue_script($name, $uri, array('jquery'), '1.2.3', true); 
} 
add_action('wp_enqueue_scripts', 'st_header_scripts'); 

/** 
* A callback for a filter `header_javascripts` 
* @param array $original_js array of JS files from parent theme 
* You can either add JS into array or create and return new array 
*/ 
function children_theme_js($original_js) { 

    //adding to original array 
    $original_js[] = 'jquery-ui-core'; 

    //not enqueueing JS from parent theme 
    //$original_js = array(); 
    //$original_js['script'] = 'I-am-the-url'; 

    //you need to return an array into the filter 
    return $original_js; 
} 
add_filter('header_javascripts','children_theme_js'); 
관련 문제