unset item from array and update wp option

I have a series of checkboxes, each of which triggers an ajax action in WordPress. It sends an id number and the state of the checkbox to my php function. The function checks two wp_options. If the checkbox is checked, it looks for the id number in one option’s array and if it’s not in the array, it adds it. It also looks for the id number in the second option’s array and if it is in the array, it’s supposed to unset it. If the checkbox is unchecked, it does the opposite.

It’s successfully adding items to the first option’s array, but I can’t get it to unset the item from the second option’s array. It returns “error” every time. Here’s the function, minus the nonce info:

Read More
function my_required_fields(){
$field = $_POST['field'];       
$checked = $_POST['checked'];
$required_fields = get_option('ghsc_required_fields') ? unserialize(get_option('ghsc_required_fields')) : array();
$optional_fields = get_option('ghsc_optional_fields') ? unserialize(get_option('ghsc_optional_fields')) : array();  
if($checked === 'yes'):
    if(!in_array($field, $required_fields)): $required_fields[] = $field; endif;
    if(in_array($field, $optional_fields)): unset($optional_fields[$field]); endif;     
elseif($checked === 'no'):
    if(!in_array($field, $optional_fields)): $optional_fields[] = $field; endif;
    if(in_array($field, $required_fields)): unset($required_fields[$field]); $required_fields = array_values($required_fields); endif;      
endif;
$update_required = update_option('ghsc_required_fields', serialize($required_fields));
$update_optional = update_option('ghsc_optional_fields', serialize($optional_fields));  
$response = ($update_required && $update_optional) ? 'success' : 'error';
$response = json_encode($response); header( "Content-Type: application/json" ); echo $response; exit;
}

Any idea what I’m doing wrong?

Related posts

Leave a Reply

1 comment

  1. I worked it out. Needed to use array_keys (or an alternative) to unset the key, rather than the value, and also needed to check to see if the value of the option had changed, because if it hadn’t it would return false. See below:

    function my_required_fields(){
    $field = $_POST['field'];       
    $checked = $_POST['checked'];
    $required_fields = get_option('ghsc_required_fields') ? unserialize(get_option('ghsc_required_fields')) : array();
    $optional_fields = get_option('ghsc_optional_fields') ? unserialize(get_option('ghsc_optional_fields')) : array();  
    if($checked === 'yes'):
        if(!in_array($field, $required_fields)): $required_fields[] = $field; endif;
        if(in_array($field, $optional_fields)): foreach(array_keys($optional_fields, $field, true) as $key) unset($optional_fields[$key]); endif;       
    elseif($checked === 'no'):
        if(!in_array($field, $optional_fields)): $optional_fields[] = $field; endif;
        if(in_array($field, $required_fields)): foreach(array_keys($required_fields, $field, true) as $key) unset($required_fields[$key]); endif;       
    endif;
    $update_required = (get_option('ghsc_required_fields') === serialize($required_fields) ? 1 : update_option('ghsc_required_fields', serialize($required_fields)));
    $update_optional = (get_option('ghsc_optional_fields') === serialize($optional_fields) ? 1 : update_option('ghsc_optional_fields', serialize($optional_fields)));   
    $response = ($update_required && $update_optional) ? 'success' : 'error';
    $response = json_encode($response); header( "Content-Type: application/json" ); echo $response; exit;
    }