WordPress.org

Make WordPress Core

Opened 5 years ago

Last modified 8 days ago

#16576 new enhancement

comment_form() fields being displayed only for non logged in users

Reported by: maorb Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 3.0.5
Component: Comments Keywords: dev-feedback has-patch needs-testing
Focuses: Cc:

Description

I've just noticed this - When using the comment_form() function and adding some comment meta fields,using the fields array in the $args, these fields are being showed in the front-end only for non logged in users.
In this case, the registered users can never use these comment fields.

Look at wp-includes/comment-template, lines 1561-1573 (WP 3.0.5), it parse the $argsfields? in the else block -

<?php if ( is_user_logged_in() ) : ?>
	<?php echo apply_filters( 'comment_form_logged_in', $args['logged_in_as'], $commenter, $user_identity ); ?>
	<?php do_action( 'comment_form_logged_in_after', $commenter, $user_identity ); ?>
<?php else : ?>
	<?php echo $args['comment_notes_before']; ?>
	<?php
	do_action( 'comment_form_before_fields' );
	foreach ( (array) $args['fields'] as $name => $field ) {
		echo apply_filters( "comment_form_field_{$name}", $field ) . "\n";
	}
	do_action( 'comment_form_after_fields' );
	?>
<?php endif; ?>

I think it is better that added meta fields should be displayable for all users (logged in and not logged in), or even to add a parameter to let the developer decide which extra fields are shown to logged or not logged users.

Attachments (1)

16576.diff (2.3 KB) - added by valendesigns 10 months ago.

Download all attachments as: .zip

Change History (10)

comment:2 @valendesigns4 years ago

I agree, it should be an option. Just because you're logged in doesn't mean you have set your URL in your profile and there is still the chance that users are subscribers and you hide the admin from them. I'm currently dealing with this. I was using the comment_form() function but just realized the inputs are hidden when logged in and our system doesn't allow them to edit their WP profile because we use aMember to authenticate and maintain profile data. Back to regular manual comment forms I guess.

comment:3 @chriscct710 months ago

  • Keywords dev-feedback added

comment:4 @valendesigns10 months ago

Hey Guys,

I would love to work on this ticket as my first contribution to the core, but a group consensus should be reached about how to approach the subject before I start coding anything.

How about adding an additional argument that if set to true (defaults to false) will loop over the $args['fields'] in the logged in block. However, adding the loop again isn't very DRY and would still need to be distilled down to something that doesn't repeat the $args['fields'] loop by creating a new function that looks something like this...

function comment_form_fields( $fields = array() ) {
  if ( ! is_array( $fields ) )
    return false;

  /**
   * Fires before the comment fields in the comment form.
   *
   * @since 3.0.0
   */
  do_action( 'comment_form_before_fields' );
  foreach ( (array) $fields as $name => $field ) {
    /**
     * Filter a comment form field for display.
     *
     * The dynamic portion of the filter hook, $name, refers to the name
     * of the comment form field. Such as 'author', 'email', or 'url'.
     *
     * @since 3.0.0
     *
     * @param string $field The HTML-formatted output of the comment form field.
    echo apply_filters( "comment_form_field_{$name}", $field ) . "\n";
  }
  /**
   * Fires after the comment fields in the comment form.
   *
   * @since 3.0.0
   */
  do_action( 'comment_form_after_fields' );
}

Then it could be used in both if/else blocks without any repetition being added to the code. It's a simple solution. If anyone else has any suggestions or thinks this is bad idea please let me know. I'm eager to start contributing to the core and this looks like a quick and easy fix for me to get started with.

Cheers,
Derek

comment:5 @valendesigns10 months ago

Actually, an alternative to the above would be to change up the if/else block. This would assume that there is an argument called $args['show_fields_logged_in'] to work, but the argument could be named anything. I think this would be a better solution because there is very little meddling in the code and very little needs to be changed in the docs to support it.

<?php if ( is_user_logged_in() ) : ?>
	<?php
	/**
	 * Filter the 'logged in' message for the comment form for display.
	 *
	 * @since 3.0.0
	 *
	 * @param string $args_logged_in The logged-in-as HTML-formatted message.
	 * @param array  $commenter      An array containing the comment author's
	 *                               username, email, and URL.
	 * @param string $user_identity  If the commenter is a registered user,
	 *                               the display name, blank otherwise.
	 */
	echo apply_filters( 'comment_form_logged_in', $args['logged_in_as'], $commenter, $user_identity );
	?>
	<?php
	/**
	 * Fires after the is_user_logged_in() check in the comment form.
	 *
	 * @since 3.0.0
	 *
	 * @param array  $commenter     An array containing the comment author's
	 *                              username, email, and URL.
	 * @param string $user_identity If the commenter is a registered user,
	 *                              the display name, blank otherwise.
	 */
	do_action( 'comment_form_logged_in_after', $commenter, $user_identity );
	?>
<?php endif; ?>
<?php if ( ! is_user_logged_in() || true === $args['show_fields_logged_in'] ) : ?>
	<?php echo $args['comment_notes_before']; ?>
	<?php
	/**
	 * Fires before the comment fields in the comment form.
	 *
	 * @since 3.0.0
	 */
	do_action( 'comment_form_before_fields' );
	foreach ( (array) $args['fields'] as $name => $field ) {
		/**
		 * Filter a comment form field for display.
		 *
		 * The dynamic portion of the filter hook, $name, refers to the name
		 * of the comment form field. Such as 'author', 'email', or 'url'.
		 *
		 * @since 3.0.0
		 *
		 * @param string $field The HTML-formatted output of the comment form field.
		 */
		echo apply_filters( "comment_form_field_{$name}", $field ) . "\n";
	}
	/**
	 * Fires after the comment fields in the comment form.
	 *
	 * @since 3.0.0
	 */
	do_action( 'comment_form_after_fields' );
	?>
<?php endif; ?>

@valendesigns10 months ago

comment:6 @valendesigns10 months ago

  • Keywords has-patch needs-testing added

I've added a patch that's a bit different than both of my previous suggestions.

I moved all of the fields related code out of the else block following the if ( is_user_logged_in() ) condition; it now comes directly after the endif for that code block. Then added a check for is_user_logged_in() inside the $args['fields'] loop that will skip the 'author', 'email', & 'url' array keys. Which means custom fields will be displayed to both sets of users, but the default fields will only be visible to those not logged in. This way we don't need any extra documentation or to add arguments to the comment_form() function.

comment:7 follow-up: @Yahire Furniture10 months ago

Did you manage to sort it in the end? I am having a similar issue.

comment:8 in reply to: ↑ 7 @valendesigns10 months ago

Replying to Yahire Furniture:

Did you manage to sort it in the end? I am having a similar issue.

Yes, the patch fixes the issue. However, it doesn't seem to be gaining traction with the core developers just yet. Hopefully we'll get someone in here to give feedback soon.

Cheers,
Derek

comment:9 @CarlSteffen10 days ago

Hello - I see this topic hasn't received any comments in a while - this particular issue is killing me - the proposed solution would fix my issue but i certainly see how this change could cause unexpected (negative) changes to sites that have build around this restriction/bug.

Are there any updates?

Thanks,

Carl

NOTE - I was able to work around this issue by replacing the comment field with custom HTML that included my input fields.

        $commentField = '<p class="comment-form-comment"><label for="comment">' . _x( 'Comment', 'noun' ) . '</label> <textarea id="comment" name="comment" cols="45" rows="8"  aria-required="true" required="required"></textarea></p>'. addMySpecialStuff($post->ID);
        $comment_args = array(
            'comment_notes_after'=>'',
            'logged_in_as'=>'',
            'title_reply'=>'',
            'comment_field'        =>$commentField,
        );

where the addMySpecialStuff function was simply appending the HTML I wanted after the comment field and the submit button.

Last edited 8 days ago by CarlSteffen (previous) (diff)
Note: See TracTickets for help on using tickets.