Take settings values in add_action( 'init'

Support MB Settings Page Take settings values in add_action( 'init'

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #13800
    proyectohappyweb@gmail.com[email protected]
    Participant

    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.

    #13804
    Anh TranAnh Tran
    Keymaster

    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.

    #13819
    proyectohappyweb@gmail.com[email protected]
    Participant

    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

    #13820
    proyectohappyweb@gmail.com[email protected]
    Participant

    I add, don't know how, but using ACF settings, with priority 0, it run well.

    thanks.

    #13855
    Anh TranAnh Tran
    Keymaster

    I got it. Let me summarize the process flow that you're trying to do:

    1. Getting settings
    2. Use the settings to register a CPT
    3. 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'] ?? '';
Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.