Support » Plugins » Hacks » Using wp_editor in "own" Plugin

  • Resolved Blubsi


    Hi guys,I’m trying to get this working for a few hours now.
    What I was trying to achieve was a plugin where the user could set text into a global variable,so I can post that variable on another page.
    wp_editor came in handy since the user also can format everything he writes.
    But I can’t get it to work,it doesn’t save text into that post after hitting the Save Button.
    Maybe I’m totally wrong and it doesn’t need a post or maybe saving can be solved somewhat different. Maybe my Idea was totally wrong and somebody knows a way better Idea?
    Any suggestions are Welcomed.
    Thanks in advance for your help!

    // Specify Hooks/Filters
    register_activation_hook(__FILE__, 'add_defaults_fn');
    add_action('admin_init', 'sampleoptions_init_fn' );
    add_action('admin_menu', 'sampleoptions_add_page_fn');
    $options = get_option('plugin_options');
    global $test;
    global $inhalt;
                            $test='<div class="box basic">';
                            $test='<div class="box gradient">';
    // Define default option settings
    function add_defaults_fn() {
    	$tmp = get_option('plugin_options');
    // Register our settings. Add the settings section, and settings fields
    function sampleoptions_init_fn(){
    	register_setting('plugin_options', 'plugin_options', 'plugin_options_validate' );
    	add_settings_section('main_section', 'Haupteinstellungen', 'section_text_fn', __FILE__);
    	//add_settings_field('plugin_textarea_string', 'Ihr Text:', 'setting_textarea_fn', __FILE__, 'main_section');
    	add_settings_field('radio_buttons', 'Hintergrund:', 'setting_radio_fn', __FILE__, 'main_section');
    // Add sub page to the Settings Menu
    function sampleoptions_add_page_fn() {
    	add_options_page('FrontQuote', 'FrontQuote', 'administrator', __FILE__, 'options_page_fn');
    // ************************************************************************************************************
    // Callback functions
    // Section HTML, displayed before the first option
    function  section_text_fn() {
    	echo '<p></p>';
    // TEXTAREA - Name: plugin_options[text_area]
    function setting_textarea_fn() {
    	$options = get_option('plugin_options');
    	echo "<textarea id='plugin_textarea_string' name='plugin_options[text_area]' rows='7' cols='50' type='textarea'>{$options['text_area']}</textarea>";
    // RADIO-BUTTON - Name: plugin_options[option_set1]
    function setting_radio_fn() {
    	$options = get_option('plugin_options');
    	$items = array("Durchsichtig", "Weiss");
    	foreach($items as $item) {
    		$checked = ($options['option_set1']==$item) ? ' checked="checked" ' : '';
    		echo "<label><input ".$checked." value='$item' name='plugin_options[option_set1]' type='radio' /> $item</label><br />";
    // Display the admin options page
    function options_page_fn() {
    	<div class="wrap">
    		<div class="icon32" id="icon-options-general"><br></div>
    		Einstellen, Aktiviren/Deaktivieren und Administrieren des Zitats auf der Homepage
    		<form action="options.php" method="post">
    		<?php settings_fields('plugin_options'); ?>
    		<?php do_settings_sections(__FILE__); ?>
    			$options = get_option('plugin_options');
    	$options = get_option('plugin_options');
    	$post = get_post( 1850, 'OBJECT' );
    	$settings = array(
    		' textarea_name' => 'somecontent',
    	wp_editor($post->post_content, 'textarea02', $settings );
    	update_option(1850 ,$_POST[1850]);
    		<p class="submit">
    			<input name="Submit" type="submit" class="button-primary" value="<?php esc_attr_e('Save Changes'); ?>" />
    // Validate user data for some/all of your input fields
    function plugin_options_validate($input) {
    	// Check our textbox option field contains no HTML tags - if so strip them out
    	$input['text_string'] =  wp_filter_nohtml_kses($input['text_string']);
    	return $input; // return validated input
Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz


    If you want to use the Settings API to manage the content, that is fine, but the value will not be saved into a post, it is saved in options like any other setting. You page should pull the content from there, not a post. If you stick with this approach, keep this in mind while carefully going through your code, you’ve made a few bad assumptions regarding how data gets managed with the Settings API. All you really need to do is register the field and get it displayed and the API handles the rest.

    Another possibility could be to create a custom post type such that only admins can add new ones, resulting in there really being only one post anyone can edit. For non-admins, remove the usual post type menu items and add a menu link that opens the one existing post for editing.

    Thanks alot for your reply bcworkz!
    For everyone who encounters the same issue:

    function setting_editor_fn() {
    $options = get_option('plugin_options');
    $args = array("textarea_name" => "plugin_options[textarea_one]");
    wp_editor( $options['textarea_one'],"plugin_options[textarea_one]", $args );

    Don’t forget to:

    add_settings_field('editor', null, 'setting_editor_fn', __FILE__, 'main_section');

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Using wp_editor in "own" Plugin’ is closed to new replies.