WordPress.org

Ready to get started?Download WordPress

Forums

Transient Cleaner
[resolved] Easy fix: PHP Notice: Undefined index: clean_optimize (3 posts)

  1. Jeremy Clarke
    Member
    Posted 3 months ago #

    Hi there, very helpful plugin for debugging my use of transients for caching!

    When I look at the PHP Error log for my site I see a small error that can be easily fixed. Here's what I get in the log:

    [02-Apr-2014 22:37:46] PHP Notice:  Undefined index:  clean_optimize in /•••/wp-content/plugins/artiss-transient-cleaner/includes/options-general.php on line 23

    PHP notices aren't a huge deal, but it's important that they are cleared up because otherwise they create noise in error logs that developers want to watch for real errors that come up in their code. Luckily they are very easy to fix.

    Here's the section of your plugin causing the notice:

    $options[ 'clean_enable' ] = $_POST[ 'clean_enable' ];
    	$options[ 'clean_optimize' ] = $_POST[ 'clean_optimize' ];
    	$options[ 'upgrade_enable' ] = $_POST[ 'upgrade_enable' ];
    	$options[ 'upgrade_optimize' ] = $_POST[ 'upgrade_optimize' ];

    The notice happens when $_POST doesn't contain a key called 'clean_optimize'. It's against the rules of PHP to use a key that doesn't exist, even though the effect seems the same as if it existed but was empty.

    What you need to do is check each value before using it to see if it's there, like:

    if (isset($_POST[ 'clean_optimize' ]))
    	$options[ 'clean_optimize' ] = $_POST[ 'clean_optimize' ];

    You should do this any time you are checking the contents of $_POST or any other array where you're not sure if the key will exist or not.

    It's easy to find these errors while working on your plugin by keeping the WP_DEBUG constant enabled on your test installation. This will show you the errors as they come up, and is standard practice, you can read all about it on the Codex:

    https://codex.wordpress.org/Debugging_in_WordPress

    Thanks!

    https://wordpress.org/plugins/artiss-transient-cleaner/

  2. Dark Designs
    Member
    Plugin Author

    Posted 3 months ago #

    Hi Jeremy,

    Yes, that's our bad - we normally check but, in this case, forgot.

    We'll push out an update later today.

    DD

  3. Jeremy Clarke
    Member
    Posted 3 months ago #

    Danke!

Reply

You must log in to post.

About this Plugin

About this Topic