Wordpress管理员设置页面 - 提取数组

时间:2013-11-20 09:30:26

标签: php wordpress

我正在寻找使用管理设置页面构建插件。看一下codex,我有以下代码:

<?php

class MyNewPlugin
{
/**
 * Holds the values to be used in the fields callbacks
 */
private $options;

/**
 * Start up
 */
public function __construct()
{
    add_action( 'admin_menu', array( $this, 'add_plugin_page' ) );
    add_action( 'admin_init', array( $this, 'page_init' ) );
}

/**
 * Add options page
 */
public function add_plugin_page()
{
    // This page will be under "Settings"
    add_options_page(
        'Settings Admin', 
        'My New Plugin', 
        'manage_options', 
        'my-new-plugin-admin', 
        array( $this, 'create_admin_page' )
    );
}

/**
 * Options page callback
 */
public function create_admin_page()
{
    // Set class property
    $this->options = get_option( 'my_new_plugin_option' );
    ?>
    <div class="wrap">
        <?php screen_icon(); ?>
        <h2>My New Plugin Settings</h2>           
        <form method="post" action="options.php">
        <?php
            // This prints out all hidden setting fields
            settings_fields( 'my_new_plugin_option_group' );   
            do_settings_sections( 'my-new-plugin-admin' );
            submit_button(); 
        ?>
        </form>
    </div>
    <?php
}

/**
 * Register and add settings
 */
public function page_init()
{        
    register_setting(
        'my_new_plugin_option_group', // Option group
        'my_new_plugin_option', // Option name
        array( $this, 'sanitize' ) // Sanitize
    );

    add_settings_section(
        'setting_section_id', // ID
        'Settings', // Title
        array( $this, 'print_section_info' ), // Callback
        'my-new-plugin-admin' // Page
    );         

    add_settings_field(
        'title', 
        'Title', 
        array( $this, 'title_callback' ), 
        'my-new-plugin-admin', 
        'setting_section_id'
    );  

    add_settings_field(
        'addresslineone', 
        'Address Line One', 
        array( $this, 'addresslineone_callback' ), 
        'my-new-plugin-admin', 
        'setting_section_id'
    ); 

    add_settings_field(
        'addresslinetwo', 
        'Address Line Two', 
        array( $this, 'addresslinetwo_callback' ), 
        'my-new-plugin-admin', 
        'setting_section_id'
    ); 
}

/**
 * Sanitize each setting field as needed
 *
 * @param array $input Contains all settings fields as array keys
 */
public function sanitize( $input )
{
    if( isset( $input['title'] ) )
        $new_input['title'] = sanitize_text_field( $input['title'] );

    if( isset( $input['addresslineone'] ) )
        $new_input['addresslineone'] = sanitize_text_field( $input['addresslineone'] ); 

    if( isset( $input['addresslinetwo'] ) )
        $new_input['addresslinetwo'] = sanitize_text_field( $input['addresslinetwo'] );     

    return $new_input;
}

/** 
 * Print the Section text
 */
public function print_section_info()
{
    print 'Enter your settings below:';
}

/** 
 * Get the settings option array and print one of its values
 */
public function title_callback()
{
    printf(
        '<input type="text" id="title" name="my_new_plugin_option[title]" value="%s" />',
        isset( $this->options['title'] ) ? esc_attr( $this->options['title']) : ''
    );
}

/** 
 * Get the settings option array and print one of its values
 */
public function addresslineone_callback()
{
    printf(
        '<input type="text" id="addresslineone" name="my_new_plugin_option[addresslineone]" value="%s" />',
        isset( $this->options['addresslineone'] ) ? esc_attr( $this->options['addresslineone']) : ''
    );
}

public function addresslinetwo_callback()
{
    printf(
        '<input type="text" id="addresslinetwo" name="my_new_plugin_option[addresslinetwo]" value="%s" />',
        isset( $this->options['addresslinetwo'] ) ? esc_attr( $this->options['addresslinetwo']) : ''
    );
}
}

if( is_admin() )
    $my_new_plugin_settings = new MyNewPlugin();

include_once "mynewpluginwidget.php";   

一个简单的插件开始,用户在设置页面上输入设置,它显示在小部件区域(用户必须将小部件添加到他们想要的位置)。

问题是,如何提取设置并显示它们?我认为它们在数组$ this中,但无法显示它们。

0 个答案:

没有答案