我为一个非常特定的用例编写了一个注册插件。表单包含数百行HTML。表单的目的是能够通过管理设置将其添加到给定页面。为了实现这一目标,我编写了以下内容:
<?php
/**
* Created by PhpStorm.
* User: me
* Date: 7/10/16
* Time: 9:42 PM
*/
?>
<div class="wrap">
<h2><?php echo esc_html(get_admin_page_title()); ?></h2>
<?php
$options = get_option(\'QFPMembership\');
settings_fields(\'QFPMembership\');
?>
<form method="post" name="qfpregister-settings" action="options.php">
<fieldset>
<legend class="screen-reader-text"><span><?php _e(\'Registration Module\', \'QFPMembership\'); ?></span></legend>
<label for="qpfmembership-registration">
<?php echo home_url(\'/\');?><input type="text" id="qfpmembership-registration" name="QFPMembership[registration]" value="<?php echo $options[\'registration\'];?>" />
<span><?php esc_attr_e(\'Registration Module\', \'QFPMembership\'); ?></span><br>
<span class="description"><?php esc_attr_e(\'The registration form will be added to the end of this page.\', \'QFPMembership\'); ?></span>
</label>
</fieldset>
<?php submit_button(\'Save\', \'primary\', \'submit\', TRUE); ?>
</form>
</div>
<?php
function qfpmembership_admin_display(){
add_options_page( \'QFP Registration Settings\', \'QFP Registration
Module Settings\', \'manage_options\', \'qfpregister_settings\',
\'qfp_membership_admin_display\');
}
function display_qfpregister_settings(){
add_action(\'admin_menu\', \'qfpregister_settings_page\');
}
function registration_template($page_template){
$options = get_option(\'QFPRegistration\');
if(is_page( $options[\'membership-pageacghjlmrsvy-\'])){
$page_template = dirname(__FILE__ ) . \'/public/partials
/registrationform.php\';
}
return $page_template;
}
add_filter( \'page_template\', \'user_profile_template\', 11);
我编写了以下类来访问设置API:
class QFPRegistration_Admin
{
/**
* The ID of this plugin.
*
* @since 1.0.0
* @access private
* @var string $plugin_name The ID of this plugin.
*/
private $plugin_name;
/**
* The version of the plugin.
*
* @since 1.0.0
* @access private
* @var string $version The current version of the plugin.
*/
private $version;
/**
* Initialize the class and it\'s properties.
*
* @since 1.0.0
* @param string $plugin_name The name of the plugin.
* @param string $version The version of the plugin
*/
public function __construct($plugin_name, $version)
{
$this->plugin_name = $plugin_name;
$this->version = $version;
}
/**
* Register the stylesheet for the admin area.
*
* @since 1.0.0
*/
public function enqueue_styles(){
wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__) . \'css/QFPMembershipAdmin.css\', array(), $this->version, \'all\');
}
/**
* Register the Javascript for the admin area.
*
* @since 1.0.0
*/
public function enqueue_scripts(){
wp_enqueue_script( $this->plugin_name, plugin_dir_url(__FILE__) . \'js/QFPMembershipAdmin.js\', array( \'jquery\'), $this->version, false);
}
/**
* Register the administration menu into the Wordpress Dashboard menu.
* @since 1.0.0
*/
public function add_plugin_admin_menu(){
/*
* Add settings page for this plugin to the settings menu.
*
*/
add_options_page(\'QFP Registration Module Setup\', \'QFPRegistration\', \'manage_options\', $this->plugin_name, array($this, \'display_plugin_setup_page\')
);
}
/**
* Add settings action links to the plugin page.
* @since 1.0.0
*/
public function add_action_links( $links ){
$settings_link = array(
\'<a href="\' . admin_url( \'options-general-php?page=\' . $this->plugin_name) . \'">\' . __(\'Settings\', $this->plugin_name) . \'</a>\',
);
return array_merge( $settings_link, $links );
}
/**
* Render the settings page for the QFPMembership plugin.
* @since 1.0.0
*/
public function display_plugin_setup_page(){
include_once(\'partials/qfpregistration-admin-display.php\');
}
/**
* Save the plugin options
* @since 1.0.0
*/
public function options_update(){
register_setting( $this->plugin_name, $this->plugin_name, array($this, \'validate\') );
}
}
当我将插件安装到本地Wordpress实例中时,我会转到QFPMembership设置并指定要插入表单的页面。我按Save,然后查看相关页面,表单不会插入。
为了确保注册表正确插入我指定的页面,必须修改/增加哪些内容?