dimitrilongo
dimitrilongo

Reputation: 45

how to add string var in array

here's my function i'd like to replace : array('administrator','author') with a var like array($string) So i have an array from my theme options (from a select field), which return the user role i need in my function, it returns like this :

 Array ( [0] => administrator [1] => author [2] => subscriber )

I did :

 $string = "'".implode("','",$array)."'";

my function:

add_filter("login_redirect", "subscriber_login_redirect", 10, 3);

function subscriber_login_redirect($redirect_to, $request, $user) {

    if(is_array($user->roles))
      if ( count( array_diff( $user->roles, array($string) ) ) !== count( $user->roles ) ) return site_url('/wp-admin/')

    return home_url();
}

But still issue,in my consolelog, the string var returns 'administrator','author', 'subscriber'

Thank you for help

Upvotes: 0

Views: 42

Answers (1)

Xavjer
Xavjer

Reputation: 9254

Use explode to create an array out of a string

Array ( [0] => administrator [1] => author [2] => subscriber )

$string = implode(",",$array);

add_filter("login_redirect", "subscriber_login_redirect", 10, 3);

function subscriber_login_redirect($redirect_to, $request, $user) {

    if(is_array($user->roles))
      if ( count( array_diff( $user->roles, explode(",",$string) ) ) !== count( $user->roles ) ) return site_url('/wp-admin/')

    return home_url();
}

The "," may not be a good delimitter, use something that will most likely be unique like a pipe | or maybe even a stringcombination (like !|! for example)

Upvotes: 0

Related Questions