Support Forum
Support › MB Settings Page › Take settings values in add_action( 'init'
Hello, in a shortcode in functions.php it work well to take the values from settings, but in add_action ('init'... the return value is empty... It's normal? How can I resolve it?
add_action( 'init', 'post_type_happyweb2', 0 );
function post_type_happyweb2() {
echo $mb_activacion_articulo = rwmb_meta( 'modulo_articulos_activacion', array( 'object_type' => 'setting' ), 'multi_tema_privado' );
echo $mb_slug_articulo = rwmb_meta( 'modulo_articulos_slug', array( 'object_type' => 'setting' ), 'multi_tema_privado' );
echo $slug_articulo = "articulo";
}
thanks.
Hi, please change the priority in the add_action
from 0 to something greater than 20. Meta Box plugin register fields at priority 20 (to make sure all custom post types and custom taxonomies are available when they also use the init
hook with priority 10). So, changing it to a number greater than 20 makes the meta boxes and fields available and you're able to use the helper functions to get the value.
I undersntand...But in the same function that I use his fields of metabox settings I registered the cpt:
I take this values in Metabox:
$mb_activacion_articulo = rwmb_meta( 'modulo_articulos_activacion', array( 'object_type' => 'setting' ), 'multi_tema_privado' );
if ($mb_activacion_articulo == 1) $show_articulo = true;
$mb_slug_articulo = rwmb_meta( 'modulo_articulos_slug', array( 'object_type' => 'setting' ), 'multi_tema_privado' );
And I registered the cpt:
'rewrite' => array('slug' => $slug_articulo),
'show_in_menu' => $show_articulo,
'show_in_nav_menus' => $show_articulo,
'show_in_admin_bar' => $show_articulo,
register_post_type( 'articulo', $args );
If I put some value priority more than 20. When I save the settings, the canghes will appear not after the refresh save, I have to refresh another time.
It's ok my explain?
Thanks
I add, don't know how, but using ACF settings, with priority 0, it run well.
thanks.
I got it. Let me summarize the process flow that you're trying to do:
If you don't do the step 3, then you can do both steps 1+2 at priority 30. The CPT will be registered without any problem.
A better solution is getting the settings with get_option
, like this:
$option = get_option( 'multi_tema_privado' );
$mb_activacion_articulo = $option['modulo_articulos_activacion'] ?? '';
if ($mb_activacion_articulo == 1) $show_articulo = true;
$mb_slug_articulo = $option['modulo_articulos_slug'] ?? '';