• Resolved piotrdeja

    (@piotrdeja)


    How to place my CMB2 fields inside options page created by ‘add_options_page’ (wordpress native function) ? https://scrnli.com/DN9ksKz3kurjP6

    Do I need more more advanced PHP (custom PHP class) than usual CMB2 parameter like custom ‘show_on_cb’ ?

    Let’s presume ‘show_on_cb’ will do the trick…

    First, as I mentioned, I created the options page by native WordPress function ‘add_options_page’ :

        function pd_theme_settings_function() {
            add_options_page( 
                'Tytu?', 
                '_Ustawienia_Motywu_', 
                'manage_options', 
                'ustawienia_motywu',
                'theme_settings'
            );
        }
    add_action('admin_menu', 'pd_theme_settings_function');

    …and I want CMB2 fields inside of it…

    ….so here is my attempt to use ‘cmb2_show_on’ filter :

        function show_only_if_screen_id_equals_settings_page_ustawienia_motywu($display) {
            $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : (object) array( 'id' => null );
    
            // Only show on our screen.
            if ( 'settings_page_ustawienia_motywu' !== $screen->id ) {
                return;
            }
    
            return $display;
        }
        add_filter( 'cmb2_show_on', 'show_only_if_screen_id_equals_settings_page_ustawienia_motywu', 10, 2 );

    ….and then I used it inside new_cmb2_box like so:

        $cmb = new_cmb2_box( array(
            'id'            => 'backlink_offer',  
            'title'         => 'fdsa',
            'object_types' => array( 'options-page', ), // Post type
    		'show_on_cb' => 'show_only_if_screen_id_equals_settings_page_ustawienia_motywu',
            'capability'      => 'manage_options',
            'context'       => 'normal',
            'priority'      => 'low',
            'show_names'    => true, // Show field names on the left
        ) );

    ….nope….it does not work

    So I tried to utilize ‘Prefix_Add_CMB2_To_Settings_Page’ class form snipets library, namely:
    I tried to utilize ‘Prefix_Add_CMB2_To_Settings_Page’ class form snipets library

    …changing the $screen parameter to ‘settings_page_ustawienia_motywu’

    ….and then executing the function

    myprefix_cmb2_on_settings();

    But still no success…

    Any ideas how to make it work ?

    • This topic was modified 3 years, 1 month ago by piotrdeja.
    • This topic was modified 3 years, 1 month ago by piotrdeja.
    • This topic was modified 3 years, 1 month ago by piotrdeja.
    • This topic was modified 3 years, 1 month ago by piotrdeja.
    • This topic was modified 3 years, 1 month ago by piotrdeja.
    • This topic was modified 3 years, 1 month ago by piotrdeja.
    • This topic was modified 3 years, 1 month ago by piotrdeja.
Viewing 12 replies - 16 through 27 (of 27 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Thank you for the patience here @piotrdeja

    It looks like as a whole, it is possible to get the draggable metabox style on options pages, as per https://stackoverflow.com/a/8628307 but it’s going to be a bit messy, so to speak, with a bunch of hacking.

    Was hoping for a more elegant answer and solution, but some things even CMB2 can’t solve.

    Thread Starter piotrdeja

    (@piotrdeja)

    Thank you very much, I will look into this code (https://stackoverflow.com/a/8628307) probably at the start of the next week, and post the results in this thread…

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Rooting for success here.

    Thread Starter piotrdeja

    (@piotrdeja)

    To recap…

    As I mentioned, I want to make (please read carefully) separate metaboxes with CMB2 fields inside settings page like in the attached image here:
    https://github.com/piotrdejapl/pd_theme_settings_v2/blob/master/metaboxes.jpg

    As I understand, your initial intention was to make use complete solution for the above mentioned issue using CMB2 ^ONLY^…which turned out
    not to be possible.

    So now, my intention is to connect(fuse together) :
    1) the metaboxes code you privded here https://gist.github.com/bueltge/757903
    …and
    2) CMB2 fields

    This is my attempt in doing so:
    https://github.com/piotrdejapl/pd_theme_settings/blob/master/libs/admin-view.php

    …This plugin DOES SAVE cmb2 to wp_options under ‘my_metabox’, but still I’m not getting the saved data from the database…
    …any idea how to get the saved data inside CMB2 fields ?

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    I assume you’re trying with myprefix_get_option or at least get_option() correct? I believe the myprefix_get_option function passes things through cmb2_get_option and tries to help with plucking out individual parts in deeper structures that can be created at times. However, not a required thing

    Thread Starter piotrdeja

    (@piotrdeja)

    Nope, have not tried neither of those methods you suggested, I thought that cmb2_get_metabox() method would not only show the cmb2 field but insert the data from the database as well

    • This reply was modified 3 years, 1 month ago by piotrdeja.
    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Just making sure I’m squared away for when I’ll be able to dive in a bit deeper.

    Your current final result is https://github.com/piotrdejapl/pd_theme_settings/blob/master/libs/admin-view.php

    And while it’s saving the values to the database like expected, it’s not displaying them back to you as filled in fields?

    Thread Starter piotrdeja

    (@piotrdeja)

    Yes! It shows empty field even though the value has been saved to the database.

    • This reply was modified 3 years, 1 month ago by piotrdeja.
    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Noted, thank you. Will try to circle back as soon as I can again with this.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    I believe your usage of tralala is what’s throwing you off. I managed to get successful fetches of saved options by changing

    $cmb = cmb2_get_metabox( 'my_metabox', 'tralala' );
    

    to

    $cmb = cmb2_get_metabox( 'my_metabox' );
    

    in the on_save_changes() function as well as these changes for on_contentbox_1_content()

    cmb2_get_metabox( 'my_metabox', 'tralala', 'options-page' )->show_form();
    

    to

    cmb2_get_metabox( 'my_metabox', 'my_metabox', 'options-page' )->show_form();
    

    Hope that helps get you going some more here.

    Thread Starter piotrdeja

    (@piotrdeja)

    Wow, it does indeed work as expected – the data appears in fields. The only detail is that the window ‘settings saved’ does not appear at the top after changes have been saved.

    • This reply was modified 3 years, 1 month ago by piotrdeja.
    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    I wonder if a do_action( 'admin_notices' ) needs to be added to your output.

Viewing 12 replies - 16 through 27 (of 27 total)
  • The topic ‘CMB2 – place inside options page created by ‘add_options_page’’ is closed to new replies.