$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' );