Skip to content

Instantly share code, notes, and snippets.

@fastlinemedia
Last active September 1, 2021 19:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fastlinemedia/3772c26e1a51ef6ebad3941b62ad29d7 to your computer and use it in GitHub Desktop.
Save fastlinemedia/3772c26e1a51ef6ebad3941b62ad29d7 to your computer and use it in GitHub Desktop.
This is an example of looping over the Beaver Builder node data and pulling the field config for each node. You can then use that config to loop over the node settings and check what type of field the setting is.
<?php
/**
* Get the field config for a node.
*
* @param object $node
* @return array|null
*/
function wpsitesync_get_beaver_builder_fields( $node ) {
if ( 'row' === $node->type ) {
return FLBuilderModel::get_settings_form_fields( 'row', 'general' );
} elseif ( 'column' === $node->type ) {
return FLBuilderModel::get_settings_form_fields( 'col', 'general' );
} elseif ( 'module' === $node->type && isset( FLBuilderModel::$modules[ $node->settings->type ] ) ) {
return FLBuilderModel::get_settings_form_fields( $node->settings->type, 'module' );
}
return null;
}
/**
* This is an example of looping over the Beaver Builder node
* data and pulling the field config for each node. You can
* then use that config to loop over the node settings and
* check what type of field the setting is.
*/
add_action( 'wp', function() {
global $post;
if ( ! is_object( $post ) || ! class_exists( 'FLBuilder' ) ) {
return;
}
/**
* Published layout data is stored in `_fl_builder_data`. Draft layout
* data is stored in `_fl_builder_draft`.
*/
$data = get_post_meta( $post->ID, '_fl_builder_data', true );
foreach ( $data as $node ) {
$fields = wpsitesync_get_beaver_builder_fields( $node );
if ( ! $fields ) {
continue;
}
foreach ( $node->settings as $key => $value ) {
if ( isset( $fields[ $key ] ) ) {
if ( 'photo' === $fields[ $key ]['type'] ) {
// $node->settings->$key or $value is a photo ID or empty
} elseif ( 'video' === $fields[ $key ]['type'] ) {
// $node->settings->$key or $value is a video ID or empty
}
}
}
}
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment