2

i need to create a callback function with a multi checkbox with all the web/blog categories as multi options.

my add_settings_fields are:

   add_settings_field(  
        'select_page',                      
        'Select Blog Page',                         /
        'journal_combo_select_page_callback',   
        'journal_theme_blog_2_col',
        'blog_page_blog_2_col_section'  

    );

    add_settings_field( 
        'limit_posts',                      
        'Limit Posts',                          
        'journal_limit_posts_callback', 
        'journal_theme_blog_2_col',
        'blog_page_blog_2_col_section'  
    );

    add_settings_field(      // $id, $title, $callback, $page, $section, $args
    'check_categories',     // $id  
    'Choose Categories',    // $title 
    'journal_check_cats_callback', // $callback -
    'journal_theme_blog_2_col', // $page
    'blog_page_blog_2_col_section' // $section 
);

do i need to declare some kind of array in settings_field 'check_categories' to comunicate with the callback function?

    function journal_check_cats_callback() {
$options = get_option('journal_theme_blog_2_col');

    $pag = journal_theme_blog_2_col;
    $_cats = get_terms( 'category' );

        $html = '';
        foreach ($_cats as $term) {
            $checked = isset( $term->term_id ) ? $term->term_id : '0' ;
            $html .= sprintf( '<input type="checkbox" id="%1$s[%2$s]" name="journal_theme_blog_2_col[]" value="%2$s"%3$s />', $pag, $term->term_id, checked( $checked, $options['check_categories'], false ) );
            $html .= sprintf( '<label for="%1$s[%3$s]"> %2$s</label><br>', $pag, $term->name, $term->term_id );
        }
        $html .= sprintf( '<span class="description"> %s</label>', '' );

        echo $html;
}

EDIT:

well i'm trying to put this code working, but it's not quiet right yet... at the moment when i click in the Save button (submit) the values are saved in the wp_options table. For example:

in settings_section i have three settings_fields. One combo box (select_page), one input text fiel (limit_posts) and one multicheck box with blog categorys (check_categories) and bellow is the data saved in wp-options table:

a:4:{s:11:"select_page";s:1:"4";s:11:"limit_posts";s:3:"100";i:0;s:2:"13";i:1;s:1:"7";}

and this is the correspondent array:

Array ( [select_page] => 4 [limit_posts] => 100 [0] => 13 [1] => 7 )

Problems to solve: 1 - the checkboxes don't stay checked! And of course if i click on save again without checking new ones, the values in the options table are erase.

2 - i also realized that the "check_categories" isn't save into the serialized value... So i think this is a problem, because i don't know how to fecth only the array values from "check_categories"

Someone can give me some help?

Thanks, nelson

1
  • Just add some more detailed info about the issues i need to solve.
    – naires
    Jul 26, 2012 at 17:05

1 Answer 1

2

I had the same problem, and here what works for me:

function journal_check_cats_callback() {
    $options    = get_option('journal_theme_blog_2_col');
    $pag        = journal_theme_blog_2_col;
    $_cats      = get_terms( 'category' );
    $html       = '';

    foreach ($_cats as $term) {

        $checked = in_array($term->term_id, $options) ? 'checked="checked"' : '';
        $html .= sprintf( '<input type="checkbox" id="%1$s[%2$s]" name="%1$s[]" value="%2$s" %3$s />', $pag, $term->term_id, $checked );
        $html .= sprintf( '<label for="%1$s[%3$s]"> %2$s</label><br>', $pag, $term->name, $term->term_id );
    }

    $html .= sprintf( '<span class="description"> %s</label>', '' );

    echo $html;

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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