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()?

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #7037
    BBBB
    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' );
    #7038
    Anh TranAnh Tran
    Keymaster

    Hi,

    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 function prefix_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 at init with priority 0. So, if you register your menu locations at init with priority 10, 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).

    #7042
    BBBB
    Participant

    Thank 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');
    #7055
    Anh TranAnh Tran
    Keymaster

    Your 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 than widgets_init (fires at init with priority 0). So, to solve this problem, simply replace widgets_init with init!

    #7074
    BBBB
    Participant

    thank you, Anh,
    I have solved my problem 🙂

Viewing 5 posts - 1 through 5 (of 5 total)
  • The topic ‘is it possible to put values from settings page to function register_widget()?’ is closed to new replies.