I am new in plugin development. I created a plugin with the help of http://codex.wordpress.org/Creating_Options_Pages

<?php

/*
Plugin Name: 1pluginframework
Plugin URI:
Description: Custom plugin for lendingbig website.
Version: 1.0
Author: Jahur Ahmed
Author URI:
License: GPLv2 or later
*/

// create custom plugin settings menu
add_action('admin_menu', 'baw_create_menu');

function baw_create_menu() {

    //create new top-level menu
    add_menu_page('BAW Plugin Settings', 'BAW Settings', 'administrator', __FILE__, 'baw_settings_page',plugins_url('/images/icon.png', __FILE__));

    //call register settings function
    add_action( 'admin_init', 'register_mysettings' );
}

function register_mysettings() {
    //register our settings
    register_setting( 'baw-settings-group', 'new_option_name' );
    register_setting( 'baw-settings-group', 'some_other_option' );
    register_setting( 'baw-settings-group', 'option_etc' );
}

function baw_settings_page() {
?>
<div class="wrap">
<h2>Your Plugin Name</h2>

<form method="post" action="options.php">
    <?php settings_fields( 'baw-settings-group' ); ?>
    <?php do_settings_sections( 'baw-settings-group' ); ?>
    <table class="form-table">
        <tr valign="top">
        <th scope="row">New Option Name</th>
        <td><input type="text" name="new_option_name" value="<?php echo get_option('new_option_name'); ?>" /></td>
        </tr>

        <tr valign="top">
        <th scope="row">Some Other Option</th>
        <td><input type="text" name="some_other_option" value="<?php echo get_option('some_other_option'); ?>" /></td>
        </tr>

        <tr valign="top">
        <th scope="row">Options, Etc.</th>
        <td><input type="text" name="option_etc" value="<?php echo get_option('option_etc'); ?>" /></td>
        </tr>
    </table>

    <?php submit_button(); ?>

</form>
</div>
<?php echo get_option('new_option_name'); ?>
<?php } ?>

I put this code in theme's header.php

<?php echo get_option('new_option_name'); ?>

When I deactivate this plugin, the value of <?php echo get_option('new_option_name'); ?>
is not disappeared. But this value must show when plugin is activated and will not show when it is deactivated.

Any suggestion is highly appreciated.

Thanks in advance to all.

Dani AI

Generated

Short answer: WordPress keeps option values in the database after a plugin is deactivated. Because placed an echo of the saved option in the theme header, that value will continue to appear until the option is explicitly removed or the theme checks whether the plugin is active. was on the right track suggesting removal, but there’s a common typo to watch for: use delete_option (singular), not delete_options.

Recommended, safe approach (remove data on uninstall, not on every deactivation). Put this in the main plugin file (or use an uninstall.php) so WordPress will run it when the plugin is uninstalled:

register_uninstall_hook( __FILE__, 'baw_uninstall' );

function baw_uninstall() {
    delete_option( 'new_option_name' );
    delete_option( 'some_other_option' );
    delete_option( 'option_etc' );
}

If the goal is simply to stop printing the value when the plugin is deactivated, avoid deleting options on deactivation. Instead have the theme print the option only when the plugin is loaded (check for a plugin function or constant) and escape the output:

if ( function_exists( 'baw_settings_page' ) ) {
    echo esc_html( get_option( 'new_option_name', '' ) );
}

Quick troubleshooting checklist tied to the thread:

  • Confirm the exact option names registered and delete those exact names (mismatched names are very common).
  • Fix the typo delete_optionsdelete_option.
  • Ensure hooks (register_uninstall_hook / register_deactivation_hook) are declared in the main plugin file.
  • For multisite/network plugins use delete_site_option when appropriate.
  • Prefer uninstall-time cleanup to avoid surprising site owners who only deactivate temporarily.

This resolves the symptom observed by while following ’s intent, and preserves safer data-handling practices.

Recommended Answers

All 7 Replies

you need to use the delete_option(); on a deactivation method

register_deactivation_hook( __FILE__, 'remove_options' );

function remove_options()
{
    delete_options('plugin_options');
}

Hi gabrielcastillo,

Thanks for your answer. But it's not working, may be for my fault.

Here is my activation and deactivation code

function pluginframework_activation() {
}
register_activation_hook(__FILE__, 'pluginframework_activation');


function pluginframework_deactivation() {
}
// register_deactivation_hook(__FILE__, 'pluginframework_deactivation');

    register_deactivation_hook( __FILE__, 'remove_options' );
    function remove_options()
    {
    delete_options('plugin_options');
    }

You have the right code.. you just need to change plugin_options to the name of your options array name.

So if you do register_settings('p_option', 'plugin_options'); you would need to do delete_options('p_option');

Hi gabrielcastillo,
You are helping me a lot. :)

Can you give me full code? I am really new to this.

When creating options for plugin you need to register options settings array
Doc

So when you want to delete the settings options array, you would need to use delete_options with the registered settings var.

Doc

You could also use uninstall hook as well

Doc

I would need to see your code in order to investigate where you are going wrong.

Thanks gabrielcastillo! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.