Extending meta_boxes arrays
- This topic has 2 replies, 2 voices, and was last updated 8 years, 10 months ago by
Anh Tran.
-
AuthorPosts
-
June 20, 2016 at 2:58 PM #3497
carassius
ParticipantI am creating a plugin that I want to release addons for.
When I create the new addon I want to be able to add tabs and fields to the existing settings page?
Any ideas on how to achieve that?
This is my main function
function bl_extensions_options_meta_boxes( $meta_boxes ) { $meta_boxes[] = array( 'id' => 'options', 'title' => __( 'Extensions Options', 'fl-builder' ), 'settings_pages' => 'builder-extensions', 'tabs' => array( 'general' => array( 'label' => __( 'General', 'fl-builder' ), 'icon' => 'dashicons-admin-tools', ), 'popup' => array( 'label' => __( 'OnClick Popup', 'fl-builder' ), 'icon' => 'dashicons-admin-plugins', ), ), 'fields' => array( array( 'name' => __( 'Panel Name', 'fl-builder' ), 'id' => 'branding', 'type' => 'text', 'std' => 'Extensions', 'tab' => 'general', ), array( 'name' => __( 'Enable Bootstrap', 'fl-builder' ), 'id' => 'bootstrap', 'type' => 'checkbox', 'tab' => 'popup', ), ), ); return $meta_boxes; } add_filter( 'rwmb_meta_boxes', 'bl_extensions_options_meta_boxes' );
And then this is how I am trying to extend that function with a addon plugin
function animate_module_tab_setup( $meta_boxes ) { $meta_boxes[] = array( 'settings_pages' => 'builder-extensions', 'tabs' => array( 'animate' => array( 'label' => __( 'Animated Text', 'fl-builder' ), 'icon' => 'dashicons-editor-textcolor', ), ), 'fields' => array( array( 'name' => __( 'Panel Name', 'fl-builder' ), 'id' => 'text', 'type' => 'text', 'tab' => 'animate', ), ), ); return $meta_boxes; } add_filter( 'rwmb_meta_boxes', 'animate_module_tab_setup' );
June 20, 2016 at 3:43 PM #3500carassius
ParticipantOk so I tried going the parent route
these are my main functions to add the menu pages and the fields
// Create Extensions Page function bl_extensions_options_page( $settings_pages ) { $settings_pages[] = array( 'id' => 'builder-extensions', 'option_name' => 'builder_extensions', 'menu_title' => __( 'Page Builder Extensions', 'fl-builder' ), 'icon_url' => 'dashicons-images-alt', 'submenu_title' => __( 'General', 'fl-builder' ), // Note this ); return $settings_pages; } add_filter( 'mb_settings_pages', 'bl_extensions_options_page' ); // Add Fields to Extensions Page function bl_extensions_options_meta_boxes( $meta_boxes ) { $meta_boxes[] = array( 'id' => 'options', 'title' => __( 'Extensions Options', 'fl-builder' ), 'settings_pages' => 'builder-extensions', 'fields' => array( array( 'name' => __( 'Panel Name', 'fl-builder' ), 'id' => 'branding', 'type' => 'text', 'std' => 'Extensions', ), array( 'name' => __( 'Enable Bootstrap', 'fl-builder' ), 'id' => 'bootstrap', 'type' => 'checkbox', ), ), ); return $meta_boxes; } add_filter( 'rwmb_meta_boxes', 'bl_extensions_options_meta_boxes' );
and then to try an extend it I added this to the next plugin
// Create Extensions Page function animated_text_settings_page( $settings_pages ) { $settings_pages[] = array( 'id' => 'animated-text-module', 'option_name' => 'builder_extensions', 'menu_title' => __( 'Animated Text', 'fl-builder' ), 'parent' => 'builder-extensions', ); return $settings_pages; } add_filter( 'mb_settings_pages', 'animated_text_settings_page' ); // Add Fields to Extensions Page function animate_module_fields( $meta_boxes ) { $meta_boxes[] = array( 'id' => 'animated_text', 'title' => __( 'Animated Text Options', 'fl-builder' ), 'settings_pages' => 'animated-text-module', 'fields' => array( array( 'name' => __( 'Custom Field', 'fl-builder' ), 'id' => 'my_custom_field', 'type' => 'text', ), ), ); return $meta_boxes; } add_filter( 'rwmb_meta_boxes', 'animate_module_fields' );
The issue with this setup is that the Animated Text appears as a sub menu to the parent item, but when I click on it, it takes me to a 404 page like this for example
http://mydomain.com/wp-admin/animated-text-module
June 20, 2016 at 4:27 PM #3501Anh Tran
KeymasterHi,
I think you can use a trick to find the correct meta box for your settings page and add/edit fields/tabs for it.
The trick is using a key for meta boxes, like this:
$meta_boxes['prefix_settings_page_id'] = array();
And in your extension:
$meta_boxes['prefix_settings_page_id']['tabs'][] = array(); $meta_boxes['prefix_settings_page_id']['fields'][] = array();
Just make sure the extension hooks into the
rwmb_meta_boxes
after the main plugin.This technique is covered partly in this documentation https://metabox.io/docs/edit-remove-meta-boxes/.
-
AuthorPosts
- The topic ‘Extending meta_boxes arrays’ is closed to new replies.