Save and display wordpress Plugin data

i create WordPress Plugin and want to save to wordpress_options table.
But in my save function wont work.

This is my code :

Read More
<?php 
/**
* Plugin Name: Social Media Plugin
* Plugin URI: http://fanjavaid.com/plugin
* Description: Plugin untuk kebutuhan data sosial media.
* Version: 1.0
* Author: Fandi Akhmad
* Author URI: http://fanjavaid.com
*/

function mp_admin() {
    if(isset($_POST['mp_web'])):
        update_option('mp_web', $_POST['mp_web']);
        echo '<div class="updated"><p><strong>Updated</strong>: Data berhasil diubah</p></div>';
    endif;
    $mp_web = get_option('mp_web');
?>

<div class="wrap">
    <?php    echo "<h2>" . __( 'Halaman Konfigurasi My Plugin', 'mp' ) . "</h2>"; ?>
    <?php echo "<p>Masukkan detail sosial media untuk website Indotechsci</p>"; ?>

    <form name="mp_form" method="post" action="<?php echo str_replace( '%7E', '~', $_SERVER['REQUEST_URI']); ?>">
        <input type="hidden" name="mp_hidden" value="Y">
        <p><?php _e("Twitter " ); ?><input type="text" name="mp_twt" value="" size="20" placeholder="username"></p>
        <p><?php _e("Facebook " ); ?><input type="text" name="mp_fb" value="" size="20" placeholder="username"></p>
        <p><?php _e("Youtube " ); ?><input type="text" name="mp_ytb" value="" size="20" placeholder="username"></p>
        <p class="submit">
        <input type="submit" name="Submit" class="button button-primary" value="<?php _e('Save Changes', 'mp' ) ?>" />
        </p>
    </form>
</div>

<?php } ?>

It wont save.
May be any wrong to my code? And how to call the saved above info into homepage in WordpresS?

Please help me. Thank you.

Related posts

Leave a Reply

1 comment

  1. The problem with your code is that you might be saving the data, but you are not reading the data that is saved, and you are not changing the data in your form according to what you have previously saved.

    The code below is a typical way of saving and reading registered variables from database.

    <form method="post" action="options.php">
            <?php settings_fields( 'FJE_Plugin_FrontPage_Settings_Group' ); ?>
            <?php do_settings_sections( 'FJE_Plugin_FrontPage_Settings_Group' ); ?>
            <table class="form-table">
                <tr valign="top">
                    <th scope="row">Website name:</th>
                    <td><input type="text" name="FJEPLG_option_Website_name" value="<?php echo esc_attr( get_option('FJEPLG_option_Website_name') ); ?>" /></td>
                </tr>
                <tr valign="top">
                    <th scope="row">Show/Hide Website name:</th>
                    <td>
                        <input name="FJEPLG_option_ShowWebsiteName" type="checkbox" value="Yes"  <?php if (esc_attr( get_option('FJEPLG_option_ShowWebsiteName'))==true ){echo "checked='checked'";}; ?> />
                        <label>Show the website name in header;</label>
                    </td>
                </tr>
            </table>
            <?php submit_button(); ?>
    

    The variable names that you use, must be registered with register_setting() function.