So I was working on a plugin and I needed to cache some third party API access, and I needed to manually create a database upon plugin activation or upgrade.

The caching stuff needs a database host ( localhost I guess ), a username and a pass, as in the code:

mysql_connect($db_host, $db_username, $db_password) or die(mysql_error());
mysql_select_db($db_name) or die(mysql_error());

I have followed many tuts in terms of creating databases, but I couldn't end solving my issue.

Any thoughts ?


Thank you lots!

  • 5
    Are you sure you need to create an entire database? That is very uncommon and many hosting providers won't allow it. Do you want to create some additional tables? If so, consider using the built-in $wpdb object. – TheGentleman Apr 21 '15 at 16:49
  • oh yes exactly I meant a table thanks @GentlemanMax !! will check that class then :) – Samuel Elh Apr 21 '15 at 16:51

As per GentlemanMax's comment, you'll want a table.

There's a really useful guide in the Codex (which also goes into how to do DB upgrades / migrations.)

One thing I'd emphasise: make sure to use $wpdb->prefix in the table name because not all sites will have tables beginning wp_*.

Also, if you're ever planning to distribute your plugins to other people, remember not every WordPress cloud hosting services will allow their customers to use custom tables (some of the larger ones allow the standard WordPress tables or preapproved plugins only – which is irritating but worth bearing in mind.)

| improve this answer | |
  • Thanks!! I was actually checking my phpmyadmin databases and I couldn't see the new table my plugin creates, stupid lol. I just found the table in the site db.. thanks for the codex link, will follow with the instructions. Thank you again, and thanks GentlemanMax :) – Samuel Elh Apr 21 '15 at 18:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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