Wp-graphql: Custom Settings Groups and Fields not showing up in Schema

Created on 26 Apr 2019  路  4Comments  路  Source: wp-graphql/wp-graphql

After registering and saving some custom Settings WP registered with the default API, I am being unable to view any changes in the Schema after adding following code. I wonder if i'm missing something, or if the feature is not working yet.

I tried using both develop and master

Any ideas what could be wrong?


function register_settings() {

    add_settings_section("pagesettings", "Pages", null, "pulsar-backend");

    $args = array(
        'type' => 'string',
        'show_in_graphql' => true,
    );


    add_settings_field("coverpage", "Cover Page", "display_coverpage", "pulsar-backend", "pagesettings");

    $args = array(
        'type' => 'string',
        'sanitize_callback' => NULL,
        'default' => NULL,
        'show_in_graphql' => true,
    );

    register_setting("pagesettings", "coverpage", $args);


}

add_action("admin_init", "register_settings");

question

Most helpful comment

The admin_init hook means you only want the setting to be available in the admin, and GraphQL requests aren鈥檛 tied to the admin. So your code is never executing in a context GraphQL is aware of.

Try hooking to init maybe instead of admin init? At least for the register_setting() portion

All 4 comments

The admin_init hook means you only want the setting to be available in the admin, and GraphQL requests aren鈥檛 tied to the admin. So your code is never executing in a context GraphQL is aware of.

Try hooking to init maybe instead of admin init? At least for the register_setting() portion

Thanks a lot for pointing out this error. Really got me out of a problem.

I attach finished code in case anybody winds up here.


<?php


function display_cover_page()
{
    wp_dropdown_pages(array(
        'selected'              => get_option('cover_page'),
        'name'                  => 'cover_page',
        'id'                    => 'cover_page',
    ));
}


function register_settings() {


    $args = array(
        'type' => 'string',
        'sanitize_callback' => NULL,
        // 'sanitize_callback' => 'sanitize_text_field',
        'default' => NULL,
        'show_in_graphql' => true,
    );

    register_setting("pages", "cover_page", $args);

}

function register_settings_groups() {

    add_settings_section("pages", "P谩ginas", null, "project-backend");

    $args = array(
        'type' => 'string',
        'show_in_graphql' => true,
    );


    add_settings_field("cover_page", "P谩gina de Portada", "display_cover_page", "project-backend", "pages");

    $args = array(
        'type' => 'string',
        'sanitize_callback' => NULL,
        // 'sanitize_callback' => 'sanitize_text_field',
        'default' => NULL,
        'show_in_graphql' => true,
    );



}



function theme_settings_page()
{
    ?>
        <div class="wrap">

        <h1>Settings</h1>

        <form method="post" action="options.php">

            <?php
                settings_fields("pages");
                do_settings_sections("project-backend");      
                submit_button(); 
            ?>          
        </form>
        </div>
    <?php
}


function add_theme_menu_item()
{
    add_menu_page("project", "project", "manage_options", "theme-panel", "theme_settings_page", null, 99);
}

add_action("admin_menu", "add_theme_menu_item");
add_action("init", "register_settings");
add_action("admin_init", "register_settings_groups");

?>

@furenku glad it worked, and thanks for sharing your final code!!

Thanks @jasonbahl and @furenku, using the init instead of admin_init did the trick!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jasonbahl picture jasonbahl  路  4Comments

moonmeister picture moonmeister  路  4Comments

saulirajala picture saulirajala  路  3Comments

nikosolihin picture nikosolihin  路  5Comments

BE-Webdesign picture BE-Webdesign  路  3Comments