PHP - 如果信息已存在于数组中,请显示该信息

时间:2013-11-21 12:37:54

标签: php wordpress

我有一个Wordpress插件设置页面,它使用以下插件设置页面:

<?php

class NewPlugin
{

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', 
        'New Plugin', 
        'manage_options', 
        'new-plugin-admin', 
        array( $this, 'create_admin_page' )
    );
}

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

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

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

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

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

    add_settings_field(
        'addresslinetwo', 
        'Address Line Two', 
        array( $this, 'addresslinetwo_callback' ), 
        '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="new_plugin_options[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="new_plugin_options[addresslineone]" value="%s" />',
        isset( $this->options['addresslineone'] ) ? esc_attr( $this->options['addresslineone']) : ''
    );
}

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

if( is_admin() )
$new_plugin_settings = new NewPlugin();

我已经有一个小部件,其中要求的详细信息位于数组“$ instance”中。我正在尝试编写一个if语句,询问是否存在$ instance,然后将字符串转换为新数组。到目前为止,我没有太多运气。有人能指出我正确的方向吗?

1 个答案:

答案 0 :(得分:0)

由于没有太多关于$ instance变量的信息,这些函数应该根据条件来考虑变量的存在: