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

Solutions

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)

Tags: PHP / Wordpress

Similar questions

Simplified version of if-conditional statement with array and don't have to declare array[0] | array[1] | array[2]?
These codes are already working and I just want to simplify it. This is a query of a custom post type called "File Upload". I'm using ACF fronted form to create these posts and has the function to select which category can the post be seen. My condition here is if the "current post" has a category that matches the category set to display the file u...
Get value of string; find this string in content; apply new style to this string thanks to jQuery
The answer seems to be simple, but I can't find the solution. I have a WordPress site. In my right column, I print some meta data. I want to get the text value of some data, then find it in my .entry-content, and apply the new style to this exact string. I can cope with this task by applying the new style to the entire content inside.entry-content,...
Check if $var = anything in an array?
I need to be able to test if $post_count is equal to any number in a given array. Here is an example of what I am trying to achieve: -- -- I have been able to make this work using the or operator but I end up with very long blocks of code..... I am guessing there is a simpler way to do this but I am new to PHP so I am not sure! Any help would be gr...
Write a custom redirect for asp.net that parses the url and check if url contains a string in a string array length = 1000
I am have an ASP.NET website, actually Umbraco, but don't think that relates to my question at all, and a related Wordpress blog. The Wordpress blog used to be the parent website so all the urls are in the following format: I have moved the blog to blog.mywebsite.com so the new url will be: So all the old seo url mappings will be hitting the new as...
Array item of type string does not work the same as string literal of the same value?
I have an array $pieces[] that is an exploded URL, on which I perform some unsetting to drill it down to one value in the zeroth place. I store the value of $pieces[0] in $category. Let's say this value is the string "page". I pass $category to another array that contains various arguments for a function. In this array, if I pass "page" as a string...
update_option is not saving an array, but saving the string 'Array'
I have come across quite an odd problem when using the update_option function, and I am sure that I've tried everything I can think of to get it working, but unfortunately it isn't. I'm using AJAX to submit a form (using the popular method described here) in the backend, on a custom options page. Everything is working, except for the update_option ...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.