Skip to:
Content
Pages
Categories
Search
Top
Bottom

bbress custom fields, auto-populate topic content


  • Jerry
    Participant

    @jerrysc

    I put this in “pimp your press”, but now I think it belongs in the troubleshooting forum. Not certain.

    I have created custom fields for my bbpress topics, and I have learned how to auto-populate those fields when a user clicks a button. I have also learned how to auto-populate the Topic Title. I am using javascript to auto-populate the topic title. Here is how I assign the data:

    $("input[name='bbp_topic_title']").val(data.title);

    I am showing this as an example to show what I need. My question is; am I able to auto-populate the content of a new topic, much like I am doing with the title for a new topic?

    To auto-populate the content of a new topic, I assume I need to have the correct term, which I think would be ‘bbp_topic_content’? So it would look like this?

    $("input[name='bbp_topic_content']").val(data.description);

    Here are three functions I am using for custom fields:

    add_action ( 'bbp_theme_before_topic_form_content', 'bbp_extra_fields');
    function bbp_extra_fields() {
     $value = get_post_meta( bbp_get_topic_id(), 'bookCover', true);
       echo '<label for="bookCover">Book Cover URL</label><br>';
       echo "<p><input type='text' name='bookCover' value='".$value."'></p>";
    }
    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=0) {
      if (isset($_POST) && $_POST['bookCover']!='')
        update_post_meta( $topic_id, 'bookCover', $_POST['bookCover'] );
    }
    add_action('bbp_template_before_replies_loop', 'bbp_show_extra_fields');
    //add_action('bbp_template_before_main_content', 'bbp_show_extra_fields');
    function bbp_show_extra_fields() {
      $topic_id = bbp_get_topic_id();
      $valuec = get_post_meta( $topic_id, 'bookCover', true);
    }

    I am thinking that I need some sort of “add_action” to get this accomplished? Do I need to create a new function? I’ve been looking in templates.php to try and get a hint to figure this out. Any help would be GREATLY appreciated. Thanks.

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

  • Jerry
    Participant

    @jerrysc

    This is a rare event for me; I figured this out within 4 hours of posting it. I am including the solution in case someone else decides they want to do the same thing. I had the input name correct: bbp_topic_content. As I was guessing above, I merely had to create a new function. Below is the variable declaration in javascript, and the function that auto-populates the content field of a new topic:
    $("textarea[name='bbp_topic_content']").val(data.description);

    add_action ( 'bbp_get_the_content', 'bbp_description_to_topic');
    function bbp_description_to_topic(){
        $value = get_post_meta( bbp_get_topic_id(), 'bbp_topic_content', true);
        echo "<p><textarea style='width: 414px; height: 151px' name='bbp_topic_content' value='".$value."'></textarea></p>";
    }

    Robin W
    Moderator

    @robin-w

    This looks like it would be really useful to some people.

    Can you post the whole solution, inc. the javascript? so that people can just copy/paste and then amend.


    Jerry
    Participant

    @jerrysc

    No problem. I found out that I did not need to create that extra function after all. All I had to do was place ‘bbp_topic_content’ in the ajax function. Here is the javascript. I put more code below this.

    <script>
          $(function() {
          $( "#dialog" ).dialog({height: 550, width: 450});
            $( ".submit" ).click(function(){
            $.ajax({
             type: "POST",
             url: 'review-meta.php',
             async:true,
             dataType: 'json',
             //assign values to the variables to be passed to the server via data, according to their B position
             //within the foreach array. See script within loop for jquery window to see how B is derived.
             data: { B : B, cover : b_cover[B], coverSmall : b_cover_small[B], title : b_title[B], author : b_author[B], published : b_published[B],
             ISBN : b_ISBN[B], description : b_description[B], pages : b_pages[B], publisher : b_publisher[B]},
             success: function(data)
                 {
                 //identify the variables for unique handing on the server side, this is
                 //how the book data gets to the input fields for bbpress forums
                 $("textarea[name='bookCover']").val(data.cover);
                 $("input[name='bookCoverSmall']").val(data.coverSmall);
                 $("input[name='bbp_topic_title']").val(data.title);
                 $("input[name='bookAuthor']").val(data.author);
                 $("input[name='bookPublished']").val(data.published);
                 //$("textarea[name='bookDescription']").val(data.description);
                 $("input[name='bookPages']").val(data.pages);
                 $("input[name='bookPublisher']").val(data.publisher);
                 $("input[name='bookISBN']").val(data.ISBN);
                 $("textarea[name='bbp_topic_content']").val(data.description);
                 //alert(B);
                 },
                 //error: function(errorThrown){
                 //alert('error');
                 //},
                 });
          $( "#dialog" ).dialog( "close" );
          });  });
    </script> 

    Here’s how I did variable assignment in a different script, located within the loop where I am generating results. I did not include all the variable as this is long. Also, I am wondering if I could have done some type of auto-indexing instead of having to list each result. Luckily there are only 10, otherwise this would have been crazy.

    $( ".submit" ).click(function(){
                //get the id number of the button the user selects, which is indexed for each result
                //the variable B is then decremented 1 from id because javascript arrays begin with [0]
                if(this.id.indexOf('select')>-1) {var id = (this.id.split(" "))[1]; console.log(id);}
                B = id - 1;   });
                //create an array for all 10 results. There is probably a cleaner way to do this
                var b_cover = [<?php echo json_encode($b_cover[1]); ?>,<?php echo json_encode($b_cover[2]); ?>,<?php echo json_encode($b_cover[3]); ?>,<?php echo json_encode($b_cover[4]); ?>,
                <?php echo json_encode($b_cover[5]); ?>,<?php echo json_encode($b_cover[6]); ?>,<?php echo json_encode($b_cover[7]); ?>,<?php echo json_encode($b_cover[8]); ?>,
                <?php echo json_encode($b_cover[9]); ?>,<?php echo json_encode($b_cover[10]); ?>];
                //this is the javascript foreach that stores the array values 0 to 9
                b_cover.forEach(function(entry){console.log(entry);});
                
                var b_title = [<?php echo json_encode($b_title[1]); ?>,<?php echo json_encode($b_title[2]); ?>,<?php echo json_encode($b_title[3]); ?>,
                <?php echo json_encode($b_title[4]); ?>,<?php echo json_encode($b_title[5]); ?>,<?php echo json_encode($b_title[6]); ?>,
                <?php echo json_encode($b_title[7]); ?>,<?php echo json_encode($b_title[8]); ?>,<?php echo json_encode($b_title[9]); ?>,<?php echo json_encode($b_title[10]); ?>];
                b_title.forEach(function(entry){console.log(entry);});

    Do you want me to include the bbpress custom fields code?


    Robin W
    Moderator

    @robin-w

    Do you want me to include the bbpress custom fields code?

    yes please !


    Jerry
    Participant

    @jerrysc

    @robin-w, here is my code for the custom fields. You will see where arguments and names match the variables being passed from javascript. This code was placed in a separate file as an additional plugin:

    add_action ( 'bbp_theme_before_topic_form_content', 'bbp_extra_fields');
    function bbp_extra_fields() {
    
      //This is where the cover will go, from google book search
       $value = get_post_meta( bbp_get_topic_id(), 'bookCover', true);
       echo '<label for="bookCover">Book Cover URL</label><br>';
       echo "<p><textarea style='width: 250px; height: 50px'  name='bookCover' value='".$value."'></textarea></p>";
      //Input field for the Author
       $value = get_post_meta( bbp_get_topic_id(), 'bookAuthor', true);
       echo '<label for="bookAuthor">Author</label><br>';
       echo "<p><input type='text' name='bookAuthor' value='".$value."'></p>";
    
       $value = get_post_meta( bbp_get_topic_id(), 'bookPublished', true);
       echo '<label for="bookPublished">Published</label><br>';
       echo "<p><input type='text' name='bookPublished' value='".$value."'></p>";
    
      //Input field for the number of pages in print
       $value = get_post_meta( bbp_get_topic_id(), 'bookPages', true);
       echo '<label for="bookPages">Pages</label><br>';
       echo "<p><input type='text' name='bookPages' value='".$value."'></p>";
      //Input field for the Publisher that corresponds to the Published data
       $value = get_post_meta( bbp_get_topic_id(), 'bookPublisher', true);
       echo '<label for="bookPublisher">Publisher</label><br>';
       echo "<p><input type='text' name='bookPublisher' value='".$value."'></p>";
      //Input field for the ISBN
       $value = get_post_meta( bbp_get_topic_id(), 'bookISBN', true);
       echo '<label for="bookISBN">ISBN</label><br>';
    //   echo "<p><input type='number' name='bookISBN' value='".$value."'></p>";
       echo "<p><input type='text' name='bookISBN' value='".$value."'></p>";
       }
    
    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=0) {
      if (isset($_POST) && $_POST['bookCover']!='')
        update_post_meta( $topic_id, 'bookCover', $_POST['bookCover'] );
      if (isset($_POST) && $_POST['bookAuthor']!='')
        update_post_meta( $topic_id, 'bookAuthor', $_POST['bookAuthor'] );
      if (isset($_POST) && $_POST['bookPublished']!='')
        update_post_meta( $topic_id, 'bookPublished', $_POST['bookPublished'] );
    //  if (isset($_POST) && $_POST['bookDescription']!='')
    //    update_post_meta( $topic_id, 'bookDescription', $_POST['bookDescription'] );
      if (isset($_POST) && $_POST['bookPages']!='')
        update_post_meta( $topic_id, 'bookPages', $_POST['bookPages'] );
      if (isset($_POST) && $_POST['bookPublisher']!='')
        update_post_meta( $topic_id, 'bookPublisher', $_POST['bookPublisher'] );
      if (isset($_POST) && $_POST['bookISBN']!='')
        update_post_meta( $topic_id, 'bookISBN', $_POST['bookISBN'] );
      }
    
    add_action('bbp_template_before_replies_loop', 'bbp_show_extra_fields');
    //add_action('bbp_template_before_main_content', 'bbp_show_extra_fields');
    function bbp_show_extra_fields() {
      $topic_id = bbp_get_topic_id();
      $valuec = get_post_meta( $topic_id, 'bookCover', true);
      $value1 = get_post_meta( $topic_id, 'bookAuthor', true);
      $value2 = get_post_meta( $topic_id, 'bookPublished', true);
    //  $value3 = get_post_meta( $topic_id, 'bookDescription', true);
      $value4 = get_post_meta( $topic_id, 'bookPages', true);
      $value5 = get_post_meta( $topic_id, 'bookPublisher', true);
      $value6 = get_post_meta( $topic_id, 'bookISBN', true);
    
    //Below is the table format for displaying a book. Might consider changing this
    //to a form??? html header???
    ?>
    
      <table style="width:900px">
      <col width="100">
        <col width="265">
        <col width="535">   
    		  <tr>
          <td style="vertical-align:top">
          <img src=<?php printf(rawurldecode($valuec))?> />
          </td>
          
          <td style="vertical-align:top">
    			 <strong><p style="font-size: 14px";>Author: </strong><?php printf( $value1); ?> </p>
    			 <strong><p style="font-size: 14px";>Published: </strong><?php printf( $value2); ?></p>					     
           <strong><p style="font-size: 14px";>Page(s): </strong><?php printf( $value4); ?></p>
    			 <strong><p style="font-size: 14px";>Publisher: </strong><?php printf( $value5); ?></p>
           <strong><p style="font-size: 14px";>ISBN: </strong><?php printf( $value6); ?></p><br>
           <strong><div style="font-size: 14px";>Description </strong></div>
          </td>
          </tr>
      </table>
    
    <?php
    }

    It would probably also be instructive for me to include the review-meta.php file that is called by javascript, so here is that code as well:

         if ( isset( $_POST['cover'] )) {
            $cover = $_POST['cover'];
          }
         if ( isset( $_POST['coverSmall'] )) {
            $coverSmall = $_POST['coverSmall'];
          }      
         if ( isset( $_POST['title'] )) {
            $title = $_POST['title'];
          }
         if ( isset( $_POST['author'] )) {
            $author = $_POST['author'];
          }
         if ( isset( $_POST['published'] )) {
            $published = $_POST['published'];
          }
         if ( isset( $_POST['description'] )) {
            $description = $_POST['description'];
          }
         if ( isset( $_POST['pages'] )) {
            $pages = $_POST['pages'];
          }
         if ( isset( $_POST['publisher'] )) {
            $publisher = $_POST['publisher'];
          }
         if ( isset( $_POST['ISBN'] )) {
            $ISBN = $_POST['ISBN'];
          }
          $r=array("cover"=>$cover,"coverSmall"=>$coverSmall,"title"=>$title,"author"=>$author,"published"=>$published,
          "description"=>$description,"pages"=>$pages,"publisher"=>$publisher,"ISBN"=>$ISBN);
          print(json_encode($r));

    Jerry
    Participant

    @jerrysc

    @robin-w, can you please help me with another issue I am having? Here is the link to my posted question:

    Is it possible to remove topic-author, but keep reply-authors


    Robin W
    Moderator

    @robin-w

    yes I’d seen that question and had a look – like all things it’s possible but not using css (or at least I’m not good enough at css to tell you how !)

    I’ll hopefully come back to it shortly when I get a moment !

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