0

I have tried to make a theme options page (starting with a sample theme options page from themeshaper) and I want to check if a comma separated list of categories is valid. Remove duplicates, remove non-existing categories, but it doesn't work. :(

It is deleting the last value if I check with empty():

function theme_options_validate( $input ) {
    if ($input['slidercats'] != '') {  
        $slidercats = $input['slidercats'];
        $slidercats_array = explode( ',', $slidercats );
        $slidercats_array = array_unique( $slidercats_array );
        $slidercats_array = array_map('intval', $slidercats_array );

        foreach($slidercats_array AS $i => $value)  
        {  
            $cat = get_category($i); // cat_ID
            if ( empty( $cat ) )
                unset($slidercats_array[$i]);
        }  

        $input['slidercats'] = implode(",", $slidercats_array);     

    }

    return $input;
}

http://pastebin.com/xR1aq6mD

And it is deleting all values but one with term_exists(id, 'category');

function theme_options_validate( $input ) {
    // global $select_options, $radio_options;

    // Change to integer
    $input['slidercount'] = (int)$input['slidercount'];
    if ($input['slidercount'] < 1 || $input['slidercount'] > 50)
        $input['slidercount'] = 10;

    // Make array from string, remove duplicates, change all values to integer, remove non existing categories, make string from array
    if ($input['slidercats'] != '') {  
        $slidercats = $input['slidercats'];
        $slidercats_array = explode( ',', $slidercats );
        $slidercats_array = array_unique( $slidercats_array );
        $slidercats_array = array_map('intval', $slidercats_array );

        foreach($slidercats_array AS $i => $value)  
        {  
            $cat = term_exists($i, 'category'); // cat_ID
            if ( ! $cat )
                unset($slidercats_array[$i]);
        } 

        $input['slidercats'] = implode(",", $slidercats_array);     

    }

    return $input;
}

http://pastebin.com/edx8F1YZ

The problem is the foreach, but I'm stuck and no php guru. Maybe someone could give me a hint how to solve this ...

1

In the following code, $i is not going to be your category ID.

 foreach($slidercats_array AS $i => $value)  
    {  
        $cat = get_category($i); // cat_ID
        if ( empty( $cat ) )
            unset($slidercats_array[$i]);
    }  

At least I don't see how it could be. If you explode a string the keys in the resulting array are numerical and sequentially generated starting at 0. I assume that your original string is a comma separated list of category ids. That would mean that the keys of the array-- the $is-- are going to have no relationship to your category ids. For example:

$arr = '9,8,7,6,5,4'; // assume these to be category IDs
print_r(explode(',',$arr));

/* Returns
Array
(
    [0] => 9
    [1] => 8
    [2] => 7
    [3] => 6
    [4] => 5
    [5] => 4
) */

You want to be using the $value not the $i. The $i is the [0],[1],[2], etc. You want the second half-- the $value-- when you check for the category (or term).

$cat = get_category($value); // cat_ID

You still want the $i when you unset though.

You seem to be making the same mistake in the second block as well and I am fairly sure that is incorrect use of the array keys is the root of the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.