options - Plugins Settings page not updating

admin2025-06-02  2

After following this tutorial
in mostly everything, except the naming,
I am currently facing an error: Error: Settings page wasn't found (translated)

This is my code:

class ConfigPlugin
{

public function __construct()
{
    add_action('admin_menu', array($this, 'create_plugin_settings_page'));
    add_action('admin_menu', array($this, 'setup_section'));
    add_action( 'admin_init', array( $this, 'setup_fields' ) );
}

public function create_plugin_settings_page()
{
    $page_title = 'Configg harray kotter';
    $menu_title = 'Config my plug-in';
    $capability = 'manage_options';
    $slug = 'Plugin';
    $callback = array( $this, 'plugin_settings_page_content' );
    $icon = 'dashicons-admin-plugins';
    $position = 100;

    add_menu_page($page_title, $menu_title, $capability, $slug, $callback, $icon, $position);
}

public function plugin_settings_page_content()
{
    ?>
<div class="wrap">
    <h2>My Awesome Settings Page</h2>
    <form method="post" action="options.php">
        <?php
            settings_fields( 'Plugin' );
            do_settings_sections( 'Plugin' );
            submit_button();
        ?>
    </form>
</div> <?php
}

public function setup_section()
{
    add_settings_section('section', '1', [$this, 'callback'], 'Plugin');

}

public function  callback($params) {
    switch ($params['id']) {
        case 'section' :
            echo 'Im so depressed<br>';

            break;
    }
}

public function setup_fields() {
    add_settings_field( 'field', 'Text Name', array( $this, 'field_callback' ), 'Plugin', 'section' );
}

public function field_callback($arguments)
{
    echo '<input name="field" id="field" type="text" value="hi' . get_option( 'field' ) . '" />';
    register_setting('Plugin', 'field');
}

}

I have no idea whats happening and I am thankful for every little hint :)

UPDATE:
I now know what went wrong. I posted the solution here.
Thank you for 6 views and no clues :/

After following this tutorial
in mostly everything, except the naming,
I am currently facing an error: Error: Settings page wasn't found (translated)

This is my code:

class ConfigPlugin
{

public function __construct()
{
    add_action('admin_menu', array($this, 'create_plugin_settings_page'));
    add_action('admin_menu', array($this, 'setup_section'));
    add_action( 'admin_init', array( $this, 'setup_fields' ) );
}

public function create_plugin_settings_page()
{
    $page_title = 'Configg harray kotter';
    $menu_title = 'Config my plug-in';
    $capability = 'manage_options';
    $slug = 'Plugin';
    $callback = array( $this, 'plugin_settings_page_content' );
    $icon = 'dashicons-admin-plugins';
    $position = 100;

    add_menu_page($page_title, $menu_title, $capability, $slug, $callback, $icon, $position);
}

public function plugin_settings_page_content()
{
    ?>
<div class="wrap">
    <h2>My Awesome Settings Page</h2>
    <form method="post" action="options.php">
        <?php
            settings_fields( 'Plugin' );
            do_settings_sections( 'Plugin' );
            submit_button();
        ?>
    </form>
</div> <?php
}

public function setup_section()
{
    add_settings_section('section', '1', [$this, 'callback'], 'Plugin');

}

public function  callback($params) {
    switch ($params['id']) {
        case 'section' :
            echo 'Im so depressed<br>';

            break;
    }
}

public function setup_fields() {
    add_settings_field( 'field', 'Text Name', array( $this, 'field_callback' ), 'Plugin', 'section' );
}

public function field_callback($arguments)
{
    echo '<input name="field" id="field" type="text" value="hi' . get_option( 'field' ) . '" />';
    register_setting('Plugin', 'field');
}

}

I have no idea whats happening and I am thankful for every little hint :)

UPDATE:
I now know what went wrong. I posted the solution here.
Thank you for 6 views and no clues :/

Share Improve this question edited Feb 26, 2019 at 10:39 A new 1 asked Feb 25, 2019 at 15:41 A new 1A new 1 93 bronze badges 2
  • 1 I suggest you to write a self-answer to this question, post it as mark as correct, so others will find a solution in case they need. – Fabrizio Mele Commented Feb 26, 2019 at 10:40
  • @FabrizioMele I'll do – A new 1 Commented Feb 26, 2019 at 10:45
Add a comment  | 

1 Answer 1

Reset to default -1

I don't know why this happened, but it seems that register_setting shouldn't be in the callback of add_settings_field

// no actual code
// this worked
add_settings_field('id','title', /*callback*/ function($arguments) {echo $htmlcode;}), 'page', 'section');
register_setting('option_group', 'option_name');

I hope this helps

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748864021a314354.html

最新回复(0)