Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
0 votes
641 views
in Plugins by

I spotted the notes about init_queries in the docs, so in a new plugin I decided to use that function to initialize a database table.

Problem now is, in my admin_form function I try to select from that table, so when a user adds the plugin then opens Admin>Plugins, they get an error about the table not existing.

The "Initialize Database" button is there and works fine, but is there a better way to handle this?

Q2A version: 1.5.2

1 Answer

0 votes
by

You're right that it doesn't make much sense to show the error in this case. The solution is to make the following change to qa-install.php:

$errorhtml.=strtr(qa_lang_html('admin/module_x_database_init'), array(

... to ...

$errorhtml=strtr(qa_lang_html('admin/module_x_database_init'), array(

[period removed before = sign]

In the meantime, if you wnat to avoid this behavior in current versions of Q2A, your plugin should first check whether the table exists. The function qa_db_list_tables_lc(...) might be helpful for this.

by
Cool, thanks. One thing I did try was to set an option in the init_queries function, e.g. qa_opt('myplugin_active', 1), then I would just check the option before running queries. However, this sets the option regardless (the init_queries function is obviously executed in order to check that initialisation is required).

Perhaps instead of only returning a query from the function, we could include an option to set, for example:

return array(
  'query' => 'CREATE TABLE ...',
  'option' => array('myplugin_active', 1),
);
by
You could condition the setting of that option on whether or not your table is there in the list passed to init_queries(...).
by
Good idea, thanks!
...