• kirkward

    (@kirkward)


    I’m creating my first plugin and creating an admin options page. It almost works, or should I say, it did on my development site.

    I copied the code to my live site, and only part of it works.

    Any ideas why it is not doing the select properly, or updating all of the options instead of just the last?

    <?php
    /*
    Plugin Name: My little plugin
    Plugin URI: https://mysite.com
    Description: A plugin.
    Version: 0.0.1
    Author: Kirk
    Author URI: https://mysite.com
    License: Custom, Unlicensed, Take Your Chances
    */
    
    /* What to do when the plugin is activated? */
    register_activation_hook(__FILE__,'mysite_pro_niches_plugin_install');
    
    /* What to do when the plugin is deactivated? */
    register_deactivation_hook( __FILE__, 'mysite_pro_niches_plugin_remove' );
    
    function mysite_pro_niches_plugin_install() {
    /* Create a new database field */
    add_option("niche1", 'autobody', '', 'no');
    add_option("n1t1", 'Enter your first testimonial', '', 'no');
    add_option("n1t2", 'Enter your second testimonial', '', 'no');
    }
    
    function mysite_pro_niches_plugin_remove() {
    /* Delete the database field */
    delete_option('niche1');
    delete_option('n1t1');
    delete_option('n1t2');
    }
    
    add_action('admin_menu', 'mysite_pro_niches_admin_menu');
    function mysite_pro_niches_admin_menu() {
    add_options_page('Plugin Admin Options', 'Pro Niches 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'); ?>
    <center>
    <br /><br />
    <h2>You can change your niche from <?php $option = 'niche1'; $default = ''; echo get_option( $option, $default ); ?> by selecting it in the box below<br />and then clicking on the "Save Changes" button.</h2>
    <br /><br />
    <?php $current_niche = get_option(niche1); ?>
    <select name="niche1">
        <option value="none" <?php if ($current_niche == "none") echo ('selected="selected"'); ?> >Clear Database</option>
        <option value="autobody" <?php if ($current_niche == "autobody") echo ('selected="selected"'); ?> >Autobody Repair</option>
        <option value="builders" <?php if ($current_niche == "builders") echo ('selected="selected"'); ?> >Builders and Contracters</option>
        <option value="c-stores" <?php if ($current_niche == "c-stores") echo ('selected="selected"'); ?> >Convenience Stores</option>
    </select>
    <br />
    <br />
    <textarea name="n1t1" rows="5" cols="100" wrap="soft" maxlength="250" ><?php echo get_option(n1t1); ?></textarea>:<br />
    <textarea name="n1t2" rows="5" cols="100" wrap="soft" maxlength="150" ><?php echo get_option(n1t2); ?></textarea>:<br />
    <br />
    <br />
    	<input type="hidden" name="action" value="update" />
    	<input type="hidden" name="page_options" value="niche1" />
    	<input type="hidden" name="page_options" value="n1t1" />
    	<input type="hidden" name="page_options" value="n1t2" />
    	<input type="submit" value="Save Changes" />
    	</form>
    	</p>
    	</div>
    <?php
    }
    // End of page
    ?>
Viewing 1 replies (of 1 total)
  • Thread Starter kirkward

    (@kirkward)

    Note: I was wrong in my assessment of my test site. I was trying to figure out what changed when I discovered the test site is also only saving one option.

    So, correct question is, what did I do wrong, and how can I fix it?

Viewing 1 replies (of 1 total)
  • The topic ‘My First Plugin With Options Page’ is closed to new replies.