Skip to content

Hide Rows or Elements conditionally

There are differen approaches to conditionally hide elements or rows in the total theme. This is what i have so far. Read more in my support thread.

/**
 * Add Custom Dynamic Variables.
 */
add_filter( 'totaltheme/replace_vars/vars', function( $vars ) {

	$post_id = get_the_ID();

	if ( ! $post_id ) {
		return '';
	}

	// Custom Field laden
	$enym_index = get_post_meta( $post_id, 'enym_index', true );

	if ( $enym_index === '' ) {
		$enym_index_out = '';
	}

	if ( (string) $enym_index === '1' ) {
		$enym_index_out = 'wpex-hidden';
	}
	
	$vars['hide_index'] = esc_html( $enym_index_out );


    return $vars;
} );


function enym_index_shortcode() {
    // Aktuelle Post-ID ermitteln
    $post_id = get_the_ID();

    if ( ! $post_id ) {
		return '';
	}

	// Custom Field laden
	$enym_index = get_post_meta( $post_id, 'enym_index', true );

	if ( $enym_index === '' ) {
		return '';
	}

	// Wenn Wert 1 ist → "hidden" ausgeben
	if ( (string) $enym_index === '1' ) {
		return 'wpex-hidden';
	}

	// Sicher ausgeben
	return esc_html( $enym_index );
}

add_shortcode( 'enym_index', 'enym_index_shortcode' );



function phalancs_check_var_classname( $atts ) {
	$check = true;
	$el_class = $atts['el_class'] ?? $atts['classes'] ?? $atts['class'] ?? ''; // get element class.
	$var_to_check = '';

	// Check if our "phalancs-check-var-{var_name}" class exists.
	if ( $el_class && str_contains( $el_class, 'phalancs-check-var-' ) ) {

		// Turn class into array and loop through classes and locate our variable classname.
		// This is incase the field has multiple classnames.
		foreach ( explode( ' ', $el_class ) as $classname ) {
			// We found our classname so lets extract the variable only.
			if ( str_starts_with( $classname, 'phalancs-check-var-' ) ) {
				$var_to_check = str_replace( 'phalancs-check-var-', '', $classname );
				break;
			}
		}
	}
	
	// Once we located are variable lets run it through the replacement function and set our check variable to true/false based on the result.
	if ( $var_to_check && function_exists( 'totaltheme_replace_vars' ) ) {
		$check = (bool) totaltheme_replace_vars( "{{{$var_to_check}}}" );
	}

	return $check;
}

// Hook into the theme elements filter that returns true/false if the element should display.
add_filter( 'vcex_maybe_display_shortcode', function( $shortcode_tag, $atts ) {
	if ( ! phalancs_check_var_classname( $atts ) ) {
		return false;
	}
	return true;
}, 50, 2 );

// Hook into WPBakery elements to return nothing for the shortcode output if it shouldn't display.
add_filter( 'vc_shortcode_output', function( $output, $obj, $atts, $shortcode ) {
	if ( ! phalancs_check_var_classname( $atts ) ) {
		return '';
	}
	return $output;
}, 50, 5 );

An den Anfang scrollen