Skip to:
Content
Pages
Categories
Search
Top
Bottom

Adding post meta to user replies

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

  • Robin W
    Moderator

    @robin-w

    you’ll want to use update_post_meta – this creates the entry if not there and amends it if it is.
    in the new reply handler, there is a hook which contains the reply_id which fires after the reply is created.

    so something like

    add_action( 'bbp_new_reply_post_extras', 'rew_reply_checkbox' );
    add_action( 'bbp_edit_reply_post_extras', 'rew_reply_checkbox' );
    
    function rew_reply_checkbox ($reply_id) {
    //and probably set the value to a $_POST parameter you set in the form
    if (! empty($_POST[‘my_parameter’] ) ) {
    $myvalue = $_POST[‘my_parameter’] ;
    }
    else $myvalue='empty' ;
    
    update_post_meta ($reply_id , 'my_parameter', $myvalue) ;
    }

    vincenzon617
    Participant

    @vincenzon617

    Thanks, Robin I will give this a go!


    vincenzon617
    Participant

    @vincenzon617

    I am having some difficulty figuring out what this does, apologies, my PHP knowledge is very minimal. What exactly is the

    if (! empty($_POST[‘my_parameter’] ) ) {
    $myvalue = $_POST[‘my_parameter’] ;
    }
    else $myvalue='empty';

    part doing? Specifically, what would ‘my_parameter’ be? Would this be the name or value of what I would like the meta to be?
    I have looked up what $_POST does, and the variables that I am using are declared using JavaScript. Now I am wondering if I will have to change my approach to checking the user input for the checkbox (something which isn’t related to bbpress).

    Many thanks


    Robin W
    Moderator

    @robin-w

    without knowing what you are trying to achieve hard to say.

    Typically you’d use JS for stuff you want to change on the fly without reloading the screen.

    For an input form you would more normally use html and php

    But I do fully appreciate that we all play to our strengths and I would tend to use PHP where JS would be better as I know it well, and I suspect you are the reverse 🙂

    So you would add a checkbox, you can do this by amending the reply template, or better using one of the do_action hooks in the template. You can also do this using JS

    In essence you are adding this to the form, you can use whatever names you want

    <p class="whatever">
    <input name="my_parameter" id="my_parameter" type="checkbox" value="whatever_value_you_want" checked="checked">								<label for="whatever">Tick this box to do whatever</label>
    </p>

    This will create a box which when submitted will set
    $_POST[‘my_parameter’] to “whatever_value_you_want” if ticked or be blank if not

    you can use the code above to set your meta data


    Robin W
    Moderator

    @robin-w

    so maybe have a function that says

    add_action( 'bbp_theme_after_reply_form_content', 'rew_add_checkbox' );
    
    function rew_add_checkbox () {
    echo '<p class="whatever">
    <input name="my_parameter" id="my_parameter" type="checkbox" value="whatever_value_you_want" checked="checked">								<label for="whatever">Tick this box to do whatever</label>
    </p>' ;
    
    }

    This will put the checkbox after the content


    vincenzon617
    Participant

    @vincenzon617

    Great, many thanks! This has clarified it for me and I now understand what I need to do :). PHP might be something I need to start learning!!


    vincenzon617
    Participant

    @vincenzon617

    I have been implementing this feature to my site today and I have managed to add the metadata to the database however, I can’t seem to be able to receive the POST variable. I have added this checkbox within the form: <form id="new-post" name="new-post" method="post" action="<?php the_permalink(); ?>">. This is the form used to get the user’s reply but as you can see the ‘action’ is set to <?php the_permalink(); ?>. Would this still allow me to access the variable within my functions.php file or will I have to change the action / create another form and submit button?

    Many thanks


    Robin W
    Moderator

    @robin-w

    ok, I’m very confused.

    if you are adding this code to a form, then it is already in a form, so you don’t need the <form…

    you’d just need to code I put above ie

    echo '<p class="whatever">
    <input name="my_parameter" id="my_parameter" type="checkbox" value="whatever_value_you_want" checked="checked">								<label for="whatever">Tick this box to do whatever</label>
    </p>' ;

    maybe you could post the entire form you are trying to change, and indeed say where/what it is?


    vincenzon617
    Participant

    @vincenzon617

    Sorry, I should have been more clear. I did post a reply but it got deleted while I was trying to get the code in the correct format to make it clearer to read!

    The form that I am adding to is within the ‘form-reply.php’ file and I have the checkbox working fine. The struggle I have is that even when the checkbox is clicked the $_POST['my_parameter'] is always empty and so the wrong value is being added as metadata. I have looked up some solutions to this, and the majority of them are suggesting it is something to do with the “action” part of the form, so I am wondering if this is the case or possibly something else?

    If you would like me to post the code I will try again!


    Robin W
    Moderator

    @robin-w

    I saw your one before the edit.

    I put this in my form-reply

    <input name="toggle-check" id="toggle-check" type="checkbox" value="value">hello

    and this in my theme’s functions file

    add_action( 'bbp_new_reply_post_extras', 'rew_reply_checkbox' );
    add_action( 'bbp_edit_reply_post_extras', 'rew_reply_checkbox' );
    
    function rew_reply_checkbox ($reply_id) {
    //and probably set the value to a $_POST parameter you set in the form
    if (! empty($_POST['toggle-check'] ) ) {
    $myvalue = $_POST['toggle-check'] ;
    }
    else $myvalue='empty' ;
    
    update_post_meta ($reply_id , 'toggle-check', $myvalue) ;

    and that works fine

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