2

I want to get rid of undesired HTML, mostly being published by my editors when copying text from other sources, like other pages or from Word

Therefore I use

add_filter( 'content_save_pre' , 'my_function', 10, 1 );

for the content in posts.

I want to do the same for the taxonomies/terms and their description, but I didn't find any filter in the WordPress documentation, nor could I find anything in the code.

4

There are e.g. the following dynamic sanitize_term_fields() filters:

  • edit_term_{$field} filter for the edit context
  • pre_term_{$field} filter for the the db context
  • term_{$field}_rss filter for the the rss context
  • term_{$field} filter (default)

and also the taxonomy-specific ones:

  • edit_{$taxonomy}_{$field} filter for the edit context
  • pre_{$taxonomy}_{$field} filter for the the db context
  • {$taxonomy}_{$field}_rss filter for the the rss context
  • {$taxonomy}_{$field} filter (default)

If the context is raw then none of the above filters are applied.

The db context is used within the wp_insert_term() and wp_update_term():

$args = sanitize_term($args, $taxonomy, 'db');

There's also the pre_insert_term filter wihtin wp_insert_term().

/**
 * Filters a term before it is sanitized and inserted into the database.
 *
 * @since 3.0.0
 *
 * @param string $term     The term to add or update.
 * @param string $taxonomy Taxonomy slug.
 */
 $term = apply_filters( 'pre_insert_term', $term, $taxonomy );

Here are simple demo examples for the term description in the case of the post tags taxonomy:

edit context:

add_filter( 'edit_post_tag_description', 'wp_strip_all_tags' );

db context:

add_filter( 'pre_post_tag_description', 'wp_strip_all_tags' );

You might want to test and restrict this further to your needs.

Note: It might be better to just strip it where you output the term description on the front-end, so you don't mess with the user input in the backend?

  • That looks pretty much like what I need. Is there a generic one, that fires for all taxonomies? Or do I have to hook into all manually. – Christoph Daum Jun 21 '16 at 8:52
  • Since both wp_insert_term() and wp_date_term() use it in the "db" context, you could try that first. Yes we can adjust it for all taxonomies, I will update the answer @ChristophDaum – birgire Jun 21 '16 at 9:04

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.