0

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);

2 Answers 2

1

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}";
}
0
0

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' );
}

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.