在PHP中为插件创建选项页面:get_option()

时间:2012-10-16 23:44:15

标签: php wordpress-plugin wordpress

我正在尝试为wordpress创建一个微笑插件。我需要能够更新自定义选项的get_option()变量。如果您知道下面的脚本有什么问题,请告诉我。

它会将字段添加到数据库并显示设置页面,但是当我点击“保存更改”按钮时它不会更新它们。

请让我知道我做错了什么......

我无法更改插件的选项页面中的任何选项值...

> <?php /* Plugin Name: Mobile Marketing Mob plugin Plugin URI:
> http://www.example.co Description: A Simple plugin
> Version: 1.0 Author: TxtClub Author URI:
> http://www.example.com License: GPL
> */
> 
> 
> /* Runs when plugin is activated */
> register_activation_hook(__FILE__,'mmm_install'); 
> 
> /* Runs on plugin deactivation*/ register_deactivation_hook( __FILE__,
> 'mmm_remove' );
> 
> function mmm_install() { /* Creates new database field */
> add_option("mmm-token", 'e.g. 14859298165079c736f31a6', '', 'yes');
> add_option("mmm-spliton", '0', '', 'yes'); add_option("mmm-senderid",
> 'e.g. Chico\'s Garage', '', 'yes'); }
> 
> function mmm_remove() { /* Deletes the database field */
> delete_option('mmm-token'); delete_option('mmm-spliton');
> delete_option('mmm-senderid'); }
> 
> 
> 
> //create custom options page add_action( 'admin_menu',
> 'my_plugin_menu' );
> 
> function my_plugin_menu() { $page_title = 'Mobile Marketing Mob
> Settings'; $menu_title = 'MMM Settings'; $capability =
> 'manage_options'; $menu_slug = 'mobile-marketing-mob-settings';
> $function = 'my_plugin_options'; add_options_page( $page_title,
> $menu_title, $capability, $menu_slug, $function );
> 
> 
> add_action( 'admin_init', 'register_mysettings' ); }
> 
> function register_mysettings() {  //register our settings
>   register_setting( 'mmm-options-group', 'mmm-token' );
>   register_setting( 'mmm-options-group', 'mmm-spliton' );
>   register_setting( 'mmm-options-group', 'mmm-senderid' ); }
> 
> 
> 
> function my_plugin_options() {    if ( !current_user_can(
> 'manage_options' ) )  {       wp_die( __( 'You do not have sufficient
> permissions to access this page.' ) );    }   else { ?> <h2> Mobile
> Marketing Mob Settings </h2>
>       
>         
>         <form method="post" action="<?php $_SERVER['PHP_SELF']?>">
>     <?php settings_fields( 'mmm-options-group' ); ?>
>     <!-- Wordpress documentation is wrong and suggests do_settings (which is for older versions below 2.7) -->
>     <?php do_settings_sections( 'mmm-options-group' ); ?>
>     <table class="form-table">
>         <tr valign="top">
>         <th scope="row">Unique token</th>
>         <td><input type="text" name="mmm-token" value="<?php echo get_option('mmm-token'); ?>" /></td>
>         </tr>
>          
>         <tr valign="top">
>         <th scope="row">Split test on</th>
>         <td><input type="text" name="mmm-spliton" value="<?php echo get_option('mmm-spliton'); ?>" />  (0 = off  1 = on)</td>
>         </tr>
>         
>         <tr valign="top">
>         <th scope="row">Sender ID</th>
>         <td><input type="text" name="mmm-senderid" value="<?php echo get_option('mmm-senderid'); ?>" /></td>
>         </tr>
>     </table>
>     
>     <?php submit_button(); ?>
> 
> </form> </div>
>         
>         
>           <?php } } ?>

1 个答案:

答案 0 :(得分:1)

尝试使用update_option()更新选项。

add_option()添加了该选项,但它不会更新它,而update_option()也会创建它,如果它不存在的话。

希望这有帮助。