This is from inside my WordPress plugin, inside the main file:
function my_plugin_install() {
$my_site_url = get_site_url();
$my_options['my_site_url'] = $my_site_url;
// Save
}
register_activation_hook(__FILE__, 'my_plugin_install');
Currently, the install is successful but the ‘my_site_url’ option is not saved. I’m assuming because the way I’m using the $my_options array at this point doesn’t mean anything. It should save this data to the wp_options table.
I can’t seem to get this to save, or even find a way to test this as using “echo” gives WordPress an error during install. Is there a best method for running a script and updating the database during install?
Thanks in advance.
You need to use the WordPress function update_option to save your option value:
And then later, when you need that value, you can use get_option:
*UPDATE
Since it appears you want to manage multiple of your own options, then I suggest using a simple “utility” function, like so:
And, an appropriate getter function:
Then, rather than calling
update_option
directly, you’ll call this function, as illustrated below:And, to get one of your options: