0

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 :/

  • 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 Feb 26 at 10:40
  • @FabrizioMele I'll do – A new 1 Feb 26 at 10:45
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.