drexlz0623 2015-03-11 16:27
浏览 37

保存并显示wordpress插件数据

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

This is my code :

<?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.

  • 写回答

1条回答 默认 最新

  • douying7289 2019-08-31 04:18
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。