WordPress自定义选项页面未保存更改

时间:2019-02-25 11:52:41

标签: wordpress options

我有一个自定义插件版本,带有一个选项页(不在“设置/选项”区域中)。

在插件中,我有一个类文件和一个显示文件,它们都可以在自定义菜单中正确显示。

自定义版本的“属性表单”正确显示了手动选项,但是在编辑和保存选项时,我得到了:

  • form-action ='options.php'的错误消息
  • 使用表单action ='。/ admin.php?page = TH_WP_Mail_Settings'重新加载页面

我尝试了几种选择,但没有成功。

有人看到我失踪的原因吗?

class-property-settings.php

<?php

namespace TH_WP_Property_Mailer\Inc\Admin;
use TH_WP_Property_Mailer\Inc\Libraries;

class Property_Settings   {

    /**
     * The text domain of this plugin.
     */
    protected $plugin_text_domain;

    public function __construct( $plugin_text_domain ) {

        $this->plugin_text_domain = $plugin_text_domain;

        // Tracks new sections for whitelist_custom_options_page()
        $this->page_sections = array();
        // Must run after wp's `option_update_filter()`, so priority > 10
        add_action( 'whitelist_options', array( $this, 'whitelist_custom_options_page' ),11 );
    }   

    // White-lists options on custom pages.
    // Workaround for second issue
    public function whitelist_custom_options_page( $whitelist_options ){
        // Custom options are mapped by section id; Re-map by page slug.
        foreach($this->page_sections as $page => $sections ){
            $whitelist_options[$page] = array();
            foreach( $sections as $section )
                if( !empty( $whitelist_options[$section] ) )
                    foreach( $whitelist_options[$section] as $option )
                        $whitelist_options[$page][] = $option;
                }
        return $whitelist_options;
    }

    // Wrapper for wp's `add_settings_section()` that tracks custom sections
    private function add_settings_section( $id, $title, $cb, $page ){
        add_settings_section( $id, $title, $cb, $page );
        if( $id != $page ){
            if( !isset($this->page_sections[$page]))
                $this->page_sections[$page] = array();
            $this->page_sections[$page][$id] = $id;
        }
    }    

    /**
     * Prepares the list of items for displaying.
     * 
     */
     public function prepare_items() {

        //register_setting( string $option_group, string $option_name, array $args = array() )
/*        register_setting( 'TH_WP_Mail_Settings', 'TH_Mail_layout' );*/


        add_option( 'th_mail_layout_to_field', 'This is my option value.');
        add_option( 'th_mail_layout_body_field', 'Option 2');

        register_setting( 'TH_WP_Mail_Settings', 'th_mail_layout_to_field', 'th_wp_property_mailer_settings_callback');
        register_setting( 'TH_WP_Mail_Settings', 'th_mail_layout_body_field', 'th_wp_property_mailer_settings_callback');


        //add_settings_section( $id, $title, $callback, $page )
        $this->add_settings_section(
            'TH_WP_Mail_Settings_section',                             //$id
            __( 'Targethousing Mail layout', 'wordpress' ),            //$title
            array($this, 'th_wp_property_mailer_settings_callback'),   //$callback
            'TH_WP_Mail_Settings'                                      //$page
        );

        //add_settings_field( $id, $title, $callback, $page, $section, $args )
        add_settings_field(
            'th_mail_layout_to_field',                                  //$id
            __( 'Afzender', 'wordpress' ),                                 //$title
            array($this, 'th_mail_layout_to_field_render'),             //$callback
            'TH_WP_Mail_Settings',                                      //$page
            'TH_WP_Mail_Settings_section'                               //$section
        );

        add_settings_field(
            'th_mail_layout_body_field',                                //$id
            __( 'Mail Body', 'wordpress' ),                     //$title
            array($this, 'th_mail_layout_body_field_render'),           //$callback
            'TH_WP_Mail_Settings',                                      //$page
            'TH_WP_Mail_Settings_section'                               //$section
        );
    }

    public function th_mail_layout_to_field_render() {
        $options = get_option( 'th_mail_layout_to_field' );
        ?>
        <input type='text' name='th_mail_layout_to_field' value='<?php echo $options; ?>'>
        <?php
    }

    public function th_mail_layout_body_field_render() {
        $options = get_option( 'th_mail_layout_body_field' );
        ?>
        <input type='text' name='TH_Mail_layout[th_mail_layout_body_field]' value='<?php echo $options; ?>'>
        <?php
    }

    public function th_wp_property_mailer_settings_callback() {
        echo __( 'Standaard layout', 'wordpress' );
    }


    /**
     * Stop execution and exit
     * @return void
     */    
     public function graceful_exit() {
         exit;
     }

    /**
     * Die when the nonce check fails.
     * @return void
     */      
     public function invalid_nonce_redirect() {
        wp_die( __( 'Invalid Nonce', $this->plugin_text_domain ),
                __( 'Error', $this->plugin_text_domain ),
                array( 
                        'response'  => 403, 
                        'back_link' =>  esc_url( add_query_arg( array( 'page' => wp_unslash( $_REQUEST['page'] ) ) , admin_url( 'users.php' ) ) ),
                    )
        );
     }      
}

th-property-settings.php -表单文件

<?php

/**
 * The admin area of the plugin to load the Settings
 */

/** WordPress Administration Bootstrap */
require_once( ABSPATH . 'wp-admin/admin.php' );

/** WordPress Translation Installation API */
require_once( ABSPATH . 'wp-admin/includes/translation-install.php' );
?>

<div class="wrap">    
    <h2><?php _e( 'TM Property Settings', $this->plugin_text_domain); ?></h2>
        <div id="th-wp-property-settings">          
            <div id="nds-post-body">        

                <form action='./admin.php?page=TH_WP_Mail_Settings' method='post'>
<!--                <form action='options.php' method='post'>  -->

                    <h2>Targethousing Mail Setup Admin Page</h2>

                    <?php
                        settings_fields( 'TH_WP_Mail_Settings' );
                        do_settings_sections( 'TH_WP_Mail_Settings' );
                        submit_button();
                    ?>
                </form>

            </div>          
        </div>
</div>

class-admin.php 加载菜单项

public function add_plugin_admin_menu() {

    $menu_hook = add_menu_page(
                    __('TH Property Mailer', $this->plugn_text_domain),      //page title
                    __('TH Property Mailer', $this->plugin_text_domain),     //menu title
                    'manage_options',                                        //capability
                    $this->plugin_name ,                                     //menu slug, th-wp-property-mailer
                    array( $this, 'load_property_list_table' )               //page callback function
    );

    add_action( 'load-'.$menu_hook, array( $this, 'load_property_list_table_screen_options' ) );

    $page_hook = add_submenu_page( 
                    $this->plugin_name ,                                      //Menu slug
                    __( 'TH Property Settings', $this->plugin_text_domain ),  //page title
                    __( 'TH Settings', $this->plugin_text_domain ),           //menu title
                    'manage_options',                                         //capability
                    'TH_WP_Mail_Settings',                                    //submenu_slug, th-wp-property-mailersettings
                    array( $this, 'load_property_settings' )                  //page callback function*/

                );
    add_action( 'load-'.$page_hook, array( $this, 'load_property_settings_screen_options' ) );

0 个答案:

没有答案