Strange behavior — code is fine, but we have a layout bug 

Problem: Layout on the post output page have bug and does not display correctly.

A layout is broken, but HTML code is fine

Solution: At the start I would like to tell a little bit background story. On the development site I got the report about this bug. But on the localhost all it looks good. Git was synchronize. Okay, then problem contain in data base. As it turns out, customer started add contain from other site copying tags along with content. Sure, he did it not on purpose. And we could see those tags in the Text tab of WYSYWIG ACF field. And this is second moment, if we used default WordPress editor and function the_excerpt() we didn't have any problems this. For sanitize WYSYWIG ACF field we can use function strip_tags() e.g. echo strip_tags( get_field( 'content' ) ). But sometime can be cases when some tags us need. In this case, we can use next code:

function custom_strip_tag( $page_id ) {
	$original_content = get_field( 'content', $page_id );
	$patterns = [];
	$patterns[0] = '/<div[^>]*>/';
	$patterns[1] = '/<\/div>/';
	$patterns[3] = '/<p[^>]*>[\s| ]*<\/p>/';
	$replacements = '';
	$processed_content = preg_replace( $patterns, $replacements, $content );
	return $processed_content;
}

Leave Comment

Your email address will not be published.