WordPress.org

Ready to get started?Download WordPress

Forums

Saving Textarea With opdate_option (3 posts)

  1. kirkward
    Member
    Posted 1 year ago #

    I have a form on a plugin options page that is working fine, saving the selected item as an option. I want to also be able to add a textarea to the same form and have it saved as a second option field.

    I cannot figure out how to get the textarea to be recognized as an option field. I would appreciate any help anyone can give an old geezer and cut/paste/test non-coder type.

    Here is what I have, along with some notes showing what I;m trying to do ...

    <?php
    /*
    Plugin Name: MyNichePlugin
    Plugin URI: http://mysite.com
    Description: A plugin that releases niche pages for Pro Level upgrades.
    Version: 0.0.1
    Author: Good Old Me
    Author URI: http://mysite.com
    License: Do what you want with it, but don't blame me.
    */
    
    /* What to do when the plugin is activated? */
    register_activation_hook(__FILE__,'mysite_plugin_install');
    
    /* What to do when the plugin is deactivated? */
    register_deactivation_hook( __FILE__, 'mysite_plugin_remove' );
    
    function mysite_plugin_install() {
    /* Create a new database field */
    add_option("niche1", 'autobody', '', 'no');
    }
    
    function mysite_plugin_remove() {
    /* Delete the database field */
    delete_option('niche1');
    }
    
    add_action('admin_menu', 'mysite_admin_menu');
    function mysite_admin_menu() {
    add_options_page('Plugin Admin Options', 'mysite Plugin Settings', 'manage_options', 'mysite_pro_niches', 'plugin_admin_options_page');
    }
    
    function plugin_admin_options_page() {
    ?>
    	<div class="wrap">
    	<?php screen_icon(); ?>
    	<h2>Plugin Options Admin Page</h2>
    	<p>
    	<form method="post" action="options.php">
    <?php wp_nonce_field('update-options'); ?>
    
    <?php $current_niche = get_option(niche1); ?>
    <select name="niche1">
        <option value="autobody" <?php if ($current_niche == "autobody") echo ('selected="selected"'); ?> >autobody</option>
        <option value="builders" <?php if ($current_niche == "builders") echo ('selected="selected"'); ?> >builders</option>
        <option value="c-stores" <?php if ($current_niche == "c-stores") echo ('selected="selected"'); ?> >c-stores</option>
    </select>
    
    // Here's what I'm trying to add
    <textarea style="width: 25%; height: 250px;" maxlength="1234"><?php $option = 'niche2'; $default = 'Testimonial'; echo get_option( $option, $default ); ?></textarea>
    
    // And, then a second one
    <textarea style="width: 25%; height: 250px;" maxlength="1234"><?php $option = 'niche3'; $default = 'Testimonial'; echo get_option( $option, $default ); ?></textarea>
    
    // But I don't know how to save anything but the first option field
    	<input type="hidden" name="action" value="update" />
    	<input type="hidden" name="page_options" value="niche1" />
    	<input type="submit" value="Save Changes" />
    	</form>
    	</p>
    	</div>
    <?php
    }
    // End of page
    ?>
  2. Rebecca Schaefer
    Member
    Posted 1 year ago #

    Looks like your HTML is incorrect for starters. You have to give your textarea a name so it can be passed to $_POST when you save the form.
    <textarea name="my_text_area">the value is here</textarea>

    Then when you want to retrieve whatever was entered

    <?php
    $myTextArea = $_POST['my_text_area'];
    update_option('my_text_area', $myTextArea);
    ?>
  3. kirkward
    Member
    Posted 1 year ago #

    Thanks Rebecca,

    First, I apologize that I missed the notification that you had posted. I just happened to see your response while continuing to search for an answer.

    I corrected the textarea naming to the names I want to use, and the script is properly adding the options at activation and deleting the options at deactivation, but when I save, it is only saving the last option in the list associated with the submit button.

    Any other ideas I might try?

Topic Closed

This topic has been closed to new replies.

About this Topic