Forum Replies Created

Viewing 15 replies - 31 through 45 (of 380 total)
  • Thread Starter ejm

    (@llizard)

    re:

    Heh, I was trying to get you to work out the final solution to the parentheses quandary by dropping a bunch of hints. One usually learns more that way…

    If it’s any consolation, I did see that the hints were being dropped but I had managed to get myself so mixed up that I failed to get beyond knowing there were arrows pointing to the flashing words “this is a hint”. (I still think I’m getting an inkling about this though…)

    Remarkably, I’m not at all uncomfortable with phpMyAdmin and have managed to remove the extraneous   values for $webname. Thank you for spelling out for me which table to save first (I’m a big fan of “save early, save often”) and then remove the offending values.

    I had started to grow attached to the tilde but still decided to returne to the parentheses to go around $webname. When I did that in the functions file, I also removed the extraneous part at the end that was simply a repeat of the add_comment_meta_values($comment_id) function….

    /*.......
     from wpengineer.com/2214/adding-input-fields-to-the-comment-form/
    ....*/ 
    function save_comment_meta_data( $comment_id ) {
        add_comment_meta( $comment_id, 'webname', $_POST[ 'webname' ] );
    }
    add_action( 'comment_post', 'save_comment_meta_data' );

    To test that everything was working, I added a webname value to a comment that didn’t have one. Now I cannot seem to remove it – except via phpMyAdmin. It insists on holding onto the space. (Oh oh….)

    What this means, I’m assuming, is that I have not managed to get the $webname to unset. Am I right that here is my clue for fixing this?

    When we use isset() like this:
    if ( isset( $webname ) && '' != $webname ) {
    it serves to prevent unassigned variable warnings when we do ” != $webname should $webname not be set for any reason.

    Here is the coding I tried, but it does not make it so that the parentheses disappear, nor does it follow the instruction to remove the   (because, I guess, the empty value is in the DB???).

    
    function add_comment_meta_values($comment_id) {
       if ( ( isset( $_POST['webname'] ) ) && ( $_POST['webname'] != '') ) {
    	$webname = wp_filter_nohtml_kses($_POST['webname']);
            add_comment_meta($comment_id, 'webname', $webname);
        } 
       if (($_POST[ 'webname' ] = ' ') || ($_POST[ 'webname' ] = ' ')) {
    	$webname = '';	
       } 
    }
    add_action ('comment_post', 'add_comment_meta_values', 1);

    I was going to try using your array somehow but, well, you know… I’m barely able to tread water to keep breathing as it is.

    • This reply was modified 6 years, 7 months ago by ejm.
    • This reply was modified 6 years, 7 months ago by ejm.
    Thread Starter ejm

    (@llizard)

    re:

    Remember that comment author link output occurs during a GET request, so there will be nothing in the global $_POST.

    I’m guessing that this explains (sort of) why a webname value that is removed in WP_admin, after being entered from outset, remains as ' ', rather than disappearing.

    Because I have been unable to figure out how to remove the parentheses from those newly empty value fields, I’ve compromised by changing the parentheses to a single tilde: $author .= esc_html(" ~ $webname");. This way, it doesn’t look ridiculous to see “Commenter’sName ~” instead of “Commenter’sName ()”

    I’m completely unclear where to put

    if ($webname = '') || ($webname = ' ') {
      !isset( $webname ) 
    }

    (I don’t even know if !isset is an actual thing….)

    OR

    if ($webname = ' ') {
      $_POST['webname'] = ''; 
    }

    Also, if $webname = '', is it not possible to display the emptiness with emptiness, rather than a tilde (or as before, inside parentheses)?

    I realize that you may well have answered this already; please excuse my obtuseness.
    Babysteps! Babysteps! Thank you again for all your help.

    • This reply was modified 6 years, 7 months ago by ejm.
    Thread Starter ejm

    (@llizard)

    Sigh… I spoke to soon. Back to the drawing board….

    The parentheses are still appearing.

    Thread Starter ejm

    (@llizard)

    Please excuse me for replying to myself….

    It’s a miracle! I think I got it… in the function, attach_websitename_to_author( $author ), I have to use if( isset( $_POST['webname'] ) ) instead of if ( $webname ). This is so exciting!

    Thank you once again for all your help. It is most appreciated.

    function comment_edit_function( $comment_id )
    {
        if( isset( $_POST['webname'] ) )
          update_comment_meta( $comment_id, 'webname', esc_attr( $_POST['webname'] ) );
    	else 
    	$_POST['webname'] = ' ';
    }
    
    add_filter( 'get_comment_author_link', 'attach_websitename_to_author' );
    
    function attach_websitename_to_author( $author ) {
        $webname = get_comment_meta( get_comment_ID(), 'webname', true );
    	if( isset( $_POST['webname'] ) )
    //    if ( $webname )
            $author .= " ($webname)";
    	elseif ($_POST['webname'] = ' ')
    		$author .= " $webname";
    //		$author .= " ";
    		return $author;
     }
    Thread Starter ejm

    (@llizard)

    Take your time, better to fully grasp what’s going on than to knock something out.

    Once again, you are wise.

    Now that I’ve got it working but am pretty certain the first set of coding falls under the category of not quite fully grasping what is going on, I’ve revised it in my WP test site:

    As you suspected would happen somewhere higher in the thread, now the website variable is showing the parentheses, even when the variable is not filled in.

    I’ve tried various riffs on the following, all producing the parentheses. What am I missing? Because there must be a way to instruct the page to show the parentheses only if that variable is filled in. Shouldn’t there?

    
    // try to show parentheses around webname ONLY if it is filled in
     
    function comment_edit_function( $comment_id )
    {
        if( isset( $_POST['webname'] ) )
          update_comment_meta( $comment_id, 'webname', esc_attr( $_POST['webname'] ) );
        else
        $_POST['webname'] = ' ';
    }
    add_filter( 'get_comment_author_link', 'attach_websitename_to_author' );
     
    function attach_websitename_to_author( $author ) {
        $webname = get_comment_meta( get_comment_ID(), 'webname', true );
        if ( $webname )
            $author .= " ($webname)";
        elseif ($_POST['webname'] = ' ')
            $author .= " $webname";
       return $author;
     }

    Here is the full coding:
    pastebin [dot] com/QHNXTwKX

    Thread Starter ejm

    (@llizard)

    That took a lot less time than expected! Good idea to add define( 'WP_DEBUG', true ); in my test WP’s config file. That was most helpful. The first thing that came up was a very easy fix on another part of the comment customizaton, once I realized the variable had to be defined inside the curly bracket of the function…

    Notice: Undefined variable: commenter in ../wp-content/themes/my-child-theme/functions.php on line 140

    function modify_comment_form_fields($fields){
    	$commenter = 0;
        $fields['url'] = '<p class="comment-form-url"><label for="url">' . __( 'Path on Web', 'domainreference' ) . '</label>' .
        '<input id="url" name="url" type="text" value="' . esc_attr( $commenter['comment_author_url'] ) .

    I was also missing opening curly bracket after if ( isset ( $_POST[‘webname’] )

    /*..........
    Update comment meta data from comment editing screen 
     ....*/ 
    function webname_comment_edit_meta_fields( $comment_id ) {
        if ( isset( $_POST['webname'] ) && '' != $_POST['webname']) {
            update_comment_meta( $comment_id, 'webname', esc_attr( $_POST['webname'] ) );
        }
    
    }

    But the main problem and the reason for the inability to edit was because I had the add-action placed erroneously inside final curly bracket of the webname_comment_edit_meta_fields( $comment_id ) function!

    add_action( 'edit_comment', 'webname_comment_edit_meta_fields' );

    Once again, many many thanks for your patient and very helpful answers, bcworkz. It’s thrilling to be able to mark this as resolved.

    Here is the full coding I ended up with (that probably still needs cleaning up, but at least it’s working!):

    /*...............
    add a field to the comment form
    ...............*/
    function add_comment_fields($newfield) {
        $newfield['webname'] = '<p class="comment-form-author" title="this field is optional"><label for="webname">' . __( 'Your Site\'s Name' ) . '</label>' .
            '<input id="webname" name="webname" type="text" size="30" /></p>';
        return $newfield;
    }
    add_filter('comment_form_default_fields','add_comment_fields');
    
    function add_comment_meta_values($comment_id) {
       if ( ( isset( $_POST['webname'] ) ) && ( $_POST['webname'] != '') ) {
    	$webname = wp_filter_nohtml_kses($_POST['webname']);
            add_comment_meta($comment_id, 'webname', $webname);
        } 
    	else    
    		$webname = '&nbsp;';
    }
    add_action ('comment_post', 'add_comment_meta_values', 1);
    
    function comment_edit_function( $comment_id )
    {
        if( isset( $_POST['webname'] ) )
          update_comment_meta( $comment_id, 'webname', esc_attr( $_POST['webname'] ) );
        else 
    		$_POST['webname'] = '&nbsp;';
    }
    
    add_filter( 'get_comment_author_link', 'attach_websitename_to_author' );
    
    function attach_websitename_to_author( $author ) {
        $webname = get_comment_meta( get_comment_ID(), 'webname', true );
        if ( $webname )
            $author .= " ($webname)";
    	elseif ($webname = '')
    		$author .= " $webname";
       return $author;
     }
    
    // Register meta box on comment edit screen
    function webname_comment_edit_add_meta_box() {
        add_meta_box( 'title', __( 'website name', 'text-domain' ), 'webname_comment_meta_box', 'comment', 'normal', 'high' );
    }
    add_action( 'add_meta_boxes_comment', 'webname_comment_edit_add_meta_box' );
    
    // Callback function for displaying the comment meta box.
    function webname_comment_meta_box( $comment ) {
        $webname = get_comment_meta( $comment->comment_ID, 'webname', true );
        wp_nonce_field( 'webname_comment_fields_update', 'webname_comment_fields_update', false );
        ?>
        <p>
            <label for="webname"><?php _e( 'Commenter\'s Site Name', 'text-domain' ); ?></label>
            <input type="text" name="webname" value="<?php echo esc_attr( $webname ); ?>" class="widefat" />
        </p><?php
    }
    
    /*..........
    Update comment meta data from comment editing screen 
     ....*/ 
    function webname_comment_edit_meta_fields( $comment_id ) {
        if ( isset( $_POST['webname'] ) && '' != $_POST['webname']) {
            update_comment_meta( $comment_id, 'webname', esc_attr( $_POST['webname'] ) );
        }
    
    }
    add_action( 'edit_comment', 'webname_comment_edit_meta_fields' );
    
    /*.......
     from wpengineer.com/2214/adding-input-fields-to-the-comment-form/
     ....*/ 
    function save_comment_meta_data( $comment_id ) {
        add_comment_meta( $comment_id, 'webname', $_POST[ 'webname' ] );
    }
    add_action( 'comment_post', 'save_comment_meta_data' );
    
    // many thanks to bcworkz for help in fixing adding custom field to comments wordpress.org/support/topic/adding-and-editing-non-required-field-in-comments/
    Thread Starter ejm

    (@llizard)

    Thank you, bcworkz! I have been away from the computer pretty much all day and have just seen your reply. It is a lot to digest…. Here’s hoping I can apply what you have kindly set out. I’ll report back in the next couple of days. Many thanks again for your patience and care!

    Thread Starter ejm

    (@llizard)

    Thank you!

    Post the code over at pastebin.com. If you sign up there (for free, but not required), you’d be able to go back and edit your code later on in case you want to show me an updated version.

    I have done as you suggested. I suspect that there may be duplicate pieces of code….

    Here is what I have in my child functions file: pastebin[dot]com/FJtVCvt2

    No matter what I do, I cannot seem to get the field to be editable and if the field is left empty, it simply does not appear at all.

    I tried it in a test WP as well, with the same results….

    Thread Starter ejm

    (@llizard)

    I fear that I may have to start all over. No matter what changes I make, everything appears to be working exactly as before: the field remains filled in if it is filled in at the outset and cannot be removed or changed; the field remains empty if it is not filled in at the outset and cannot be filled in from the admin area….

    I just noticed you have $author .= " ($webname)";. That is where the parentheses are coming from. You’re using if ( $webname ) to conditionally check for an assigned value, which is good, but now that we’re storing empty strings at times, it’s going to add ” ()” for empty strings (or a trimmed space).

    This is what I thought was going to happen if I asked it to store an empty string. But, judging from the fact that an empty field does not appear as ” ()” beside the author, I guess I have not succeeded in storing the empty string.

    I’ll keep chipping away over the next few days. Maybe, by a miracle, it will suddenly work. I’ll report back.

    Many thanks for your help. I now have a vague inkling about which functions do what.

    Thread Starter ejm

    (@llizard)

    Than you again, for your response.

    re:

    I’m following your explanation except for how the parentheses appear when the field is not filled in.

    At this point, the parentheses do not appear if the field is not filled in. But if I put in an instruction to save an empty string if the value is ”, then wouldn’t it appear in the comment meta box as ()?

    function attach_websitename_to_author( $author ) {
        $webname = get_comment_meta( get_comment_ID(), 'webname', true );
        if ( $webname )
            $author .= " ($webname)";
       return $author;
     }
    

    Not knowing how to do it (and having tried a number of incorrect bits of code) I’m guessing that I have to say something like

    if ($_POST['webname'] = '')
       echo ' ';

    This didn’t throw any errors. But it also doesn’t seem to do anything….

    re:

    [Y]ou don’t need conditional logic on output, only upon form submit. For output, just echo out whatever is in comment meta, whether it be the saved value or an empty string.

    Once again, I understand the logic here, but am not certain of which code sections are used for the form submit. Is it this part??

    function add_comment_meta_values($comment_id) {
       if ( ( isset( $_POST['webname'] ) ) && ( $_POST['webname'] != '') ) {
            $webname = wp_filter_nohtml_kses($_POST['webname']);
            add_comment_meta($comment_id, 'webname', $webname);
        } 
    }
    add_action ('comment_post', 'add_comment_meta_values', 1);
    Thread Starter ejm

    (@llizard)

    Thank you for your reply and your patience with a rank beginner.

    re:

    I don’t fully understand what you’re trying to accomplish.

    I have added an optional field “website name” to my comments form. If it is filled in, I would like it to appear in the comments area on my WP as “commenter’s name (commenter’s website name)” AND in the editing area for the comments. If it is NOT filled in, I do not want empty parentheses to appear in the comments area on my WP.

    At this point, if the field is filled in, it appears on my WP as expected, as well as appearing in the editing area for the comments in WP-admin. However, if I try to edit the empty field in WP-admin, it remains empty. If I remove an entered value on the new optional field, it stays empty and cannot be filled in again. (I hope this is making sense.)

    I will work on implementing the following so that there is an empty string.

    function() {
      if $_POST value is set,
        then save the value
      else
        save an empty string
    }

    I’m guessing I’ll also somehow have to add an elseif to the following that if the string is empty, the parentheses should NOT appear.

    function webname_comment_meta_box( $comment ) {
        $webname = get_comment_meta( $comment->comment_ID, 'webname', true );
        wp_nonce_field( 'webname_comment_fields_update', 'webname_comment_fields_update', false );
        ?>
        <p>
            <label for="webname"><?php _e( 'Website Name', 'text-domain' ); ?></label>
            <input type="text" name="webname" value="<?php echo esc_attr( $webname ); ?>" class="widefat" />
        </p><?php
    }

    Something like if $webname == '' {echo ''} ???

    Thread Starter ejm

    (@llizard)

    Thank you for your reply, bcworkz.

    That makes sense that if the field is empty, no data is sent. And intellectually, I understand what you mean by “Change the logic to always save something as long as the comment is created, regardless of the field’s isset() result. You use isset() to decide if you save an empty string or the passed value, not to decide if anything should be saved at all.

    Alas, I do not know how to implement this.

    I thought it might be with the function update_option() but as usual, the codex is written for people who probably already know the answer to the question.

    Not to mention that apparently, update_option() “has been deprecated […] in favor of update() method instead” (https://developer.wordpress.org/reference/classes/wp_customize_setting/_update_option/).

    Is https://developer.wordpress.org/reference/classes/wpdb/update/ the page that might contain the information I need?

    Because I couldn’t wrap my head around the wp developer update page I took a shot and tried removing the “if isset” with the following:

    function update_comment_meta_values($comment_id) {
    if ( $_POST['webname'] != '') {
            $webname = wp_filter_nohtml_kses($_POST['webname']);
            add_comment_meta($comment_id, 'webname', $webname);
        } 
    }
    add_action ('comment_post', 'update_comment_meta_values', 1);

    It will probably come as no surprise to you that this did not work.

    Staring more at the coding at the bottom of the page on wpdb::update, I’m guessing that this contains clues to what I am trying to achieve. Is there another place I can refer to (something a little more along the lines of “wp for dummies”) to glean the answer?

    
    public function update( $table, $data, $where, $format = null, $where_format = null ) {
        if ( ! is_array( $data ) || ! is_array( $where ) ) {
            return false;
        }
     
        $data = $this->process_fields( $table, $data, $format );
        if ( false === $data ) {
            return false;
        }
        $where = $this->process_fields( $table, $where, $where_format );
        if ( false === $where ) {
            return false;
        }
     
        $fields = $conditions = $values = [... etc. etc. ...]
    Thread Starter ejm

    (@llizard)

    followup question on this posed in Developing with WordPress forum: https://wordpress.org/support/topic/adding-and-editing-non-required-field-in-comments/

    Thread Starter ejm

    (@llizard)

    This is great. Thank you for dealing with this so quickly!

    Thread Starter ejm

    (@llizard)

    Thank you!

Viewing 15 replies - 31 through 45 (of 380 total)