WordPress.org

Ready to get started?Download WordPress

Forums

Saving Plugin Preference (2 posts)

  1. sumone4life
    Member
    Posted 4 years ago #

    Hello,

    I am making a plugin that needs to save preferences based on the post its attached with. I assume that i need to write the settings to the database so that they can be retrieved later.

    Is there an easy way to do this?

  2. WatchCount.com
    Member
    Posted 4 years ago #

    Yes, there definitely is. When I first needed to (learn to) do the same thing, I was a little worried I would need to brush up on mysql commmands. Then I found that there was a layer of abstration that WordPress introduced to make it easier to write to the DB - something like the $wpdb class (in PHP) that gave easy access to the DB.

    Then I discovered that they made it even easier for reading/writing/saving plugin preferences to the WPDB without even needing to use objects/classes/etc. Just a simple function, get_option(), retrieves options from the WP DB that you wrote with update_option(). The format is pretty much a simple name-value pair.

    As you learn about and play with these functions, here are 3 tips for good implementation:

    1) update_option() is great for both adding a new option, and updating one - usually no need to bother with the add_option() function at all.

    2) Instead of writing many options to the DB for all of your plugin's settings, store all of your settings in 1 array, then store that 1 array as 1 simple option in the WP DB.

    3) When your plugin is uninstalled/deleted, run delete_option() to remove the option(s) from the WP DB.

    -WCC

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.