2013-11-14 49 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不是一个返回函数,所以你没有理由将它设置为一个变量。它的作用是在header.php中调用wp_head()后生成所有脚本标记。

其次,问题源于你使用add_filter和apply_filter。但我认为,我们应该通过实际的差别是操作和过滤器之间有什么(你可能会知道,但其他人可能没有):

操作依赖于它接收
过滤器中的数据做的东西做的东西到数据接收和返回它

do_action()apply_filter()是采取触发器名称作为其第一个参数的触发功能和参数,你想传递给回调,因为它是第2次,第n个参数。

add_action()add_filter()是您的侦听程序,它在第一个参数中查找已定义的名称,然后执行第二个参数中定义的回调函数。

鉴于您的情况,您最好使用动作钩子的第三个参数优先处理动作钩子。

母题:

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 
} 

我花了一点,以获得在所有不同的核心操作和过滤器扎实抓好,但是一旦你习惯了它并将其用于所有主题的需求,它们就会变得非常强大l工具。

让我知道,如果这有助于

0

所以,你在父母的主题想有一个功能,即挂钩您的JS文件和子主题只添加JS文件?

你的代码有点乱。我解释

  1. 脚本应该勾搭成wp_enqueue_scripts,不init
  2. wp_enqueue_script不返回任何值(甚至未NULL :-D),所以它赋值给一个变量是无用的
  3. 注意你过滤器的命名,功能add_filterapply_filters“工作”起来,所以他们的第一个参数是相同

这里是代码,我认为,你想,在父母的主题,有创建了一个函数,进行入队,并通过儿童主题,你只需设置一个javascript文件的数组来挂钩和入队。

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