Skip to content

Instantly share code, notes, and snippets.

@nathaningram
Last active December 1, 2025 20:49
Show Gist options
  • Select an option

  • Save nathaningram/44d02e3ed5af66a47d397598a963336e to your computer and use it in GitHub Desktop.

Select an option

Save nathaningram/44d02e3ed5af66a47d397598a963336e to your computer and use it in GitHub Desktop.
Starter Site 2025 - MU Beaver Builder
<?php
/*
Plugin Name: Custom Functions for Beaver Builder
Plugin URI: https://wpnathan.com
Description: Extend and Customize Beaver Builder
Version: 1.0
Author: Nathan Ingram
Author URI: https://wpnathan.com
License: GPL2
*/
// Security Check
if (!defined('ABSPATH')) {
die();
}
// Allow Beaver Builder modules to use custom image sizes
add_filter('image_size_names_choose', 'ni_insert_custom_image_sizes');
function ni_insert_custom_image_sizes($sizes) {
global $_wp_additional_image_sizes;
if (empty($_wp_additional_image_sizes)) {
return $sizes;
}
foreach ($_wp_additional_image_sizes as $id => $data) {
if (!isset($sizes[$id])) {
$sizes[$id] = ucfirst(str_replace('-', ' ', $id));
}
}
return $sizes;
}
// Enable SVG + WebP Uploads in Beaver Builder Modules
add_filter( 'fl_module_upload_regex', function( $regex, $type, $ext, $file ) {
$regex['photo'] = '#(jpe?g|png|gif|bmp|tiff?|svg|webp)#i';
return $regex;
}, 10, 4 );
// Add a Notes Field to BvB Modules
function ni_register_notes( $form, $slug ) {
if ( 'module_advanced' === $slug ) {
$form[ 'sections' ][ 'css_selectors' ][ 'fields' ][ 'bt_notes' ] = [
'type' => 'textarea',
'label' => 'Developer Notes',
'placeholder' => 'Notes',
'rows' => '6'
];
}
if ( 'col' === $slug || 'row' === $slug ) {
$form[ 'tabs' ][ 'advanced' ][ 'sections' ][ 'css_selectors' ][ 'fields' ][ 'bt_notes' ] = [
'type' => 'textarea',
'label' => 'Developer Notes',
'placeholder' => 'Notes',
'rows' => '6'
];
}
return $form;
}
add_filter( 'fl_builder_register_settings_form', 'ni_register_notes', 100, 2 );
// Removes Empty Modules in Themer if field is empty
function ni_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', 'ni_check_field_connections', 10, 2 );
// Beaver Builder Module Enqueued Google Fonts
// From here https://docs.wpbeaverbuilder.com/beaver-builder/developer/how-to-tips/load-google-fonts-locally-gdpr/
add_filter( 'fl_builder_google_fonts_pre_enqueue', function( $fonts ) {
return array();
} );
add_action( 'wp_enqueue_scripts', function() {
global $wp_styles;
if ( isset( $wp_styles->queue ) ) {
foreach ( $wp_styles->queue as $key => $handle ) {
if ( false !== strpos( $handle, 'fl-builder-google-fonts-' ) ) {
unset( $wp_styles->queue[ $key ] );
}
}
}
}, 101 );
// Remove WP Colors from BVB Global Picker
add_filter( 'fl_builder_global_colors_json', function( $json ) {
unset( $json['themeJSON']['color']['palette']['default'] );
return $json;
} );
// Add Delete Cache to Beaver Builder Admin Menu Item
class BB_Delete_Cache_Admin_Bar {
public function __construct() {
add_action( 'init', array( $this, 'load_textdomain' ) );
add_action( 'admin_bar_menu', array( $this, 'add_item' ) );
add_action( 'admin_post_purge_cache', array( $this, '__clear_cache' ) );
}
public function load_textdomain() {
load_plugin_textdomain( 'bb-delete-cache', false, basename( dirname( __FILE__ ) ) . '/languages' );
}
/**
* Add's new global menu, if $href is false menu is added but registered as submenuable
*
* $id String
* $title String
* $href String
* $parent String
* $meta Array
*
* @return void
*/
public function add_menu( $id, $title, $href = false, $meta = false, $parent = false ) {
global $wp_admin_bar;
if ( ! is_super_admin() || ! is_admin_bar_showing() ) {
return;
}
$wp_admin_bar->add_menu(
array(
'id' => $id,
'parent' => $parent,
'title' => $title,
'href' => $href,
'meta' => $meta,
)
);
}
/**
* Add's new submenu where additional $meta specifies class, id, target, or onclick parameters
*
* $id String
* $parent String
* $title String
* $href String
* $meta Array
*
* @return void
*/
public function add_sub_menu( $id, $parent, $title, $href, $meta = false ) {
global $wp_admin_bar;
if ( ! is_super_admin() || ! is_admin_bar_showing() ) {
return;
}
$wp_admin_bar->add_menu(
array(
'id' => $id,
'parent' => $parent,
'title' => $title,
'href' => $href,
'meta' => $meta,
)
);
}
public function add_item() {
global $post;
$referer = '&_wp_http_referer=' . urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) );
$action = 'purge_cache';
if ( ! is_admin() ) {
// Purge a URL (frontend)
if ( ! is_null( $post ) ) {
$this->add_sub_menu(
'bb-delete-url-cache',
'fl-builder-frontend-edit-link',
__( 'Clear this post', 'bb-delete-cache' ),
wp_nonce_url( admin_url( 'admin-post.php?action=' . $action . '&type=post-' . $post->ID . $referer ), $action . '_post-' . $post->ID )
);
}
// Purge All
$this->add_sub_menu(
'bb-delete-all-cache',
'fl-builder-frontend-edit-link',
__( 'Clear cache', 'bb-delete-cache' ),
wp_nonce_url( admin_url( 'admin-post.php?action=' . $action . '&type=all' . $referer ), $action . '_all' )
);
}
}
public function __clear_cache() {
if ( isset( $_GET['type'], $_GET['_wpnonce'] ) ) {
$_type = explode( '-', sanitize_text_field( wp_unslash( $_GET['type'] ) ) );
$_type = reset( $_type );
$_id = explode( '-', sanitize_text_field( wp_unslash( $_GET['type'] ) ) );
$_id = end( $_id );
if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'purge_cache_' . sanitize_text_field( wp_unslash( $_GET['type'] ) ) ) ) {
wp_nonce_ays( '' );
}
switch ( $_type ) {
// Clear all cache
case 'all':
FLBuilderModel::delete_asset_cache_for_all_posts();
break;
// Clear a current post
case 'post':
FLBuilderModel::delete_all_asset_cache( $_id );
break;
default:
wp_nonce_ays( '' );
break;
}
wp_redirect( wp_get_referer() );
die();
}
}
}
function bb_delete_cache_admin_bar_init() {
if ( class_exists( 'FLBuilder' ) ) {
$bb_delete_cache_admin_bar = new BB_Delete_Cache_Admin_Bar();
}
}
add_action( 'plugins_loaded', 'bb_delete_cache_admin_bar_init' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment