0

This doest not works:

Warning: in_array() expects parameter 2 to be array, null given

$non_area_page = array(160951,160955); // This does not work

add_filter( 'term_link',change_term_name , 10, 3 );  
function change_term_name( $termlink, $term, $taxonomy )
{
   /*If on page */
    if(is_page()){
        $current_id = get_queried_object_id();
        $current_post = get_post($current_id);
        $slug = $current_post->post_name; 
        if(!in_array($current_id,$non_area_page)) // array is defined in start of code above
        $termlink = esc_url( add_query_arg( ['tag' => $slug], $termlink ) );

    }          
    return  $termlink;
}

Below code works: (Only difference is array is not defined outside function)

add_filter( 'term_link',change_term_name , 10, 3 );
function change_term_name( $termlink, $term, $taxonomy )
{
   /*If on page */
    if(is_page()){
        $current_id = get_queried_object_id();
        $current_post = get_post($current_id);
        $slug = $current_post->post_name; 
        if(!in_array($current_id,array(160951,160955))) // directly passed array
        $termlink = esc_url( add_query_arg( ['tag' => $slug], $termlink ) );

    }        
    return  $termlink;
}

Is that is related to defining array globally, if so how we do that In WordPress, Please suggest ?

Thanks!

1 Answer 1

1

You cannot define something outside a function and then simply try to use it inside the function without calling it into the function. This is basic PHP and how functions work in general.

You need to pass that specific something to the function or define that something inside the function or use a method to globalize that something and then invoking it inside the function

On the topic of "globalizing", never ever use global variables to globalise a value. WordPress has already made such a big mess of it. Globals are evil, period. If you need to globalize something, use a function. For example

funtion get_global_array()
{
    // Return an array with values 1, 2 and 3
    return [1, 2, 3];
}

You can now use get_global_array() anywhere where needed like inside of another function or filter function

1
  • 1
    Yes thanks, it was silly question by me in hurry for work. Sorry for bothering and thanks for explaining about "Globals" :)
    – jas
    Commented Nov 16, 2015 at 10:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.