Save Plugin Version Number as Option?

I seem to recall seeing a tip somewhere saying that it was a best practice to save a plugin’s version number as an option. I’m working through releasing a plugin, and I’m considering whether to do it, but since all the plugin does is make a widget (right now, it literally has no other options), I’m struggling to understand what I would ever do with that option. I’m already setting a constant with the version number for use in a couple places (mostly wp_enqueue_*).

Can someone either point me to a good resource or explain the use cases for saving the version number as an option?

Related posts

Leave a Reply

3 comments

  1. You need to save the version to the database– aka. save it “as an option”– so that your script has a comparison case. That is, your script should…

    1. Check the database for a version number
    2. Compare that version number to the new version number– a variable or constant in your plugin file.
    3. Update things if need be
    4. Save the new version number to the database

    If all you have is a constant in your .php file. It always matches, meaning it is pretty much useless for any automated updating. Of course, you can keep track of the plugin versions with just the constant, but the point of saving it to the database is to be able to automatically update things when needed.

    As you say, if the plugin is simple enough it may not be necessary.

  2. Basically, yes, you store it in both formats, as an option and as a constant for comparative purposes. It provides a layer of checks and balances that is no more difficult to implement than not doing it in the first place. Truth…

    Refer to the following Q&A thread for some nice examples how:
    Note: none of the code shown here is mine so full credit where credit is due!


    Question: Releasing new plugin version, how to rename old options keys?

    However,

    Please don’t accept this answer as the correct answer, I’m just providing additional, supporting material that I have as a reference and which I think supports this topic well.

    Do however show your support and up-vote their answers if you use them with any success.

  3. I think you are referring to the Wrox WordPress Plugin book or perhaps something on Ozh et als Blog. I’ve also just read it referenced in the Apress WordPress plugin guide.

    The argument is that by recording the version number in the database, then when you issue an update to the plugin, you have more control over what to ‘update’ … the code looks something like this

    DEFINE VERSION (1.0);
    
    function add_settings();
    if (get_option(youroptions[VERSION]) === false { 
     \ ie first time user
        add_option(youroptions[version]) = VERSION;
        add_option(youroptions[anotheroption]) ;
    }
    elseif (get_option(youroptions[VERSION]) !='1.0'{ 
     \user has old plugin ... do something for them
       add_option(youroptions[optionname])
    }
    

    Does that help? I’m using the code sample above from the Apress WP book (but I typed this in from memory).