I'm building a WordPress theme that uses the Advanced Custom Fields (ACF plugin). I have the following function via functions.php:
function filter_p_tags( $content ) {
$content = str_replace( '<p>','<p class="custom__class">', $content );
return $content;
}
add_filter('the_content', 'filter_p_tags');
add_filter('acf_the_content','filter_p_tags');
The <p>
tags via posts and pages are successfully being replaced with <p class="custom__class">
. However, my ACF fields are not being filtered. What am I doing wrong here?
It's worth mentioning that the ACF fields in question belong to an options page. Here's how an option field looks within one of my templates.
<?php the_field( 'text', 'option' ); ?>
If your ACF field is a textarea
, then you would want to use acf/format_value/type=textarea
filter hook instead of using acf_the_content
which would be applied on wysiwyg
.
add_filter('acf/format_value/type=textarea', 'filter_p_tags_acf', 10, 3);
So your entire code would be something like this:
add_filter('the_content', 'filter_p_tags');
function filter_p_tags( $content ) {
$content = str_replace( '<p>','<p class="custom__class">', $content );
return $content;
}
add_filter('acf/format_value/type=textarea', 'filter_p_tags_acf', 10, 3);
function filter_p_tags_acf( $value, $post_id, $field ) {
$value = str_replace( '<p>','<p class="custom__class">', $value );
return $value;
}
Alternatively, as you suggested, we could use acf/format_value/key={$key}
filter hook instead of using acf/format_value/type=textarea
. Like so:
add_filter('acf/format_value/key=field_abc123456', 'filter_p_tags_acf', 10, 3);
function filter_p_tags_acf( $value, $post_id, $field ) {
$value = str_replace( '<p>','<p class="custom__class">', $value );
return $value;
}