Installing Your Own Custom Tables Essay

1374 Words Sep 13th, 2016 6 Pages
Although it is generally recommended to avoid creating custom tables when you make WordPress plugins, but there are some situations that it is more efficient add your own custom tables to store your data.

Recently I was in a similar situation. My plugin required an additional database table. In today 's post, I 've put together some important considerations that will help you, when you create your own tables for your plugins.

Create tables on plugin activation

Custom database tables should be created upon plugin activation. As you may know, WordPress has a function that allows us to run a custom function when plugin gets activated.

register_activation_hook( __FILE__, 'cp_create_tables ' ); function cp_create_tables() { // Create database table(s)
Use table prefix and charset/collate variables

For some reasons such as better security or being able to have multiple WordPress installations in one database, WordPress allows us to set a custom prefix for database tables. It is controlled by a variable called prefix from $wpdb class. Many people change this prefix to something different from the default one , "wp_", for better security.

/** * WordPress table prefix * * You can set this to have multiple WordPress installations * in a single database. The second reason is for possible * security precautions. * * @since 2.5.0 * @access public * @var string */ public $prefix = ' ';
You will need to add the dynamic prefix to your own tables too, so you…

Related Documents