Skip to:
Content
Pages
Categories
Search
Top
Bottom

Adding custom taxonomies to Topic form


  • Back to Front
    Participant

    @traverser11

    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 );
    };
    
Viewing 7 replies - 1 through 7 (of 7 total)

  • Robin W
    Moderator

    @robin-w

    some code from one of my plugins (bbp-style-pack) that plays with this area might help

    add_action ('bbp_new_topic_post_extras' , 'bsp_topic_tags_process' ) ;
    	add_action ('bbp_edit_topic_post_extras' , 'bsp_topic_tags_process' ) ;
    	add_action( 'bbp_new_reply_post_extras', 'bsp_reply_tags_process' );
    	add_action( 'bbp_edit_reply_post_extras', 'bsp_reply_tags_process' );
    
    //handle topic tags
    
    function bsp_topic_tags_process ($topic_id) {
    	if ( bbp_allow_topic_tags() && ! empty( $_POST['bbp_topic_tags'] ) ) {
    		
    		$terms = $_POST['bbp_topic_tags'] ;
    		
    		// Add topic tag ID as main key
    		$terms = array( bbp_get_topic_tag_tax_id() => $terms );
    		
    		$topic_data = array(
    		'ID'           => $topic_id,
    		'tax_input'    => $terms,
    		) ;
    		$topic_id = wp_update_post( $topic_data );
    		
    		
    	}
    }
    
    function bsp_reply_tags_process ($reply_id) {
    	if ( bbp_allow_topic_tags() && ! empty( $_POST['bbp_topic_tags'] ) ) {
    		$topic_id = bbp_get_reply_topic_id ($reply_id) ;
    		
    		$terms = $_POST['bbp_topic_tags'] ;
    		
    		// Add topic tag ID as main key
    		$terms = array( bbp_get_topic_tag_tax_id() => $terms );
    		
    		$topic_data = array(
    		'ID'           => $topic_id,
    		'tax_input'    => $terms,
    		) ;
    		$topic_id = wp_update_post( $topic_data );
    	}
    }

    Back to Front
    Participant

    @traverser11

    Thanks @robin-w! I’m still very lost, but I’ll try and pick up the clues from this as best I can:
    using bbp_new_topic_post_extras() in place of bbp_new_topic() ?
    using wp_update_post() instead of wp_set_object_terms() ?

    And I guess these two functions define $terms and all the variables seperately within each… I think that’s the bit I”m most confused how to achieve…

    Hmm I’ll keep trying, but launch the site first and leave this feature for down the track.
    but if anyone knows how to work this out, and wants some work to do it, let me know 🙂


    Robin W
    Moderator

    @robin-w

    ok, beyond free help, but my code takes an array from $_POST – which presumably is what your form is creating?

    maybe post your form code? and I’ll take one last look


    Back to Front
    Participant

    @traverser11

    Thanks again for taking a look is the code that appends the checkboxes to the bbpress topic forms… I guess $_POST is a global variable that bbpress topic form is also using?

    Sorry this is obviously a bit beyond me. And obviously beyond a free solution! But yes if you or others in bbpress do paid work like thiset me know and I’ll be in touch!

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

    Robin W
    Moderator

    @robin-w

    contact me via

    Contact me

    with a link to this topic


    ilyessd
    Participant

    @ilyessd

    Did you manage to do what you wanted ? I want to do the same actually but i didn’t figure it out…


    Back to Front
    Participant

    @traverser11

    No sorry I never did work it out.

Viewing 7 replies - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.
Skip to toolbar