is it possible to put values from settings page to function register_widget()?
Support › MB Settings Page › is it possible to put values from settings page to function register_widget()?
- This topic has 4 replies, 2 voices, and was last updated 7 years, 7 months ago by
BB.
-
AuthorPosts
-
September 26, 2017 at 9:38 AM #7037
BB
Participant$bootstrap_classes is a bunch of bootstrap classes (that I get form mb settings page through the function prefix_mega_menu_widget_classes()). I want to insert into 'before widget' but it doesn't work (it returns null). But when I try to print it out into a page, it works properly.
I think, because widget init is prioritized, it runs before the setting function so the setting function (which I call in widget init function) returns null value.
Could you help me, please! Thanks in advance <3
function prefix_mega_menu_widget_classes() { $option_name = 'prefix_options'; // update option_name $columns = rwmb_meta( 'mega-menu-columns', array( 'object_type' => 'setting' ), $option_name ); if ( '4' === $columns ) { $classes = 'col-xl-3 col-lg-4 col-md-6'; } elseif ( '3' === $columns ) { $classes = 'col-lg-4 col-md-6'; } else { $classes = 'col-md-6'; } return $classes; } function prefix_init_mega_menu_widgets() { $location = 'mega-menu'; $css_class = 'has-mega-menu'; $locations = get_nav_menu_locations(); if ( isset( $locations[ $location ] ) ) { $menu = get_term( $locations[ $location ], 'nav_menu' ); if ( $items = wp_get_nav_menu_items( $menu->name ) ) { foreach ( $items as $item ) { if ( in_array( $css_class, $item->classes ) ) { $bootstrap_classes = prefix_mega_menu_widget_classes(); register_sidebar( array( 'id' => 'mega-menu-widget-area-' . $item->ID, 'name' => $item->title . ' - Mega Menu', 'before_widget' => '', 'after_widget' => '', 'before_title' => '<h3>', 'after_title' => '</h3>', ) ); } } } } } add_action( 'widgets_init', 'prefix_init_mega_menu_widgets' );
September 26, 2017 at 11:44 AM #7038Anh Tran
KeymasterHi,
As long as you saved theme settings, the settings is stored in the options table and of course, you can use the helper function to get it anytime you want, even before
init
hook. That's why when you print the value of the functionprefix_mega_menu_widget_classes
, it always work.I guess the problem is the way you register menu locations in your theme. It probably happens after
widgets_init
hook. FYI,widgets_init
hook fires atinit
with priority 0. So, if you register your menu locations atinit
with priority10
, it will run *after* and doesn't work. Try to move the code to register menu locations to priority 0. (You still need to check to make sure it runs *before*widgets_init
).September 26, 2017 at 12:19 PM #7042BB
ParticipantThank you Anh,
I register my menu like this, it displays but the value from setting page doesn't seem to be passed in. am I doing it right?if ( ! function_exists( 'prefix_theme_setup' ) ) { function prefix_theme_setup() { // Nav Menus register_nav_menus( array( 'mega-menu' => __( 'Mega Menu', 'textdomain' ) // Mega Menu ) ); } } add_action( 'after_setup_theme', 'prefix_theme_setup');
September 28, 2017 at 10:41 AM #7055Anh Tran
KeymasterYour code looks fine. I just found the problem. It's just the MB Settings Page is loaded at
init
with priority 5, which is later thanwidgets_init
(fires atinit
with priority 0). So, to solve this problem, simply replacewidgets_init
withinit
!October 1, 2017 at 3:52 PM #7074BB
Participantthank you, Anh,
I have solved my problem 🙂 -
AuthorPosts
- The topic ‘is it possible to put values from settings page to function register_widget()?’ is closed to new replies.