Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm using the plugin Post from site, which sets up a WP front end writer. I changed some line of code because I need to select categories with checkboxes and not with a multiple select (as it was originally in the plugin). Unfortunately it doesn't work: there is a checkbox in the front end writer, now, but checking boxes just does nothing: the post is saved without categories.

Here's the code which handles the taxonomy selection:

public function get_taxonomy_list( $taxonomy ){
    $terms = get_terms($taxonomy, array(
        'hide_empty' => 0
    ));
    if (!$terms || empty($terms)) return '';
    //preg_match_all('/\s*<input class="(\S*)" value="(\S*)" type="checkbox">(.*)<\/input>\s*/', $terms, $matches, PREG_SET_ORDER);
    $out = apply_filters( 'pfs_taxonomy_label', "<label for='terms_$taxonomy'>Seleziona i corsi</label>", $taxonomy );
    foreach ($terms as $term){
        if (is_taxonomy_hierarchical($taxonomy))
            $out .= "<input class='{$term->term_taxonomy_id}' type='checkbox' value='{$term->term_taxonomy_id}' name='{$term->term_taxonomy_id}' /> {$term->name}<br />";
        else
            $out .= "<input class='{$term->term_taxonomy_id}' type='checkbox' value='{$term->name}' name='{$term->name}' /> {$term->name}";
    }
    $out .= "<br />\n";
    return apply_filters("pfs_{$taxonomy}_list",$out);
}

and here's the code which saves the post and the taxonomy (which I didn't change):

$postarr['tax_input'] = (array_key_exists('terms',$pfs_data)) ? $pfs_data['terms'] : array();
$post_id = wp_insert_post($postarr);
share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

You changed the checkbox's name attribute value. You should use the same name value: terms[$taxonomy][]

This should fix the code:

if (is_taxonomy_hierarchical($taxonomy))
        //$out .= "<input class='{$term->term_taxonomy_id}' type='checkbox' value='{$term->term_taxonomy_id}' name='{$term->term_taxonomy_id}' /> {$term->name}<br />";
        $out .= "<input class='{$term->term_taxonomy_id}' type='checkbox' value='{$term->term_taxonomy_id}' name='terms[{$taxonomy}][]' /> {$term->name}<br />";
    else
        // $out .= "<input class='{$term->term_taxonomy_id}' type='checkbox' value='{$term->name}' name='{$term->name}' /> {$term->name}";
        $out .= "<input class='{$term->term_taxonomy_id}' type='checkbox' value='{$term->name}' name='terms[{$taxonomy}][]' /> {$term->name}";
}
share|improve this answer
    
Yes! Thanks to you and a big facepalm for me. –  She Hulk Oct 8 '12 at 11:26
add comment

I usually use http://codex.wordpress.org/Function_Reference/wp_set_post_terms

when setting taxonomies on post creation.

$post_id = wp_insert_post();
if($post_id){
   wp_set_post_terms($post_id, $postarr['tax_input'], 'category' );
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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