Take settings values in add_action( 'init'
Support › MB Settings Page › Take settings values in add_action( 'init'
- This topic has 4 replies, 2 voices, and was last updated 6 years, 1 month ago by
Anh Tran.
-
AuthorPosts
-
March 19, 2019 at 11:24 PM #13800
[email protected]
ParticipantHello, 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.
March 20, 2019 at 9:19 AM #13804Anh Tran
KeymasterHi, 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 theinit
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.March 20, 2019 at 11:46 PM #13819[email protected]
ParticipantI 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
March 20, 2019 at 11:47 PM #13820[email protected]
ParticipantI add, don't know how, but using ACF settings, with priority 0, it run well.
thanks.
March 22, 2019 at 9:23 AM #13855Anh Tran
KeymasterI got it. Let me summarize the process flow that you're trying to do:
- Getting settings
- Use the settings to register a CPT
- Register custom fields for the CPT
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'] ?? '';
-
AuthorPosts
- You must be logged in to reply to this topic.