Adding custom taxonomies to Topic form
-
Hi! I’m trying to allow users to assign their topics to custom taxonomies from the front end bbpress topic form. I thought it would be cool to be allow users to sort topics in multiple ways, and not just the anarchy that comes with unhierarchical ‘tags’.
With a lot of searching on forums, I’ve managed to register the taxonomies, display them, include topics in the archives, and added inputs to the form to allow uses to select the relevant ones.
But I’m totally stuck with saving the value from the checkboxes. You can tick the box, but nothing is saved. I’m guessing I need to use wp_set_object_terms()? and hook into bbp_new_topic() to save the terms? But I have no idea how to save the value from the checkboxes in there.
Any ideas, tips, scorn, alternative suggestions are welcome. Do i need to learn more js and use AJAX to accomplish this? Or is this achievable with php and am I even close?
// Add custom taxonomies to topic form add_action ( 'bbp_theme_after_topic_form_content', 'bbp_extra_fields'); function bbp_extra_fields() { $value = get_post_meta( bbp_get_topic_id(), 'issue', true); echo '<div id="custom-meta">'; echo'<fieldset> <legend>Issues</legend>'; $issues = get_terms('issue', array('hide_empty' => 0)); foreach ($issues as $issue) { echo '<span><input type="checkbox" class="issue" for="issue" value="'.$issue->slug.'"></input><label>'.$issue->name.'</label></span>'; }; echo '</fieldset>'; $value = get_post_meta( bbp_get_topic_id(), 'region', true); global $region; $region = get_terms('region', array('hide_empty' => 0)); echo'<fieldset> <legend>Region</legend>'; $regions = get_terms('region', array('hide_empty' => 0)); foreach ($regions as $region) { echo '<span><input type="checkbox" class="issue" for="issue"value="'.$region->slug.'"><label>'.$region->name.'</label></span>'; }; echo '</fieldset></div>'; }; // Save the terms from the form add_action ( 'bbp_new_topic', 'bbp_save_extra_fields', 10, 1 ); add_action ( 'bbp_edit_topic', 'bbp_save_extra_fields', 10, 1 ); function bbp_save_extra_fields($topic_id) { $post_id = get_the_ID(); $category_id = $region->id; $taxonomy = 'region'; wp_set_object_terms( $post_id, intval( $category_id ), $taxonomy ); };
- You must be logged in to reply to this topic.