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 |1 Answer
Reset to default 2I 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
版权声明:本文标题:filters - How to add custom field to top of Wordpress Comment Form for both logged in and anon users 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736287880a1927976.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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