admin管理员组

文章数量:1122846

This is a follow up to this question.

As mentioned in that question, when hooking into the add comment form there are two hooks to use:

  • comment_form_logged_in_after
  • comment_form_after_fields

The first is for logged in users and the second for anon users. As you can see in the answer to that question, one action appears before the main Comment textarea and one appears after.

Is there a Wordpress action (or combo of actions and/or filters) to display my custom Comment field to both logged-in and non-logged in users in the same spot, i.e. immediately before the main Comment textarea?

This is a follow up to this question.

As mentioned in that question, when hooking into the add comment form there are two hooks to use:

  • comment_form_logged_in_after
  • comment_form_after_fields

The first is for logged in users and the second for anon users. As you can see in the answer to that question, one action appears before the main Comment textarea and one appears after.

Is there a Wordpress action (or combo of actions and/or filters) to display my custom Comment field to both logged-in and non-logged in users in the same spot, i.e. immediately before the main Comment textarea?

Share Improve this question asked Sep 26, 2024 at 16:50 StepppoStepppo 1033 bronze badges 2
  • The comment_form() function has a lot of filter and action hooks; do any of them suit your needs? e.g. comment_form_fields, comment_form_before, ... – Pat J Commented Sep 26, 2024 at 17:36
  • I tried out several of the hooks but there doesn't seem to be a combo that gets the custom field to display in the same place for both logged in and anon users. – Stepppo Commented Sep 26, 2024 at 17:39
Add a comment  | 

1 Answer 1

Reset to default 2

I think you could use the comment_form_field_comment() filter to add the custom HTML before the main comment field. The filter is fired for both visitors and loggedin users.

add_filter( 'comment_form_field_comment', 'wpse_426971_comment_form_field_before' );
function wpse_426971_comment_form_field_before( string $field ): string {
    return sprintf(
        '<p>%s</p>%s',
        is_user_logged_in() ? "I'm logged in" : "I'm not logged in",
        $field
    );
}

Or you can use the other dynamic comment_form_field_{$name} filters, if you want to add content before or after the other fields.

本文标签: filtersHow to add custom field to top of Wordpress Comment Form for both logged in and anon users