How to run a function on plugin's options page?
I am writing a plugin that syncs data from a third party service to custom post types in wordpress via HTTP/SOAP. There will be the option to run it on an interval (using wP_cron functions) but I also want to include a 'Sync' button to be able to manually pull in or update data without having to wait for the next time the cron runs.
I have an options page for the plugin page setup using Wordpress's Settings API, which works nicely.
Ideally, this is what I would like to happen:
- When the 'Sync Now' button is clicked, a php function from the main plugin's php file is run (connecting to the third-party service and syncing data)
- After this function is run, it returns information about what was synced
- This information is displayed in an alert box or a panel of some sort to let the user know about what was synced and if anything went wrong.
For some reason, I cannot wrap my head around how this should work.
Here are some things I have tried:
The sync function itself works. I have added a temporary checkbox called "Sync Now" to the settings form and called the update function as the callback when registering that particular setting (
register_setting()
), but it will run this function regardless of the value of the checkbox.register_setting( 'my_plugin_settings', 'sync_now_option', 'syncNowFunction');
When I have a separate form than the one generated by the Settings API on the page that only has a button which calls GET to another php file (not the main plugin php file), I get a 500 error. The 500 error seems to be thrown if there is any type of error at all. (Can't find an include file, function not found.) I cannot use anything besides vanilla php or it throws the 500 error. I have attempted call the file several different way all with the same results:
- Visiting it at the URL in the browser
- Having the form's action point to that URL
form method="GET" action="http://url.com/wp-content/plugins/my-plugin/sync.php" input type="submit" value="Sync Now" /
`- Using jQuery's
.load()
and .ajax()
It seems odd that it throws a 500 error no matter what the issue seems to be.
- I have also tried to use Ajax to load a a function from the main plugin file directly to no avail.
I don't really mind if the page is reloaded, but I do not want the user to be redirected to another page afterwards.
How might I accomplish this?
Topic wp-remote-request plugin-development Wordpress
Category Web