This is a follow-up question to this one.
When hooking into the add comment form there are two hooks to use:
- comment_form_logged_in_after
- comment_form_after_fields
Am I right in assuming that the first is for logged in users and the second for anon users? Are there any other differences I should be aware of?
For the benefit of anyone coming here via a search engine, please explain as if I am a slow learner.
This is a follow-up question to this one.
When hooking into the add comment form there are two hooks to use:
- comment_form_logged_in_after
- comment_form_after_fields
Am I right in assuming that the first is for logged in users and the second for anon users? Are there any other differences I should be aware of?
For the benefit of anyone coming here via a search engine, please explain as if I am a slow learner.
Share Improve this question asked Sep 18, 2019 at 13:07 Matthew Brown aka Lord MattMatthew Brown aka Lord Matt 1,0683 gold badges13 silver badges34 bronze badges1 Answer
Reset to default 3Am I right in assuming that the first is for logged in users and the second for anon users?
Yes.
And "a picture is worth a thousand words"; so, see and compare the sample output/previews below. (The theme used is the Twenty Nineteen theme.)
1. comment_form_logged_in_after
WordPress runs this hook if the current user is logged-in.
You can use this hook to add something after the opening
<form>
tag — or more precisely, after whatever stuff added via thecomment_form_top
hook — and that something would be displayed before the comment field (which is the text box/textarea
for entering the comment text/content).This hook is basically a shortcut to using the
comment_form_top
hook withis_user_logged_in()
:// Note that WordPress runs comment_form_top regardless the user is logged-in or not. add_action( 'comment_form_top', function () { if ( is_user_logged_in() ) { echo 'comment_form_top: is logged-in<br />'; } else { echo 'comment_form_top: not logged-in<br />'; } } );
2. comment_form_after_fields
WordPress runs this hook if the current user is not logged-in.
You can use this hook to add something in between the last field and the submit button in the form — in the preview below, the last field is the one below the "Save my name, email, ..." (i.e. cookies consent).
This hook is similar to
comment_form_before_fields
which you can use to add something in between the comment field and the name field in the form.
So referring to this answer, the add_my_custom_field_to_comment_form
function is hooked to both comment_form_logged_in_after
and comment_form_after_fields
so that the custom field would appear for both authenticated and non-authenticated users.
Why so: When the user is not authenticated, extra form fields are displayed after the comment field. When the user is authenticated, then the extra form fields are not displayed by default. (Extra form fields are those other than the comment field; see $fields
.)
(Note that this answer covers the default behaviors in WordPress. Plugins and themes may change any of the behaviors, such as using CSS/JavaScript to change element's position/visibility/etc.)
And for all the currently available hooks for the comment form (i.e. comment_form()
), check this.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745138349a4613310.html
评论列表(0条)