Skip to:
Content
Pages
Categories
Search
Top
Bottom

customize bbpress reply form with dropdown selection list form


  • g28f99
    Participant

    @g28f99

    I hope to customize bbpress reply form area with dropdown selection list. I could not find any previous post reporting similar case in bbpress. Referring to post “Save meta box data from selected dropdown list” with link I prepared similar code for the bbpress reply post condition, but failed to achieve the goal.
    Please kindly provide suggestions!

    ` add_action( ‘bbp_theme_before_reply_form_content’, ‘so_custom_meta_box’ );
    //add_action( ‘add_meta_boxes’, ‘so_custom_meta_box’ );

    function so_custom_meta_box($post){
    add_meta_box(‘so_meta_box’, ‘Custom Box’, ‘custom_element_grid_class_meta_box’, $post->post_type, ‘normal’ , ‘high’);
    }

    add_action(‘bbp_theme_before_reply_content’, ‘so_save_metabox’);

    function so_save_metabox(){
    global $post;
    if(isset($_POST[“custom_element_grid_class”])){
    //UPDATE:
    $meta_element_class = $_POST[‘custom_element_grid_class’];
    //END OF UPDATE

    update_post_meta($reply_id, ‘custom_element_grid_class_meta_box’, $meta_element_class);
    //print_r($_POST);
    }
    }
    function custom_element_grid_class_meta_box($post){
    $reply_id = bbp_get_reply_id();
    $meta_element_class = get_post_meta($reply_id, ‘custom_element_grid_class_meta_box’, true); //true ensures you get just one value instead of an array
    ?>
    <label>Choose the size of the element : </label>

    <select name=”custom_element_grid_class” id=”custom_element_grid_class”>
    <option value=”normal” <?php selected( $meta_element_class, ‘normal’ ); ?>>normal</option>
    <option value=”square” <?php selected( $meta_element_class, ‘square’ ); ?>>square</option>
    <option value=”wide” <?php selected( $meta_element_class, ‘wide’ ); ?>>wide</option>
    <option value=”tall” <?php selected( $meta_element_class, ‘tall’ ); ?>>tall</option>
    </select>
    <?php
    }

Viewing 21 replies - 1 through 21 (of 21 total)

  • Robin W
    Moderator

    @robin-w

    meta boxes are used in the backend, but you are most of the way there.

    so to add the dropdown you can use

    add_action(‘bbp_theme_before_reply_content’, ‘so_additional_content’);

    (there is a bbp_theme_after_reply_content action as well)

    and then in the function put the drop down stuff – I’ve not checked your code, so you’ll need to tidy and correct the following if it doesn’t work

    so

    function so_additional_content () {
    ?>
    <label>Choose the size of the element : </label>
    <select name=”custom_element_grid_class” id=”custom_element_grid_class”>
    <option value=”normal” <?php selected( $meta_element_class, ‘normal’ ); ?>>normal</option>
    <option value=”square” <?php selected( $meta_element_class, ‘square’ ); ?>>square</option>
    <option value=”wide” <?php selected( $meta_element_class, ‘wide’ ); ?>>wide</option>
    <option value=”tall” <?php selected( $meta_element_class, ‘tall’ ); ?>>tall</option>
    </select>
    <?php 
    }

    Then when the user presses submit a function in bbpress called ‘new reply handler’ takes over.

    that has a hook

    do_action( ‘bbp_new_reply_post_extras’, $reply_id );

    which you can link to, to do the save.

    so you would have

    add_action ( 'bbp_new_reply_post_extras' , 'so_save' ) ;
    
    function so-save ($reply_id) {
    $meta_element_class = $_POST[‘custom_element_grid_class’];
    update_post_meta($reply_id, ‘custom_element_grid_class_meta_box’, $meta_element_class);
    }

    again your code is not checked.

    you will need to add functionality if you want users to edit their replies and change the dropdown selection

    I’ll let you play with the above, and do come back with a first version if you need extra help.

    If you fix it, then PLEASE post your solution here to help others


    g28f99
    Participant

    @g28f99

    Thank you, Robin!
    I made some corrections and further checked the selection option parts accordingly. I also add a show contents part referring to a workable previous post with link.
    The code can provide options for the user to selection. But the selection result could not be shown. I am not sure whether the selection result was added into metabox properly because I can not use the debug function. And another problem is that the “Field 1 ” was inserted into all previous replies.
    Could you further provide help to solve this problem?

    
    add_action( 'bbp_theme_before_reply_form_content', 'so_additional_content' );
    function so_additional_content($post){
    	$reply_id = bbp_get_reply_id();
        $meta_element_class = get_post_meta($reply_id, 'custom_element_grid_class_meta_box', true); //true ensures you get just one value instead of an array
        ?>   
        echo '<label>Choose the size of the element :  </label>
    
        <select name="custom_element_grid_class" id="custom_element_grid_class">
          <option value="normal" <?php selected( $meta_element_class, 'normal' ); ?>>normal</option>
          <option value="square" <?php selected( $meta_element_class, 'square' ); ?>>square</option>
          <option value="wide" <?php selected( $meta_element_class, 'wide' ); ?>>wide</option>
          <option value="tall" <?php selected( $meta_element_class, 'tall' ); ?>>tall</option>
        </select>'
        <?php
    }
     
    
     
    add_action('bbp_new_reply_post_extras', 'so_save_metabox');
    
    function so_save_metabox(){ 
        global $post;
        if(isset($_POST["custom_element_grid_class"])) {
             //check the capability: if ( !current_user_can( 'edit_post', $post->ID ))  return $post->ID;
            $meta_element_class = $_POST['custom_element_grid_class'];
            //END OF UPDATE
    
            update_post_meta($reply_id, 'custom_element_grid_class_meta_box', $meta_element_class);
            //print_r($_POST);
    		
        }
    }
    
    add_action('bbp_theme_before_reply_content', 'so_show_metabox');
    
    function so_show_metabox() {
      $meta_element_class = get_post_meta($reply_id, 'custom_element_grid_class_meta_box', true);
      // the problem is "Field 1" appear in every reply post
      echo "Field 1: ". $meta_element_class."<br>";  
    }
    
    

    Robin W
    Moderator

    @robin-w

    ok, I’ve ONLY edited this to correct syntax, but try it and then come back

    add_action( 'bbp_theme_before_reply_form_content', 'so_additional_content' );
    function so_additional_content($post){
    	$reply_id = bbp_get_reply_id();
        $meta_element_class = get_post_meta($reply_id, 'custom_element_grid_class_meta_box', true); //true ensures you get just one value instead of an array
         echo '<label>Choose the size of the element :  </label>' ;
    	?> 
        <select name="custom_element_grid_class" id="custom_element_grid_class">
          <option value="normal" <?php selected( $meta_element_class, 'normal' ); ?>>normal</option>
          <option value="square" <?php selected( $meta_element_class, 'square' ); ?>>square</option>
          <option value="wide" <?php selected( $meta_element_class, 'wide' ); ?>>wide</option>
          <option value="tall" <?php selected( $meta_element_class, 'tall' ); ?>>tall</option>
        </select>'
        <?php
    }
     
    
     
    add_action('bbp_new_reply_post_extras', 'so_save_metabox');
    
    function so_save_metabox(){ 
        global $post;
        if(isset($_POST["custom_element_grid_class"])) {
             //check the capability: if ( !current_user_can( 'edit_post', $post->ID ))  return $post->ID;
            $meta_element_class = $_POST['custom_element_grid_class'];
            //END OF UPDATE
    
            update_post_meta($reply_id, 'custom_element_grid_class_meta_box', $meta_element_class);
            //print_r($_POST);
    		
        }
    }
    
    add_action('bbp_theme_before_reply_content', 'so_show_metabox');
    
    function so_show_metabox() {
      $meta_element_class = get_post_meta($reply_id, 'custom_element_grid_class_meta_box', true);
      // the problem is "Field 1" appear in every reply post
      echo "Field 1: ". $meta_element_class."<br>";  
    }

    g28f99
    Participant

    @g28f99

    I have tested it and no progress observed. How to save and demonstrate the selected option have not been properly resolved.


    Robin W
    Moderator

    @robin-w

    easier to start again

    this works

    add_action( 'bbp_theme_before_reply_form_content', 'rew_additional_field' );
    function rew_additional_field($post){
    	?>
    	<select name="rew_additional_field" id="rew_additional_field">
    		<option value="enqury">Enquiry</option>
    		<option value="complaint">Complaint</option>
    		<option value="feedback">Feedback</option>
    	</select>
    	<?php
    }
     
    
    add_action('bbp_new_reply_post_extras', 'rew_save_additional_field');
    
    function rew_save_additional_field($reply_id){ 
        global $post;
        if(isset($_POST["rew_additional_field"])) {
            update_post_meta ($reply_id, 'rew_additional_field', $_POST["rew_additional_field"]) ;
    	}
    }
    
    add_action('bbp_theme_before_reply_content', 'rew_show_additional_field');
    
    function rew_show_additional_field () {
    $reply_id = bbp_get_reply_id();
      $field = get_post_meta($reply_id, 'rew_additional_field', true);
      if (!empty ($field)) {
    	echo "Type: ". ucfirst($field)."<br>";  
      }
    }

    g28f99
    Participant

    @g28f99

    Great!Thanks a lot.


    g28f99
    Participant

    @g28f99

    But I found a new problem. I hope more than one selection box will be shown there.
    And there will be errors. Could you please kindly suggest solutions?

    
    add_action( 'bbp_theme_before_reply_form_content', 'rew_additional_field' );
    function rew_additional_field($post){
    	?>
    	<select name="rew_additional_field" id="rew_additional_field">
    		<option value="enqury">Enquiry</option>
    		<option value="complaint">Complaint</option>
    		<option value="feedback">Feedback</option>
    	</select>
    	
    	
    	<select name="rew_additional_field_2" id="rew_additional_field_2">
    		<option value="enqury2">Enquiry2</option>
    		<option value="complaint2">Complaint2</option>
    		<option value="feedback2">Feedback2</option>
    	</select>
    	<?php
    }
     
    
    add_action('bbp_new_reply_post_extras', 'rew_save_additional_field');
    
    function rew_save_additional_field($reply_id){ 
        global $post;
        if(isset($_POST["rew_additional_field"])) {
            update_post_meta ($reply_id, 'rew_additional_field', $_POST["rew_additional_field"]) ;
    		
        if(isset($_POST["rew_additional_field_2"])) {
            update_post_meta ($reply_id, 'rew_additional_field_2', $_POST["rew_additional_field_2"]) ;
    	}
    }
    
    add_action('bbp_theme_before_reply_content', 'rew_show_additional_field');
    
    function rew_show_additional_field () {
    $reply_id = bbp_get_reply_id();
      $field = get_post_meta($reply_id, 'rew_additional_field', true);
      if (!empty ($field)) {
    	echo "Type: ". ucfirst($field)."<br>";  
    	
      $field_2 = get_post_meta($reply_id, 'rew_additional_field_2', true);
      if (!empty ($field_2)) {
    	echo "Type: ". ucfirst($field_2)."<br>";  
      }
    }
    
    

    Robin W
    Moderator

    @robin-w

    so does that not work ?


    g28f99
    Participant

    @g28f99

    Thanks!
    When one selection box, works well.
    When two as above, will have errors at loading website.


    Robin W
    Moderator

    @robin-w

    add_action( 'bbp_theme_before_reply_form_content', 'rew_additional_field' );
    function rew_additional_field($post){
    	?>
    	<select name="rew_additional_field" id="rew_additional_field">
    		<option value="enqury">Enquiry</option>
    		<option value="complaint">Complaint</option>
    		<option value="feedback">Feedback</option>
    	</select>
    	
    	
    	<select name="rew_additional_field_2" id="rew_additional_field_2">
    		<option value="enqury2">Enquiry2</option>
    		<option value="complaint2">Complaint2</option>
    		<option value="feedback2">Feedback2</option>
    	</select>
    	<?php
    }
     
    
    add_action('bbp_new_reply_post_extras', 'rew_save_additional_field');
    
    function rew_save_additional_field($reply_id){ 
        global $post;
        if(isset($_POST["rew_additional_field"])) {
            update_post_meta ($reply_id, 'rew_additional_field', $_POST["rew_additional_field"]) ;
    	}
        if(isset($_POST["rew_additional_field_2"])) {
            update_post_meta ($reply_id, 'rew_additional_field_2', $_POST["rew_additional_field_2"]) ;
    	}
    }
    
    add_action('bbp_theme_before_reply_content', 'rew_show_additional_field');
    
    function rew_show_additional_field () {
    $reply_id = bbp_get_reply_id();
    	$field = get_post_meta($reply_id, 'rew_additional_field', true);
    	if (!empty ($field)) {
    		echo "Type: ". ucfirst($field)."<br>"; 
    	}	
    	$field_2 = get_post_meta($reply_id, 'rew_additional_field_2', true);
    	if (!empty ($field_2)) {
    		echo "Type: ". ucfirst($field_2)."<br>";  
    	}
    }

    Robin W
    Moderator

    @robin-w

    I just amended the above, so try it again in case you loaded the wrong version !


    g28f99
    Participant

    @g28f99

    Great Robin! It now works well!


    g28f99
    Participant

    @g28f99

    I know you made some update.
    But, I just can not found where has been modified. Could you tell me?


    Robin W
    Moderator

    @robin-w

    just overwrite your code with the above – that code works


    g28f99
    Participant

    @g28f99

    Thank you again!
    I also hope to add specified sentence after user personalized replies.

    
    add_action( 'bbp_theme_after_reply_content', 'new_reply_additional_fields', 10 );
    function new_reply_additional_fields() {
    $new_reply_additional_fields = 'Above is my personal opinion';
    	echo "<p id='new_reply_additional_fields'><font color=red font size='4pt'>$new_reply_additional_fields</p>";	
    

    Using above code directly also induce wordpress website error. To permanently show above sentence after every reply, I suppose it should be saved into metabox. Could you please help manage how to integrated into above code to solve this problem?


    Robin W
    Moderator

    @robin-w

    add_action( 'bbp_theme_after_reply_content', 'new_reply_additional_fields', 10 );
    function new_reply_additional_fields() {
    $new_reply_additional_fields = 'Above is my personal opinion';
    	echo "<p id='new_reply_additional_fields'><font color=red font size='4pt'>".$new_reply_additional_fields."</p>";	
    }

    g28f99
    Participant

    @g28f99

    Many many thanks!


    Robin W
    Moderator

    @robin-w

    great – glad you are fixed


    g28f99
    Participant

    @g28f99

    So I shall finalize the code for others for reference use. One last optimization for this topic so that it is really powerful:
    I may set forums with “menu_order” <10 (class=”screen-reader-text”) as admin purpose forums and do not want to apply above customization setting. Can I really achieve that goal with some condition setting?


    g28f99
    Participant

    @g28f99

    Hi, Robin. I think the above goal to apply the customization only in some specified forums should be practical. For example, I do NOT want the above customization to take effects in two forums with forum id 124 and 126. Or, alternatively, I do NOT want the customization to take effects in forums with menu_order < 10.But I could not find any previous posts with codes to set the effective range among forums.
    How do you think about it? I have no idea how to integrate the following selection information into the above customization code. Could you please make a demonstration?

    
    $current_forum_id = get_forum_id();
    if $current_forum_id !==124 AND $current_forum_id !==126 
    

    Robin W
    Moderator

    @robin-w

    It is all quite doable, but sorry, you well into design and beyond free help in a support forum.

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