1

I'm creating a plugin that adds some data to a custom table created by my plugin on plugin activations.

I also created a settings page with a form that users can insert their own data to the database.

The problem Everything is working fine but each time i deactivate(i don't mean uninstall) and reactivate the plugin, the data get duplicated in the table.

Mind you, due to the large amount of data, i chose to use "longtext" as the table column type.

Making the columns type is not an UNIQUE.

My question How do i prevent the plugin from inserting the same data that was meant to be inserted int the database just once on plugin activation?

2 Answers 2

0

Add a check when the plugin is activated to see if the data(column/table) already exists in db or not ?

If the data aldready exists then skip the db part and just mark the plugin as active.

0

Found a great solution.

Basically, you can check if an option name exists and if it already exists you don't insert your data.

The main idea is, each time your database schema changes and requires an update on your user’s database, you need to increment the DB_VER by 1, then write a database upgrade routine for the current DB_VER. This would create some sort of an evolution trail of your database schema, which is very helpful when your user needs to upgrade from an ancient version to the latest one. WordPress itself keeps track of its schema changes this way, so it’s always safe to upgrade from WordPress 2.x to 3.x without much issue.

Full explanation on how this work ca be found at here http://solislab.com/blog/plugin-activation-checklist/#manage-version

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.