What are potential pitfalls when outputting PHP code in WordPress, especially when dealing with conditional display of fields?
When outputting PHP code in WordPress, especially when dealing with conditional display of fields, a common pitfall is forgetting to properly escape the output to prevent security vulnerabilities like Cross-Site Scripting (XSS) attacks. To solve this issue, always use WordPress functions like `esc_html`, `esc_attr`, or `esc_url` to sanitize and escape the output.
<?php
// Example of outputting a field value with conditional display
$field_value = get_field('custom_field');
if ($field_value) {
echo esc_html($field_value);
}
?>