Adding a menu item to the Beaver Builder admin menu

Beaver Builder has a top level menu item in the WordPress admin menu. However, it doesn’t allow new menu items to be added using the standard WordPress methods. After reading a bunch of the Beaver Builder code base, I was able to find the filter that handles the submenu items in the /extensions/fl-builder-user-templates/classes/class-fl-builder-user-templates-admin-menu.php file.

The filter is called fl_builder_user_templates_admin_menu and is applied to the $submenu array on line 53 (as of BB 2.8).

Before we add our own submenu item, it is helpful to see how they have constructed the $submenu array:

$submenu[ $parent ]      = array();
$submenu[ $parent ][100] = array( __( 'Templates', 'fl-builder' ), $cap, $list_url . 'layout' );
$submenu[ $parent ][200] = array( __( 'Saved Rows', 'fl-builder' ), $cap, $list_url . 'row' );
$submenu[ $parent ][300] = array( __( 'Saved Columns', 'fl-builder' ), $cap, $list_url . 'column' );
$submenu[ $parent ][400] = array( __( 'Saved Modules', 'fl-builder' ), $cap, $list_url . 'module' );
$submenu[ $parent ][500] = array( __( 'Categories', 'fl-builder' ), $cap, $cats_url );
$submenu[ $parent ][700] = array( __( 'Add New', 'fl-builder' ), $cap, 'fl-builder-add-new', '' );

As you can see, each submenu has an ID and an array of settings:

  1. Menu title
  2. $cap: This is the user capability needed to access the page.
  3. $list_url: This is the URL of the page to be displayed.

Now that we understand how the Beaver Builder $submenu array works, we can use their filter to add our own page to the submenu.

function prefix_add_bb_submenu( $submenu ) {

	// Loop through existing submenu items
	foreach ( $submenu as $key => $value ) {

		// Is the submenu key greater than our new submenu item?
		if ( $key > 410 ) {

			// Add our submenu
			$submenu_update[410] = array(
				'Menu Title',
				'edit_posts',
				'edit.php?post_type=cpt'
			);
		}

		// Add the old submenu item to the new submenu
		$submenu_update[$key] = $value;
	}

	return $submenu_update;
}
add_filter( 'fl_builder_user_templates_admin_menu', 'prefix_add_bb_submenu' );

In this case, we used a key of 410, which will put the new menu item just below the Saved Modules menu item. We could have used any key between 401 and 499 to accomplish this. To place the new menu item in a different location withing the submenu, we can simply adjust our key based on the keys listed in the $submenu array shown above.

The last thing to notice about this code is the URL we’re using: edit.php?post_type=cpt

This URL would point to a custom post type with a slug of cpt. To point to a different page, this string will need to be adjusted accordingly.

Posted in

Leave a Comment

Your email address will not be published. Required fields are marked *