I'm using a Text Area field within ACF.
I've set the "New Lines" option to "Automatically add paragraphs".
Here's how the PHP looks within my template file:
<?php
$notice = '<p>Some text.</p>';
$text = get_field( 'text', 'option' );
if ( $text ) {
echo str_replace( '<p>', '<p class="footer__paragraph">', $text );
}
else {
echo $notice;
}
?>
You'll see that I'm using str_replace
to add a custom CSS class to the <p>
tags.
I'd usually escape output, like so <?php echo esc_html( $text ); ?>
. This obviously doesn't work because autop adds a <p>
tag to each line.
Is it possible to escape output whilst maintaining my custom classes?
Yes it's feasible by whitelisting the tags and attributes you're interested in. Wordpress has a special function for that:
Here's how it works! You could pass it an array of tags that you want to accept while escaping every other tags and attributes. For example if you only need to keep you p
tag and your custom class and escape everything else, then you could do something like this:
$text = get_field( 'text', 'option' );
$whitelist_tags = array(
'p' => array(
'class' => array('footer__paragraph')
)
);
echo wp_kses( $text, $whitelist_tags );
This will keep your p
tag with your custom class and escape every other tags and attributes.
Let me know if that was what you were looking for!