如何修改插件的管理设置,以便将插件添加到特定页面?

时间:2016-08-09 作者:SidC

我为一个非常特定的用例编写了一个注册插件。表单包含数百行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,然后查看相关页面,表单不会插入。

为了确保注册表正确插入我指定的页面,必须修改/增加哪些内容?

1 个回复
最合适的回答,由SO网友:Mateusz Hajdziony 整理而成

您正在注册user_profile_template 函数在page_template 过滤器挂钩,但实际上调用了您的函数registration_template.

这就是为什么所选页面可能没有加载自定义模板文件的原因。这是一回事。另一个原因是,使用此方法,所选页面的外观将与活动主题中的所有其他页面完全不同。虽然可以在插件的自定义模板中模拟一个主题的HTML结构,但这在其他主题中不起作用。这是假设您的自定义页面模板应该看起来像是主题的一部分(至少有页眉和页脚)。解决方法是在the_content 在原始内容之前、之后或替代原始内容,钩住并打印自定义HTML。这样,您只关心自己的标记,并且让活动主题控制整个HTML结构。

相关推荐