0

I hook to comment form (on comment_form_logged_in_after if user is logged in and comment_form_before_fields if not) to add 2 fields (rating and title).

These fields can be mandatory or comment will not save. These fields will not appear on comment reply or if review are not enabled for a certain post or page.

If these fields are mandatory, I've to check on comment_post (just before to save comment) if filter has run, and if not just save the comment withoud any comment_meta.

The problem is that if I try to use

has_filter('comment_form_before_fields', 'my_function_to_check'); 

when I hook on comment_post it ALWAYS return FALSE.

If, for example, I try to show

var_dump(has_filter('comment_form_before_fields', 'my_function_to_check')) 

on comment_text filter it works.

How can I do this?

4
  • Where is the filter my_function_to_check on comment_form_before_fields added? Could you post all relevant code — especially where the filter is added and when the function that adds the filter is called.
    – engelen
    Mar 31, 2016 at 9:47
  • thank's for the answer, this is an example: add_action( 'comment_form_logged_in_after', 'add_input_to_comment_form' ); this add_action is inside a function hooked on comment_form_before
    – Dudo1985
    Mar 31, 2016 at 9:50
  • Adding an action can be done at any point, basically, after WordPress is initiated. Could you try adding it on action init instead of on comment_form_before?
    – engelen
    Mar 31, 2016 at 9:59
  • Good :-). I'll add an answer to the question so it can be marked as answered.
    – engelen
    Mar 31, 2016 at 10:07

1 Answer 1

1

You're adding the action on comment_form_before_fields in comment_form_logged_in_after. The latter is called only when the user is logged in, and the former is only called when the user is not logged in.

You can add WordPress actions and filters at any time after WordPress is initialized. If you're adding the action in a plugin, add it on init or even when the code is first executed (i.e. not on an action but directly in the plugin).

This way, the action is added immediately and called when it needs to be called.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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