将Wordpress“网站标题”功能放在“主题选项”页面中

时间:2012-06-04 17:58:56

标签: php html css wordpress themes

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

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

theme-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();
   ?>
}

1 个答案:

答案 0 :(得分:1)

此示例使用“配置文件”页面显示和修改blognameblogdescription,但应该非常直接地移植到您的代码中。

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'] );
}