如何让wp_editor在插件管理页面中保存数据

问题描述 投票:2回答:2

我正在尝试使用插件设置api来添加wp_editor,但是text / html没有保存。

// add the admin settings and such
add_action('admin_init', 'wp_myplugin_admin_init');
function wp_myplugin_admin_init(){
register_setting( 'wp_myplugin_settings', 'wp_myplugin_settings', 'wp_myplugin_settings_validate');
add_settings_field('wp_myplugin_user_custom_text', __('Enter your message','WP-wp_myplugin'), 'wp_myplugin_user_custom_text', 'wp_myplugin', 'wp_myplugin_main');

function wp_myplugin_user_custom_text() {
$options = get_option('wp_myplugin_settings');
$settings  = array('textarea_rows' => 5,'textarea_name' => 'user_cutom _text_msg');
wp_editor( $options['user_custom_text'],'user_custom_text', $settings  );}  

// validate  
function wp_myplugin_settings_validate() {
$options = get_option('wp_myplugin_settings');
$user_custom_text = $input['user_custom_text'];

if ( empty($user_custom_text) ){
$options['user_custom_text'] = $user_custom_text;
}else{
$options['user_custom_text'] =  __('Enter your own text','WP-wp_myplugin');// as set when the plugin activated
wordpress wordpress-plugin wp-editor
2个回答
0
投票

我使用了$ input ['user_custom_text'];

我需要的只是$ _POST ['user_custom_text'];

也要让媒体工作需要wordpress Sanitize:

<?php wp_kses_post( $data ); ?>

http://codex.wordpress.org/Function_Reference/wp_kses_post


0
投票

如果有人在Wordpress Codex上按照Creating Options Page上的步骤进行操作并且很难保存输入,您可以使用以下代码确保wp_editor保存您的输入。

<?php
class MySettingsPage
{
    /**
     * 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 Settings', 
            'manage_options', 
            'my-setting-admin', 
            array( $this, 'create_admin_page' )
        );
    }

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

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

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

        add_settings_field(
            'my_content', //This ID of the field you want to use wp_editor for
            'Write Your Content Here', 
            array( $this, 'my_content_callback' ), 
            'my-setting-admin', 
            'setting_section_id'
        );      
    }

    /**
     * Sanitize each setting field as needed
     *
     * @param array $input Contains all settings fields as array keys
     */
    public function sanitize( $input )
    {
        $new_input = array();

        if( isset( $input['my_content'] ) )
            $new_input['my_content'] = wp_kses_post( $input['my_content'] );

        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 my_content_callback()
    {
        $args = array (
            'media_buttons' => false,
            'textarea_rows' => '10',
            'textarea_name' => 'my_option_name[my_content]'
        );
        $options = get_option('my_option_name');
        wp_editor( $options['my_content'], 'my_content', $args );
    }
}

if( is_admin() )
    $my_settings_page = new MySettingsPage();
© www.soinside.com 2019 - 2024. All rights reserved.