为什么会出现致命错误:if($ _SESSION ['filter1'] =='')时,无法重新声明exc_post()?

时间:2018-11-15 22:02:44

标签: php wordpress

我正在为wordpress写一个标签过滤插件,但是由于某种原因我遇到了错误

Fatal error: Cannot redeclare exc_post () (previously declared C: \ xampp \ htdocs \ wor \ wp-content \ plugins \ GOG11 \ GOG11.php: 64) in C: \ xampp \ htdocs \ wor \ wp-content \ plugins \ GOG11 \ GOG11.php on line 141

在线

 if ($ _SESSION ['filter2'] == '') {

最奇怪的是,仅在同一行上

 if ($ _SESSION ['filter1'] == '') {

没有错误 而且仅在

 ['filter2'] == '' 

对于所有其他值,一切正常 如果我不是跳过,而是表示1,则该错误已经出现在filter4跳转filter3上

这是整个插件代码。

      session_start();
      if ($_POST['filter1']){   
    $_SESSION['filter1'] = $_POST['filter1'];
    }

      if ($_POST['filter2']){   
    $_SESSION['filter2'] = $_POST['filter2'];
    }

     add_shortcode ('wp-r_dop_filter','A2'); 
    function A2(){




        $R1='

<form method="POST" >
<p>Option filter</p>

<p>Filter 1</p>
level 1:  <input type="radio" name="filter1" value="1"' . ($_SESSION["filter1"] == 1 ? 'checked' : '') . '/> 
level 2:  <input type="radio" name="filter1" value="2"' . ($_SESSION["filter1"] == 2 ? 'checked' : '') . '/> 

<p>Filter 2</p>
level 1:  <input type="radio" name="filter2" value="1"' .  ($_SESSION["filter2"] == 1 ? 'checked' : '') .  '/> 
level 2:  <input type="radio" name="filter2" value="2"' .  ($_SESSION["filter2"] == 2 ? 'checked' : '') .  '/> 



<p><input type="submit" value="User"></p>
</form>
       ';

       return  $R1;
    }











if ($_SESSION['filter1'] == ''){

add_filter('pre_get_posts','exc_post'); 
function exc_post($filter1_query_lv0 ) {
    if ($filter1_query_lv0->is_category || ($filter1_query_lv0->is_home || ($filter1_query_lv0->is_feed) || ($filter1_query_lv0->is_tag)))  
    {
$filter1_way_lv0_1 = plugin_dir_url( __FILE__ );  
$filter1_way_lv0_2 = file_get_contents( $filter1_way_lv0_1 . 'filter1/level_1.txt');   
$filter1_way_lv0_3 = explode (",", $filter1_way_lv0_2);  



        $filter1_query_lv0->set('tax_query', array(  
            array(
                'taxonomy' => 'post_tag',
                'field' => 'slug',
                'terms' =>  $filter1_way_lv0_3,  
                'operator' => 'NOT IN'
            )
        ));
    }
    return $filter1_query_lv0;
}
}
else if ($_SESSION['filter1'] == 1){

add_filter('pre_get_posts','exc_post'); 
function exc_post($filter1_query_lv1) {
    if ($filter1_query_lv1->is_category || ($filter1_query_lv1->is_home || ($filter1_query_lv1->is_feed) || ($filter1_query_lv1->is_tag)))  
    {
$filter1_way_lv1_1 = plugin_dir_url( __FILE__ );  
$filter1_way_lv1_2 = file_get_contents( $filter1_way_lv1_1 . 'filter1/level_1.txt');   
$filter1_way_lv1_3 = explode (",", $filter1_way_lv1_2);  



        $filter1_query_lv1->set('tax_query', array(  
            array(
                'taxonomy' => 'post_tag',
                'field' => 'slug',
                'terms' =>  $filter1_way_lv1_3,  
                'operator' => 'NOT IN'
            )
        ));
    }
    return $filter1_query_lv1;
}

}
else if ($_SESSION['filter1'] == 2){

add_filter('pre_get_posts','exc_post'); 
function exc_post($filter1_query_lv2) {
    if ($filter1_query_lv2->is_category || ($filter1_query_lv2->is_home || ($filter1_query_lv2->is_feed) || ($filter1_query_lv2->is_tag)))  
    {
$filter1_way_lv2_1 = plugin_dir_url( __FILE__ );  
$filter1_way_lv2_2 = file_get_contents( $filter1_way_lv2_1 . 'filter1/level_2.txt');   
$filter1_way_lv2_3 = explode (",", $filter1_way_lv2_2);  



        $filter1_query_lv2->set('tax_query', array(  
            array(
                'taxonomy' => 'post_tag',
                'field' => 'slug',
                'terms' =>  $filter1_way_lv2_3,  
                'operator' => 'NOT IN'
            )
        ));
    }
    return $filter1_query_lv2;
}

}   



if ($_SESSION['filter2'] == 1){

add_filter('pre_get_posts','exc_post'); 
function exc_post($filter2_query_lv0) {
    if ($filter2_query_lv0->is_category || ($filter2_query_lv0->is_home || ($filter2_query_lv0->is_feed) || ($filter2_query_lv0->is_tag)))  
    {
$filter2_way_lv0_1 = plugin_dir_url( __FILE__ );  
$filter2_way_lv0_2 = file_get_contents( $filter2_way_lv0_1 . 'filter2/level_1.txt');   
$filter2_way_lv0_3 = explode (",", $filter2_way_lv0_2);  



        $filter2_query_lv0->set('tax_query', array(  
            array(
                'taxonomy' => 'post_tag',
                'field' => 'slug',
                'terms' =>  $filter2_way_lv0_3,  
                'operator' => 'NOT IN'
            )
        ));
    }
    return $filter2_query_lv0;
}
}
else if ($_SESSION['filter2'] == 2){

add_filter('pre_get_posts','exc_post'); 
function exc_post($filter2_query_lv1) {
    if ($filter2_query_lv1->is_category || ($filter2_query_lv1->is_home || ($filter2_query_lv1->is_feed) || ($filter2_query_lv1->is_tag)))  
    {
$filter2_way_lv1_1 = plugin_dir_url( __FILE__ );  
$filter2_way_lv1_2 = file_get_contents( $filter2_way_lv1_1 . 'filter2/level_1.txt');   
$filter2_way_lv1_3 = explode (",", $filter2_way_lv1_2);  



        $filter2_query_lv1->set('tax_query', array(  
            array(
                'taxonomy' => 'post_tag',
                'field' => 'slug',
                'terms' =>  $filter2_way_lv1_3,  
                'operator' => 'NOT IN'
            )
        ));
    }
    return $filter2_query_lv1;
}

}
else if ($_SESSION['filter2'] == ''){

add_filter('pre_get_posts','exc_post'); 
function exc_post($filter2_query_lv2) {
    if ($filter2_query_lv2->is_category || ($filter2_query_lv2->is_home || ($filter2_query_lv2->is_feed) || ($filter2_query_lv2->is_tag)))  
    {
$filter2_way_lv2_1 = plugin_dir_url( __FILE__ );  
$filter2_way_lv2_2 = file_get_contents( $filter2_way_lv2_1 . 'filter2/level_2.txt');   
$filter2_way_lv2_3 = explode (",", $filter2_way_lv2_2);  



        $filter2_query_lv2->set('tax_query', array(  
            array(
                'taxonomy' => 'post_tag',
                'field' => 'slug',
                'terms' =>  $filter2_way_lv2_3,  
                'operator' => 'NOT IN'
            )
        ));
    }
    return $filter2_query_lv2;
}

}

代码已简化。

1 个答案:

答案 0 :(得分:1)

如果这是您的简化版本,那么我不想知道原始版本的样子。不过,整个问题是,您两次定义了函数exc_post-每个过滤器定义一次,但是每次定义的方式都完全相同。一种简单的解决方案是将第一个过滤器的功能重命名为exc_post1,将另一个过滤器的功能重命名为exc_post2

那可以,但是仍然不是一个好的解决方案,因为逻辑的主要部分重复了6次。因此,每次更改功能时,都必须更改6个位置而不是一个位置,这是一个非常糟糕的做法。

最好只定义一次过滤器函数并重用它们,因为只有文件名更改而其他所有内容都相同。

function exc_post($filter, $file) {
    if ($filter->is_category || ($filter->is_home || ($filter->is_feed) || ($filter->is_tag)))
    {
        $dir = plugin_dir_url( __FILE__ );
        $content = file_get_contents( $dir . $file);
        $terms = explode (",", $content);

        $filter->set('tax_query', array(
            array(
                'taxonomy' => 'post_tag',
                'field' => 'slug',
                'terms' =>  $terms,
                'operator' => 'NOT IN'
            )
        ));
    }
    return $filter;
}

if ($_SESSION['filter1'] == '') {
    $file = 'filter1/level_1.txt';
} else if ($_SESSION['filter1'] == 1) {
    $file = 'filter1/level_1.txt';
} else if ($_SESSION['filter1'] == 2) {
    $file = 'filter1/level_2.txt';
}

add_filter('pre_get_posts', function($filter) use ($file) {
    return exc_post($filter, $file);
}, 0);


if ($_SESSION['filter2'] == 1){
    $file = 'filter2/level_1.txt';
} else if ($_SESSION['filter2'] == 2){
    $file = 'filter2/level_1.txt';
} else if ($_SESSION['filter2'] == ''){
    $file = 'filter2/level_2.txt';
}

add_filter('pre_get_posts', function($filter) use ($file) {
    return exc_post($filter, $file);
}, 0);