Jump to content

Php - hide form field based on user role


fabianschultz

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.