Friday, January 27, 2023
HomeWordPress DevelopmentWordPress plugin choices web page not saving choices, no errors

WordPress plugin choices web page not saving choices, no errors


My plugin’s choices web page has two string choices: username and access_key. After I enter my username and click on “Save Adjustments”, the web page reloads, however the username will not be current within the possibility subject.

If I add var_dump($_POST); to my possibility’s web page output, I see array(0) { } on the subsequent web page load after attempting to avoid wasting choices.

Right here is my plugin’s choices.php file:

if (is_admin()) {
    add_action( 'admin_menu', 'example_portfolio_options_page' );
}

operate example_portfolio_options_page() {

    //create new top-level menu
    add_menu_page('Instance Portfolio Settings', 'Instance', 'administrator', __FILE__, 'example_portfolio_options_page_form', 'dashicons-admin-generic');

    //name register settings operate
    add_action( 'admin_init', 'example_portfolio_settings' );

    if ( ! isset( $_POST['example_portfolio_options_update_nonce'] ) ) {
        return;
    }
    
    if ( ! wp_verify_nonce( $_POST['example_portfolio_options_update_nonce'], 'example_portfolio_options_update' ) ) {
        return;
    }
    
    // Replace the 'username' possibility with the brand new worth entered by the person
    if ( isset( $_POST['username'] ) ) {
        update_option( 'example_portfolio_username', sanitize_text_field($_POST['username']) );
    }
    
    // Replace the 'username' possibility with the brand new worth entered by the person
    if ( isset( $_POST['access_key'] ) ) {
        update_option( 'example_portfolio_access_key', sanitize_text_field($_POST['access_key']) );
    }
}

operate example_portfolio_settings() {
    add_options_page(
        'Instance Writer Portfolio',
        'Instance Writer Portfolio',
        'manage_options',
        'example_portfolio',
        'example_portfolio_options_page'
    );

    register_setting( 'example_portfolio_options', 'example_portfolio_options', 'example_portfolio_options_validate' );

    add_settings_section(
        'example_portfolio_settings_section',
        __( 'Instance Writer Portfolio Settings', 'example_portfolio' ),
        'example_portfolio_settings_section_callback',
        'example_portfolio_options'
    );
    
    add_settings_field(
        'example_portfolio_username',
        __( 'Instance.com Username', 'example_portfolio' ),
        'example_portfolio_username_callback',
        'example_portfolio_options',
        'example_portfolio_settings_section'
    );
    
    add_settings_field(
        'example_portfolio_access_key',
        __( 'Instance API Entry Key', 'my_plugin' ),
        'example_portfolio_access_key_callback',
        'example_portfolio_options',
        'example_portfolio_settings_section'
    );

    operate example_portfolio_settings_section_callback() {
        echo '<p>Enter your Instance username and entry key under:</p>';
    }

    operate example_portfolio_username_callback() {
        $choices = get_option( 'example_portfolio_options' ) ?: array();
        echo '<enter sort="textual content" id="example_portfolio_options_username" title="example_portfolio_username" worth="' . $choices['username'] . '" />';
    }
    
    operate example_portfolio_access_key_callback() {
        $choices = get_option( 'example_portfolio_options' ) ?: array();
        echo '<enter sort="textual content" id="example_portfolio_options_access_key" title="example_portfolio_access_key" worth="' . $choices['access_key'] . '" />';
    }

    add_option( 'example_portfolio_username', '' );
    add_option( 'example_portfolio_access_key', '' );

}

operate example_portfolio_options_page_form() {
    if ( ! current_user_can( 'manage_options' ) ) {
        return;
    }
    var_dump($_POST);
    ?>
    <div class="wrap">
        <kind motion="choices.php" technique="put up">
            <?php
                settings_fields( 'example_portfolio_options' );
                do_settings_sections( 'example_portfolio_options' );
                submit_button();
                wp_nonce_field( 'example_portfolio_options_update', 'example_portfolio_options_update_nonce' );
            ?>
        </kind>
    </div>
    <?php
}

Is my <kind motion="choices.php" technique="put up"> coded accurately?

Assist appreciated.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments