Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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;

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

share|improve this question
1  
You could, but why would you run a regex pattern over the whole array when you only need to update a single key in that array? Lots of needless processing. –  t31os May 2 '13 at 14:08
    
I'm actually looking for a better way but I want to use my regex –  JMau May 2 '13 at 14:38
    
You want to use your regex to do what? And why do you want to use regex if there is some other solution? –  s_ha_dum May 2 '13 at 14:39
    
Why do you feel you need the regex? I don't follow. –  t31os May 2 '13 at 14:40
    
Guys I do not need it. Just wanted to know if it's possible. Thanks for your comments :) –  JMau May 2 '13 at 14:46
add comment

1 Answer 1

up vote 2 down vote accepted

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.

share|improve this answer
    
Ok then. I just wanted to know if it's possible, I'm not saying I want to use it every time I have to alter allowed tags. Thanks –  JMau May 2 '13 at 14:43
    
I just made it work by putting $defaults['comment_notes_after'] in the preg_match() and I didn't get any notice. So it works. Thanks ! Again, I do not encourage people using that. –  JMau May 5 '13 at 14:59
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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