I have an option screen for my plugin, it’s just a simple form.
<div class="wrap">
<h2>SoundCloud Podcast Importer Options</h2>
<hr/>
<h2>Add a show</h2>
<form method="post" action="../wp-content/plugins/SCImp-Pro/scimp_add_feed.php">
<p>Feed URL: <input type="text" name="scimp_feed_url" /></p>
<p>Show Category: <select name="scimp_show_category">
<?php
$categories=get_categories();
foreach ($categories as $category) { ?>
<option value="<?php echo $category->name; ?>"><?php echo $category->name; ?></option>
<?php } ?>
</select></p>
<?php submit_button(); ?>
</form>
</div>
As you can see, I take the form data, and send it to a php script, which adds it to the database using $wpdb. It looks like this:
<html>
<body>
<?php
require_once('../../../wp-config.php');
require_once('../../../wp-load.php');
global $wpdb;
$feedurl=$_POST['scimp_feed_url'];
$showcategory=$_POST['scimp_show_category'];
$table_name = $wpdb->prefix . 'scimp';
$wpdb->insert( $table_name, array('feedurl' => $feedurl, 'category' => $showcategory));
?>
</body>
</html>
Everything is working just fine so far. However, when the script has added the data to the database, I want to end up on the original options page I started at. I intend to add some code to the options page that will display what’s in the database table, I just don’t know how to take a user back there.
Is this something I could do with Javascript? Maybe the script could be run without taking the user away from the options page at all? I’m learning as I go, but I know next to nothing about what I can do with AJAX and Javascript.
You said your original code is working.
I think you should try using wp_redirect but you can’t output any HTML/Headers for that.
Untested, but try the following:
Note: intentionally missed out the html/body tags…
Well first of all, it seems that what you’re doing would be far easier, and more in line with WordPress best-practices to not use your own table. Use a custom post type and post meta data, or use the Settings API. Don’t reinvent wheels. You also make your data more portable, tools that correctly use the WordPress API can export/import and back up your data.
However, for the sake of answering the question (if you were to ignore the above advice) don’t send to an external php script, omit the
action
in your form so you submit the form to the same page, then hook an early action, likeadmin_init
, check yourPOST
vars and do your form processing there.