1

If I want to globally change the comment form for a theme I would use add_filter for "comment_form_default_fields". Then I might use add_action for "comment_form_logged_in_after" and "comment_form_after_fields". What I want to do, however, is create a single page with customised comment form fields.

My first thought was to add filters and use hooks in a custom page template. But that will, at best, work for one or two sites. What I am hoping to find is another way that I can target a chosen page. Is there a hook, filter, or logical test I can apply to modify a single page?

2
  • 1
    are you using the body_class and post_class functions? If so the classes they add can be used to do this for styling. I'm unsure how to add markup though
    – Tom J Nowell
    Apr 18, 2021 at 15:26
  • Thanks, Tom, if it were just a styling issue that would be a great approach. I'm looking to alter behaviour and add fields but only for one page. Apr 18, 2021 at 15:37

1 Answer 1

1

I think I would go with a customizer setting, which a user could use to select a page (select) or pages (checkboxes) where the custom comment fields would be shown. The setting should save page ID(s).

Then I'd add a callback function to template_redirect action, which would first get the customizer setting value with get_theme_mod() and then compare the current query against the saved value. This could be either a is_page() check, if the user can only select one page, or a is_array() check with the get_queried_object_id() value, if the the user was allowed to choose multiple pages and the saved value was an array or turned into one.

If the above conditional check is passed, then appropriate action and filter callbacks would be applied to the comment form inside the template_redirect callback.

Or, if you want to use a custom page template instead of the customizer setting, then you could just swap the above conditional checks with is_page_template() and keep the concept otherwise the same.

But with customizer you could also provide options to the user to choose, which additional fields should be shown on the comment form, if needed. Each custom field should then have its own "is enabled" check and rendering callback in the template_redirect callback.

P.s. I'm sorry for not adding code examples. I'm typing this on my mobile device and adding code this way is a bit inconvenient.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.