0

In settings/reading, I'm trying to add an option to exclude one or more post categories from main posts/home page. I'd like to utilize the wp_category_checklist function to create the new settings "field".

While I have successfully added a checklist under reading settings as intended, I don't see the means by which to get the selected values output to the new exclude_cats option on save. Meanwhile, I also tried adapting wp_dropdown_categories for multiple selection, and have successfully output selected values to the intended exclude_cats option, but don't see a way to return the dropdown with current values pre-selected in the field/dropdown after saving.

Here's what I have...

    class Add_Settings_Field {
    /* Class for adding a new field to the options-reading.php page */
    public function __construct() {
        add_filter( 'admin_init' , array( &$this , 'register_fields' ) );
    }
    public function register_fields() {
        register_setting( 'reading', 'exclude_cats');
        add_settings_field('exclude_cats',
            '<span title="Exclude Category Posts from Main Blog/Posts Page">Exclude Categories</label>',
            array( &$this, 'fields_html' ),
            'reading'
        );
    }
    public function fields_html() {
        // This would suffice if I could set the current values as selected in the resulting dropdown.
        $select_cats = wp_dropdown_categories('&hierarchical=1&hide_empty=0&orderby=name&name=exclude_cats&show_count=0&title_li=&use_desc_for_title=1&echo=0');
        $select_cats = str_replace( "name='exclude_cats' id=", "name='exclude_cats[]' multiple='multiple' id=", $select_cats );
        echo $select_cats;
        // I would prefer this if only I could get the selected values output to the "exclude_cats" option in wp_options on save.
        // $selected_cats = get_option( 'exclude_cats');
        // $exclude_cats = wp_category_checklist( '', '', $selected_cats, '', '', 1);
        // echo $exclude_cats;
    }
}
new Add_Settings_Field();

add_filter('pre_get_posts', 'exclude_cats');
function exclude_cats($query) {
    if ( $query->is_home() ) {
        foreach(get_option( 'exclude_cats') as $excluded) $excluded_cats[] = $excluded*-1;
        $excluded = implode(",",$excluded_cats);
        $query->set('cat', $excluded);
    }   return $query;
}
1

FWIW, to get this to work as intended, I went ahead with the wp_dropdown_categories function approach, settling for a js on-load hack to facilitate selection of currently excluded categories witin the dropdown.

/* Add Settings Field --"Exclude Categories"-- under options-reading.php page */
class Add_Settings_Field {
    public function __construct() {
        add_filter( 'admin_init' , array( &$this , 'register_fields' ) );
    }
    public function register_fields() {
        register_setting( 'reading', 'exclude_cats');
        add_settings_field('exclude_cats',
            '<span title="Exclude Category Posts from Main Blog/Posts Page">Exclude Categories</label>',
            array( &$this, 'fields_html' ),
            'reading'
        );
    }
    public function fields_html() {
        # Requires additional hidden field and js hack to set the current values as selected in the resulting dropdown.
        if($selected = get_option( 'exclude_cats')) $selected = implode(",",$selected);
        $excluded_cats = "<input type ='hidden' id='excluded_cats' value='$selected'>"; // to facilitate js selection options onload.
        $args = array(
            'show_option_none' => ' -- Select -- ',
            'hierarchical' => 1,
            'depth' => 1,
            'hide_empty' => 0,
            'orderby' => 'name',
            # 'selected'=> $selected, # accepts integer only, so won't work for multiple selections
            'name' => 'exclude_cats',
            'show_count' => 0,
            'use_desc_for_title' => 1,
            'echo'=> 0
        );
        $select_cats = wp_dropdown_categories($args);
        $select_cats = str_replace( "name='exclude_cats' id=", "name='exclude_cats[]' multiple='multiple' id=", $select_cats );
        echo $select_cats." ".$excluded_cats;
    }
}
new Add_Settings_Field();

and for the JS

if(jQuery('#exclude_cats').length)
{   jQuery('#exclude_cats option[value="-1"]').val(''); //'show_options_none' returns value of -1, which just won't do...
    var excluded = jQuery('#excluded_cats').val().split(',');
    jQuery(excluded).each(function(){
        jQuery('#exclude_cats option[value="'+this+'"]').prop('selected','selected');
    });
}
  • Please click on the check mark near the top of your answer to mark it approved. Thanks for coming back and telling us what worked. – Charles Clarkson Jul 26 '13 at 4:03

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.