0

I know this is a common error but I haven't come across any solutions that have worked. I'm creating a plugin that uses custom post types and I have a settings page under that CPT's menu. The settings page displays fine but when I go to save the settings I get the error ERROR: options page not found. Here is my settings class:

<?php namespace SAW\Hours;

class Settings {
    private $view;

    public function __construct( $view ) {
        $this->view = $view;
        add_action( 'admin_menu', array( $this, 'submenu' ) );
        add_action( 'admin_init', function () {
            add_settings_section(
                'saw_hours',
                'Hours Settings', // Title
                array( $this, 'settings' ),
                'saw_hours_settings'
            );
        } );
    }

    public function submenu()
    {
        add_submenu_page(
            "edit.php?post_type=saw_hours", // Parent slug
            "Settings", // Page title
            "Settings", // Menu title
            "activate_plugins", // Role required
            'saw_hours_settings', // Menu slug
            array($this->view, 'admin'));
    }

    public function settings()
    {
        // API key setting
        register_setting(
            'saw_hours',
            'saw_hours_api_key'
        );
        add_settings_field(
            'saw_hours_api_key',
            'Set API Key:',
            function(){
                $clientId = get_option('saw_hours_api_key');
                echo '<input type="text" name="saw_hours_api_key" value="' . $clientId . '" >';
            },
            'saw_hours_settings',
            'saw_hours'
        );

        // Client ID setting
        register_setting(
            'saw_hours',
            'saw_hours_client_id'
        );
        add_settings_field(
            'saw_hours_client_id',
            'Set Client ID:',
            function(){
                $clientId = get_option('saw_hours_client_id');
                echo '<input type="text" name="saw_hours_client_id" value="' . $clientId . '" >';
            },
            'saw_hours_settings',
            'saw_hours'
        );
    }


}

And here is the form page:

<form method="POST" action="options.php">
    <?php settings_fields( 'saw_hours' ); ?>
    <?php do_settings_sections( 'saw_hours_settings'); ?>
    <?php submit_button(); ?>
</form>

I'm at a loss, any help would be appreciated.

0

This worked:

"Error: Options Page Not Found" on Settings Page Submission for an OOP Plugin

I'd like to understand why register_setting isn't properly adding the property to the whitelisted list to begin with though.

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.