Skip to main content

Hide a row or module when a field connection is empty

There may be times when you don't want entire rows or modules to show up when a field connection is empty.

Tip

If you don't need to hide entire rows or modules, you can use conditional shortcodes instead of PHP code to display field connections only when they return content.

You can use the following code to hide rows or modules that have empty field connections. Add it to your child theme's functions.php file.

Note

This code is just a starting point and will not work in every situation. Use your knowledge of PHP to extend this code, using the $node variable to choose which rows, columns or modules are hidden based on a number of circumstances such as $node->type or $node->settings.

function check_field_connections( $is_visible, $node ) {

if ( isset( $node->settings->connections ) ) {
foreach ( $node->settings->connections as $key => $connection ) {
if ( ! empty( $connection ) && empty( $node->settings->$key ) ) {
return false;
}
}
}

return $is_visible;
}

add_filter( 'fl_builder_is_node_visible', 'check_field_connections', 10, 2 );

Examples

The examples below are common use cases on how to hide rows, columns and modules.

Hide Row

The example below removes a row if row has a class of my-target-row.

add_filter( 'fl_builder_is_node_visible', function( $is_visible, $node ) {
if ( 'row' === $node->type ) {
if ( ! empty( $node->settings->class ) && false !== strpos( $node->settings->class, 'my-target-row' ) ) {
return false;
}
}
return $is_visible;
}, 10, 2);

Hide Column

The example below removes a column if column has a class of my-target-column.

add_filter( 'fl_builder_is_node_visible', function( $is_visible, $node ) {
if ( 'column' === $node->type ) {
if ( ! empty( $node->settings->class ) && false !== strpos( $node->settings->class, 'my-target-column' ) ) {
return false;
}
}
return $is_visible;
}, 10, 2);

Hide Module

The example below removes a module if module has a class of my-target-module.

add_filter( 'fl_builder_is_node_visible', function( $is_visible, $node ) {
if ( 'module' === $node->type ) {
if ( ! empty( $node->settings->class ) && false !== strpos( $node->settings->class, 'my-target-module' ) ) {
return false;
}
}
return $is_visible;
}, 10, 2);

Hide on specific pages

Hide row on a specific page if row has class of my-target-row and page id is 123. You can refactor this code to work on other WordPress pages such as archives or the front page. To do this you will need to use WordPress conditionals.

You can also replace the row with a column or module by changing the $node->type:

  • if ( 'column' === $node->type ) for column
  • if ( 'module' === $node->type ) for module.
add_filter( 'fl_builder_is_node_visible', function( $is_visible, $node ) {
if ( 'row' === $node->type ) {
if ( ! empty( $node->settings->class ) && false !== strpos( $node->settings->class, 'my-target-row' ) && is_page( 123 ) ) {
return false;
}
}
return $is_visible;
}, 10, 2);