Why does the Fatal error appear: Cannot redeclare exc_post () when if ($ _SESSION ['filter1'] == '')?












0















I am writing a tag filtering plugin for wordpress but for some reason I get an error



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


On line



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


And the most curious thing is that on the exact same line only



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


There are no errors
And it only occurs when



 ['filter2'] == '' 


for all other values, everything works fine
If, instead of skipping, I indicate 1, then the error appears already on filter4 jumping filter3



Here is the entire plugin code.



      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;
}

}


the code has been simplified.










share|improve this question


















  • 1





    because it was "previously declared" ....

    – user10226920
    Nov 15 '18 at 22:05






  • 3





    you have function exc_post(... in the file above six times, you cant reuse the same function name.

    – user10226920
    Nov 15 '18 at 22:10






  • 1





    In particular if the flags overlap. Try to clean up the spaghetti code, and abstract the function if it only differs ever so slightly.

    – mario
    Nov 15 '18 at 22:11
















0















I am writing a tag filtering plugin for wordpress but for some reason I get an error



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


On line



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


And the most curious thing is that on the exact same line only



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


There are no errors
And it only occurs when



 ['filter2'] == '' 


for all other values, everything works fine
If, instead of skipping, I indicate 1, then the error appears already on filter4 jumping filter3



Here is the entire plugin code.



      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;
}

}


the code has been simplified.










share|improve this question


















  • 1





    because it was "previously declared" ....

    – user10226920
    Nov 15 '18 at 22:05






  • 3





    you have function exc_post(... in the file above six times, you cant reuse the same function name.

    – user10226920
    Nov 15 '18 at 22:10






  • 1





    In particular if the flags overlap. Try to clean up the spaghetti code, and abstract the function if it only differs ever so slightly.

    – mario
    Nov 15 '18 at 22:11














0












0








0








I am writing a tag filtering plugin for wordpress but for some reason I get an error



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


On line



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


And the most curious thing is that on the exact same line only



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


There are no errors
And it only occurs when



 ['filter2'] == '' 


for all other values, everything works fine
If, instead of skipping, I indicate 1, then the error appears already on filter4 jumping filter3



Here is the entire plugin code.



      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;
}

}


the code has been simplified.










share|improve this question














I am writing a tag filtering plugin for wordpress but for some reason I get an error



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


On line



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


And the most curious thing is that on the exact same line only



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


There are no errors
And it only occurs when



 ['filter2'] == '' 


for all other values, everything works fine
If, instead of skipping, I indicate 1, then the error appears already on filter4 jumping filter3



Here is the entire plugin code.



      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;
}

}


the code has been simplified.







php wordpress






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 22:02









zeni1agentzeni1agent

356




356








  • 1





    because it was "previously declared" ....

    – user10226920
    Nov 15 '18 at 22:05






  • 3





    you have function exc_post(... in the file above six times, you cant reuse the same function name.

    – user10226920
    Nov 15 '18 at 22:10






  • 1





    In particular if the flags overlap. Try to clean up the spaghetti code, and abstract the function if it only differs ever so slightly.

    – mario
    Nov 15 '18 at 22:11














  • 1





    because it was "previously declared" ....

    – user10226920
    Nov 15 '18 at 22:05






  • 3





    you have function exc_post(... in the file above six times, you cant reuse the same function name.

    – user10226920
    Nov 15 '18 at 22:10






  • 1





    In particular if the flags overlap. Try to clean up the spaghetti code, and abstract the function if it only differs ever so slightly.

    – mario
    Nov 15 '18 at 22:11








1




1





because it was "previously declared" ....

– user10226920
Nov 15 '18 at 22:05





because it was "previously declared" ....

– user10226920
Nov 15 '18 at 22:05




3




3





you have function exc_post(... in the file above six times, you cant reuse the same function name.

– user10226920
Nov 15 '18 at 22:10





you have function exc_post(... in the file above six times, you cant reuse the same function name.

– user10226920
Nov 15 '18 at 22:10




1




1





In particular if the flags overlap. Try to clean up the spaghetti code, and abstract the function if it only differs ever so slightly.

– mario
Nov 15 '18 at 22:11





In particular if the flags overlap. Try to clean up the spaghetti code, and abstract the function if it only differs ever so slightly.

– mario
Nov 15 '18 at 22:11












1 Answer
1






active

oldest

votes


















1














If that's your simplified version, I don't want to know how the original version looked like. Nevertheless - the whole problem is, that you defined the function exc_post two times - one time for each filter, but each time exactly the same way. An easy solution would be to rename the functions for the first filter to exc_post1 and the other to exc_post2.



That would work, but still isn't a good solution, as the main part of your logic is duplicated 6 times. So each time you change your function, you have to change 6 places instead of one, which is a very bad practice.



It would be better to define the filter function one generic time and reuse them, as only the filename changes and everything else is the same.



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





share|improve this answer


























  • Yes. That would be a great solution. Only it gives me an error Warning: Illegal offset type in isset or empty in C:xampphtdocsworwp-includesclass-wp-hook.php on line 75 Warning: Illegal offset type in C:xampphtdocsworwp-includesclass-wp-hook.php on line 77 Warning: Illegal offset type in isset or empty in C:xampphtdocsworwp-includesclass-wp-hook.php on line 75 Warning: Illegal offset type in C:xampphtdocsworwp-includesclass-wp-hook.php on line 77

    – zeni1agent
    Nov 16 '18 at 10:30













  • What's your wordpress version?

    – Philipp
    Nov 16 '18 at 11:43











  • I have a version 4.9.8

    – zeni1agent
    Nov 16 '18 at 13:17






  • 1





    maybe this is because: 3 parameter ($priority) should not be a function but a number

    – zeni1agent
    Nov 16 '18 at 13:42













  • I think I was able to change it slightly, but it only applies to either filter1 or filter2 is there a way to apply filters at the same time add_filter('pre_get_posts',function($filter) use ($file) { return My_filter_post($filter, $file); });

    – zeni1agent
    Nov 16 '18 at 17:19











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53328520%2fwhy-does-the-fatal-error-appear-cannot-redeclare-exc-post-when-if-sessio%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














If that's your simplified version, I don't want to know how the original version looked like. Nevertheless - the whole problem is, that you defined the function exc_post two times - one time for each filter, but each time exactly the same way. An easy solution would be to rename the functions for the first filter to exc_post1 and the other to exc_post2.



That would work, but still isn't a good solution, as the main part of your logic is duplicated 6 times. So each time you change your function, you have to change 6 places instead of one, which is a very bad practice.



It would be better to define the filter function one generic time and reuse them, as only the filename changes and everything else is the same.



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





share|improve this answer


























  • Yes. That would be a great solution. Only it gives me an error Warning: Illegal offset type in isset or empty in C:xampphtdocsworwp-includesclass-wp-hook.php on line 75 Warning: Illegal offset type in C:xampphtdocsworwp-includesclass-wp-hook.php on line 77 Warning: Illegal offset type in isset or empty in C:xampphtdocsworwp-includesclass-wp-hook.php on line 75 Warning: Illegal offset type in C:xampphtdocsworwp-includesclass-wp-hook.php on line 77

    – zeni1agent
    Nov 16 '18 at 10:30













  • What's your wordpress version?

    – Philipp
    Nov 16 '18 at 11:43











  • I have a version 4.9.8

    – zeni1agent
    Nov 16 '18 at 13:17






  • 1





    maybe this is because: 3 parameter ($priority) should not be a function but a number

    – zeni1agent
    Nov 16 '18 at 13:42













  • I think I was able to change it slightly, but it only applies to either filter1 or filter2 is there a way to apply filters at the same time add_filter('pre_get_posts',function($filter) use ($file) { return My_filter_post($filter, $file); });

    – zeni1agent
    Nov 16 '18 at 17:19
















1














If that's your simplified version, I don't want to know how the original version looked like. Nevertheless - the whole problem is, that you defined the function exc_post two times - one time for each filter, but each time exactly the same way. An easy solution would be to rename the functions for the first filter to exc_post1 and the other to exc_post2.



That would work, but still isn't a good solution, as the main part of your logic is duplicated 6 times. So each time you change your function, you have to change 6 places instead of one, which is a very bad practice.



It would be better to define the filter function one generic time and reuse them, as only the filename changes and everything else is the same.



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





share|improve this answer


























  • Yes. That would be a great solution. Only it gives me an error Warning: Illegal offset type in isset or empty in C:xampphtdocsworwp-includesclass-wp-hook.php on line 75 Warning: Illegal offset type in C:xampphtdocsworwp-includesclass-wp-hook.php on line 77 Warning: Illegal offset type in isset or empty in C:xampphtdocsworwp-includesclass-wp-hook.php on line 75 Warning: Illegal offset type in C:xampphtdocsworwp-includesclass-wp-hook.php on line 77

    – zeni1agent
    Nov 16 '18 at 10:30













  • What's your wordpress version?

    – Philipp
    Nov 16 '18 at 11:43











  • I have a version 4.9.8

    – zeni1agent
    Nov 16 '18 at 13:17






  • 1





    maybe this is because: 3 parameter ($priority) should not be a function but a number

    – zeni1agent
    Nov 16 '18 at 13:42













  • I think I was able to change it slightly, but it only applies to either filter1 or filter2 is there a way to apply filters at the same time add_filter('pre_get_posts',function($filter) use ($file) { return My_filter_post($filter, $file); });

    – zeni1agent
    Nov 16 '18 at 17:19














1












1








1







If that's your simplified version, I don't want to know how the original version looked like. Nevertheless - the whole problem is, that you defined the function exc_post two times - one time for each filter, but each time exactly the same way. An easy solution would be to rename the functions for the first filter to exc_post1 and the other to exc_post2.



That would work, but still isn't a good solution, as the main part of your logic is duplicated 6 times. So each time you change your function, you have to change 6 places instead of one, which is a very bad practice.



It would be better to define the filter function one generic time and reuse them, as only the filename changes and everything else is the same.



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





share|improve this answer















If that's your simplified version, I don't want to know how the original version looked like. Nevertheless - the whole problem is, that you defined the function exc_post two times - one time for each filter, but each time exactly the same way. An easy solution would be to rename the functions for the first filter to exc_post1 and the other to exc_post2.



That would work, but still isn't a good solution, as the main part of your logic is duplicated 6 times. So each time you change your function, you have to change 6 places instead of one, which is a very bad practice.



It would be better to define the filter function one generic time and reuse them, as only the filename changes and everything else is the same.



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






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 16 '18 at 15:48

























answered Nov 15 '18 at 23:16









PhilippPhilipp

13.5k22141




13.5k22141













  • Yes. That would be a great solution. Only it gives me an error Warning: Illegal offset type in isset or empty in C:xampphtdocsworwp-includesclass-wp-hook.php on line 75 Warning: Illegal offset type in C:xampphtdocsworwp-includesclass-wp-hook.php on line 77 Warning: Illegal offset type in isset or empty in C:xampphtdocsworwp-includesclass-wp-hook.php on line 75 Warning: Illegal offset type in C:xampphtdocsworwp-includesclass-wp-hook.php on line 77

    – zeni1agent
    Nov 16 '18 at 10:30













  • What's your wordpress version?

    – Philipp
    Nov 16 '18 at 11:43











  • I have a version 4.9.8

    – zeni1agent
    Nov 16 '18 at 13:17






  • 1





    maybe this is because: 3 parameter ($priority) should not be a function but a number

    – zeni1agent
    Nov 16 '18 at 13:42













  • I think I was able to change it slightly, but it only applies to either filter1 or filter2 is there a way to apply filters at the same time add_filter('pre_get_posts',function($filter) use ($file) { return My_filter_post($filter, $file); });

    – zeni1agent
    Nov 16 '18 at 17:19



















  • Yes. That would be a great solution. Only it gives me an error Warning: Illegal offset type in isset or empty in C:xampphtdocsworwp-includesclass-wp-hook.php on line 75 Warning: Illegal offset type in C:xampphtdocsworwp-includesclass-wp-hook.php on line 77 Warning: Illegal offset type in isset or empty in C:xampphtdocsworwp-includesclass-wp-hook.php on line 75 Warning: Illegal offset type in C:xampphtdocsworwp-includesclass-wp-hook.php on line 77

    – zeni1agent
    Nov 16 '18 at 10:30













  • What's your wordpress version?

    – Philipp
    Nov 16 '18 at 11:43











  • I have a version 4.9.8

    – zeni1agent
    Nov 16 '18 at 13:17






  • 1





    maybe this is because: 3 parameter ($priority) should not be a function but a number

    – zeni1agent
    Nov 16 '18 at 13:42













  • I think I was able to change it slightly, but it only applies to either filter1 or filter2 is there a way to apply filters at the same time add_filter('pre_get_posts',function($filter) use ($file) { return My_filter_post($filter, $file); });

    – zeni1agent
    Nov 16 '18 at 17:19

















Yes. That would be a great solution. Only it gives me an error Warning: Illegal offset type in isset or empty in C:xampphtdocsworwp-includesclass-wp-hook.php on line 75 Warning: Illegal offset type in C:xampphtdocsworwp-includesclass-wp-hook.php on line 77 Warning: Illegal offset type in isset or empty in C:xampphtdocsworwp-includesclass-wp-hook.php on line 75 Warning: Illegal offset type in C:xampphtdocsworwp-includesclass-wp-hook.php on line 77

– zeni1agent
Nov 16 '18 at 10:30







Yes. That would be a great solution. Only it gives me an error Warning: Illegal offset type in isset or empty in C:xampphtdocsworwp-includesclass-wp-hook.php on line 75 Warning: Illegal offset type in C:xampphtdocsworwp-includesclass-wp-hook.php on line 77 Warning: Illegal offset type in isset or empty in C:xampphtdocsworwp-includesclass-wp-hook.php on line 75 Warning: Illegal offset type in C:xampphtdocsworwp-includesclass-wp-hook.php on line 77

– zeni1agent
Nov 16 '18 at 10:30















What's your wordpress version?

– Philipp
Nov 16 '18 at 11:43





What's your wordpress version?

– Philipp
Nov 16 '18 at 11:43













I have a version 4.9.8

– zeni1agent
Nov 16 '18 at 13:17





I have a version 4.9.8

– zeni1agent
Nov 16 '18 at 13:17




1




1





maybe this is because: 3 parameter ($priority) should not be a function but a number

– zeni1agent
Nov 16 '18 at 13:42







maybe this is because: 3 parameter ($priority) should not be a function but a number

– zeni1agent
Nov 16 '18 at 13:42















I think I was able to change it slightly, but it only applies to either filter1 or filter2 is there a way to apply filters at the same time add_filter('pre_get_posts',function($filter) use ($file) { return My_filter_post($filter, $file); });

– zeni1agent
Nov 16 '18 at 17:19





I think I was able to change it slightly, but it only applies to either filter1 or filter2 is there a way to apply filters at the same time add_filter('pre_get_posts',function($filter) use ($file) { return My_filter_post($filter, $file); });

– zeni1agent
Nov 16 '18 at 17:19




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53328520%2fwhy-does-the-fatal-error-appear-cannot-redeclare-exc-post-when-if-sessio%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine