Created
August 30, 2024 23:10
-
-
Save Qubadi/b9a61f5c43a9f4a3f787d97e53107c60 to your computer and use it in GitHub Desktop.
Jetformbuilder: Contact form, Jetbooking and Jetappointment, hide labels or titles when fields are empty
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Copy the following PHP code and create a PHP snippet using your snippet plugins. | |
Paste the code into the plugin and save it. | |
Hide labels or titles when fields are empty ( Send email action - content ) | |
Add this shortcode name to Send email post action ( content ) | |
[display_if_not_empty field="%your field name%" label="Your label name:"] | |
Repeat this process until all the fields | |
and the respective labels have been entered in each shortcode name. You may include as many as you want. | |
________________________________________ | |
// Shortcode to display field and label only if the field is not empty | |
function jfb_display_if_not_empty($atts, $content = null) { | |
// Define default attributes and merge with user-defined attributes | |
$defaults = array( | |
'field' => '', // The field value to check | |
'label' => '', // The label text to display | |
); | |
$atts = shortcode_atts($defaults, $atts); | |
// Initialize output variable | |
$output = ''; | |
// Check if the field value is set and not empty | |
$field_value = trim($atts['field']); | |
// If the field is not empty, append the label and field value to the output with controlled spacing | |
if ($field_value !== '') { | |
$output .= '<div style="margin-bottom: 5px;">' . esc_html($atts['label']) . ' ' . esc_html($field_value) . '</div>'; | |
} | |
// Return the output without additional spaces or gaps | |
return $output; | |
} | |
// Register the shortcode with WordPress | |
add_shortcode('display_if_not_empty', 'jfb_display_if_not_empty'); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment