Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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:

  1. 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)
  2. After this function is run, it returns information about what was synced
  3. 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?

share|improve this question

1 Answer 1

Simply register an additional sub-admin page for your plugin More info here. Then perform a wp_remote_request() to fetch the data. Loop through the results, display it in some sort of view for the end user - for e.g. a WP_List_Table and save your post types. Name the admin sub menu entry "Updates" and you're done.

If you really need to implement another update button, use the same styles as get_submit_button() - the native WP admin button - uses and add them to an anchor/link that links to your sub-admin page. This as well separates the user feedback (the view) from the rest of the page, let's your plugin shine more, goes inline with WP core admin styles and overall let's your plugin look bigger with a better separation of concerns.

Hint: Click through all the links in this answer, as I linked to previous answers that explain the details.

share|improve this answer
    
Thank you kaiser. I will be trying this out once I get a chance. –  jlego Sep 6 '13 at 16:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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