1

I do have a problem regarding the plug-in settings that I've made.

I've created a simple textbox and a submit button and run it on the admin page setting. The name of the textbox and submit button is myfirst_plugin.php. Now all I just want is to fill the field and when you refresh it, it'll show the last attributes that you entered on the name, email, comments etc. Would that be possible? How can I use the get_option and update_option?

For example, on the name field I type the name “Jayson Parker”, if I am going to refresh it again, Jayson Parker would be on the name field even if I refresh it.

Hope someone can

<?php
/*
Plugin Name: Wordpress Event Plugin
Plugin URI:
Description: plugin for wordpress
Version: 1.0
Author:
Author URI:jayson.parker@gmail.com
License: GPLv2
*/

// Admin Page

    add_action('admin_menu', 'myfirstplugin_admin_actions');

    function myfirstplugin_admin_actions() {
        add_options_page(
            "Event Settings",
            "Event Settings",
            'manage_options',
            "wp_event_plugin",
            "myfirstplugin_admin_page"
        );


    }

     function myfirstplugin_admin_page() {
        include("myfirst_plugin.php");
    }



?>
1

Handling form fields is not related to WordPress and is off-topic here, BTW I'm answering this on how to use the options table. And your code you presented here is not relevant to your question.

Ok, first of all having form data into a field after submission could put you in risk of entering duplicate entries. BTW here's how you can do that (follow the inline comments):

<?php
if( isset($_POST['x_submit']) ) {
    $data = array(
            'name'  => sanitize_text_field( $_POST['name'] ),
            'email' => sanitize_email( $_POST['email'] ),
            'comment'   => esc_textarea( $_POST['comment'] )
        );
    //entering data into options table
    update_option( 'my_option_key', $data );
}
?>

<?php
//having data from options table
$db_values = get_option( 'my_option_key' );

//setting empty values to avoid 'undefined index' warning
$name = '';
$email = '';
$comment = '';

//if there's any data in options table, updating our variables with relevant data
if( $db_values ) {
    $name = $db_values['name'] ? $db_values['name'] : '';
    $email = $db_values['email'] ? $db_values['email'] : '';
    $comment = $db_values['comment'] ? $db_values['comment'] : '';
}
?>
<form method="post">
    <label>Name <input type="text" name="name" value="<?php echo isset($_POST['name']) ? $_POST['name'] : $name; ?>"></label><br>
    <label>Email <input type="email" name="email" value="<?php echo isset($_POST['email']) ? $_POST['email'] : $email; ?>"></label><br>
    <label>Comment <textarea name="comment" cols="30" rows="10"><?php echo isset($_POST['comment']) ? $_POST['comment'] : $comment; ?></textarea></label>
    <input type="submit" name="x_submit" value="Submit">
</form>

P.S.: NOTHING IS TESTED here, just follow the procedures and change it as it suits you. And important, do the necessary escaping and sanitizing.

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.