fabianschultz Posted June 25, 2022 Share Posted June 25, 2022 Hello, I am trying to hide a specific search form field based on Wordpress user role but I am missing somehting, it does not hide the field: add_filter ('bps_before_search_form_filter' , 'hide_field_19_wrap', 10, 1); $array1 = array(field_19_wrap, field_28_match_any_wrap, field_34_distance_wrap); function hide_field_19_wrap ($field_ids){ global $current_user; $user_roles = $current_user->roles; if (!current_user_can('subscriber')) { unset($array1[field_19_wrap]); } Return $field_ids; } Quote Link to comment https://forums.phpfreaks.com/topic/314962-php-hide-form-field-based-on-user-role/ Share on other sites More sharing options...
maxxd Posted June 25, 2022 Share Posted June 25, 2022 So, it looks like there are a lot of things going on here. First off, when you create an array you want to wrap the string values in quotes. This alone should be throwing an error about undefined constants. Now, inside your function you refer to the array you created outside the function. This should also be throwing an error about an undefined variable - the $array1 array doesn't exist inside that function. Read up on variable scope - it's an important concept. What you need to do is see what exactly the $field_ids parameter contains and what the values correspond to. You can do that by adding this right after the opening bracket in the function signature: die("<pre>".var_export($field_ids, true)."</pre>"); Then remove the appropriate value from the $field_ids array (not $array1 - that doesn't exist in this function, remember) before you return it. Most importantly, on your development machine open the file 'wp-config.php'. Look for a line that reads `define( 'WP_DEBUG', false );` (in the sample file it's on line 82) and change it to read `define( 'WP_DEBUG', true );`. This will let you know when errors like the above happen. Quote Link to comment https://forums.phpfreaks.com/topic/314962-php-hide-form-field-based-on-user-role/#findComment-1597619 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.