Save plugin settings per user

I thought of a sandbox starter plugin (sterilized code below) to save a url for a user. So in the admin section there is a button. When the user clicks it a new window opens taking them to the configured url. I know really simple.

I have the plugin form and post handling functions working. What I am now running into is confusion on using wp_options table.

Read More

Specifically how to save per USER settings into wp_options table.

I can think of three ways.

  1. Add a third hidden field on the configuration page that contains the users id. Then the entry is made into wp_options. On recall I would find the entry in wp_options matching the userid. What I am unsure of here is if wp_options canned function would over write….In other words if user A saved www.google.com then 10 minutes later user B saves www.bing.com does the canned wp_options canned function overwrite user A? Further can I specify particulars for recall?

  2. The second is to save the settings in the users table….as a sort of user setting…but to do that am I not stepping on convention and further risking upgrade / compatibility?

  3. Custom table which it seems to me is the way to go….but you guys know far more than I so I wanted to get your input.

Here is the code…again sterilized so some things might not make sense because I have changed themfor my playground purposes.

    function register_setting() 
    {
        register_setting( 'Options-group', 'OptionsA_options', 'options_callback');
        register_setting( 'Options-group', 'url' );

    }


    function options_callback()
    { 
        alert('Options Callback'); 
    }
    add_action( 'admin_init', 'register_setting' );



    function my_plugin_menu() 
    {
        add_options_page( 'Test Options', 'Button Click', 'activate_plugins', 'UNIque Name', 'some_options' );
    }
    add_action( 'admin_menu', 'my_plugin_menu' );


    function clicky_options() 
    {
        if ( !current_user_can( 'manage_options' ) )  {
            wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
        }
        ?>

            <div class="wrap">
                <h2>Config</h2>

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

                    <?php settings_fields( 'Options-group' );?>

                    <?php do_settings_sections( 'Options-group' );?>


                    <table class="form-table">
                        <tr valign="top">
                            <th scope="row">Site Url (including http:// OR https:// )</th>
                            <td><input type="text" name="url" value="<?php echo esc_attr( get_option('url') ); ?>" /></td>                     
                        </tr>
                    </table>


                    <?php submit_button('Sign On');?>

                </form>
            </div>

        <?php
    }

Form post handling:

$url = $_POST['url'];

$FullUrl = $url."/"

header("Location: $FullUrl", true, 301);
exit();

Related posts

Leave a Reply

1 comment

  1. So basically, you are trying to save the user inputs in ‘wp_options’ table by using ‘register_settings’. Instead of that you can simply save the form and add the input to usermeta to handle the settings per user –

    $user_id = get_current_user_id();

    update_user_meta( $user_id, ‘input_url’, $_POST[‘url’] );

    You can use ‘update_user_option’ if dealing with multisite.