0

我在 Wordpress 中创建了一个“ theme-options.php ”页面,可以在 WP API“外观”设置菜单下找到该页面。在页面上,我想包含来自Settings > General-Options页面(站点标题标语/副标题)的两件事,可以在此处(在我的主题选项页面上)进行编辑并保存..

我该如何实现这一目标?到目前为止,我有这个,它显示了所需的框和信息,但没有正确保存/更新。我错过了什么?

主题选项.php

// Build our Page
function build_options_page() {

// Page Structure
   ob_start(); ?>
     <div class="wrap">
       <?php screen_icon();?>
       <h2>Theme Options</h2>

       <p><b><label for="blogname"><?php _e('Site Title') ?></label></b></p>
       <p><input name="blogname" type="text" id="blogname" value="<?php form_option('blogname'); ?>" class="regular-text" />  
       <span class="description"><?php _e('The name of your site.') ?></span></p> 
       <br />

       <p><b><label for="blogdescription"><?php _e('Tagline or Subheading') ?></label></b></p>
       <p><input name="blogdescription" type="text" id="blogdescription"  value="<?php form_option('blogdescription'); ?>" class="regular-text" /> 
       <span class="description"><?php _e('A brief description of your site.') ?></span></p> 
       <p class="submit">
       <input name="Submit" type="submit" class="button-primary" value="<?php esc_attr_e('Save Changes'); ?>" />
       </p>

     </div>
   <?php
   echo ob_get_clean();
   ?>
}
4

1 回答 1

1

此示例使用 Profile 页面来显示和修改blognameand blogdescription,但应该非常直接地移植到您的代码中。

add_action( 'show_user_profile', 'show_extra_profile_fields', 1 );
add_action( 'edit_user_profile', 'show_extra_profile_fields', 1 );

function show_extra_profile_fields( $user ) { 
    ?>
    <table class="form-table">  
        <tr>
            <th><label for="user_address">Site Title</label></th>
            <td>
                <input type="text" name="blogname" id="blogname" value="<?php echo esc_attr( get_option('blogname') ); ?>" class="regular-text" /><br />
                <span class="description"></span>
            </td>
        </tr>
        <tr>
            <th><label for="user_zip">Tagline</label></th>
            <td>
                <input type="text" name="blogdescription" id="blogdescription" value="<?php echo esc_attr( get_option( 'blogdescription' ) ); ?>" class="regular-text" /><br />
                <span class="description"></span>
            </td>
        </tr>       
    </table>
    <?php 
}

add_action( 'personal_options_update', 'save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_profile_fields' );

function save_extra_profile_fields( $user_id ) {

    if ( !current_user_can( 'edit_user', $user_id ) )
        return false;

    update_option( 'blogname', $_POST['blogname'] );
    update_option( 'blogdescription', $_POST['blogdescription'] );
}
于 2012-06-05T22:32:41.730 回答