preg_replace and comment_form_defaults

this was kinda of a joke at the beginning but now I’m wondering if it’s possible to use regex with the hook comment_form_defaults. Here is what I’m looking for :

function remove_default_allowed_tags( $defaults) {
    $defaults = preg_replace('/<p class="form-allowed-tags">(.*?)</p>/','', $defaults);
    return $defaults;
}
add_filter('comment_form_defaults', 'remove_default_allowed_tags', 2);

I know it can be easily done with something like this :
$defaults['comment_notes_after'] = ''; return $defaults;

Read More

But I just want to know if I can use my regex in this context and if not, why.
Thanks for your answer(s).

Related posts

1 comment

  1. You can use regex anywhere you have a string to manipulate. That is basic PHP. There is nothing special about WordPress that changes that.

    But why use regex when there are other options? As much fun as it is, regex is tricky and easy to get wrong, and there is significant overhead to using it.

    What you are doing generates an “array to string conversion” Notice, by the way. preg_replace will accept an array of strings as the third parameter but $defaults['fields'] is an array so you get an Notice so it wouldn’t work as expected if you tried to alter that field.

Comments are closed.