1

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.

2 Answers 2

1

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...

<?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));
wp_redirect( home_url( '/form/' ) );
exit();
?>
2

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, like admin_init, check your POST vars and do your form processing there.

3
  • Thanks for your help. What would I use a custom post for? I don't know how that would help for what I'm working on, it's got nothing to do with posts. Commented Apr 2, 2014 at 3:28
  • Don't think of them as posts, they're just objects that you can add meta data to and organize via taxonomies. In your case, a "post" would be a show, with feed url saved as post meta. Wordpress uses a post type internally for the nav menu system. Plugin examples are Gravity Forms, where each post is a form, with form fields saved as meta data, Advanced custom fields uses a post type to store field groups, etc.. You get the UI and the API to create, update, and query for free, and since it's native WP data, the export tool lets you export your data to other sites.
    – Milo
    Commented Apr 2, 2014 at 3:43
  • Yeah, I know what custom post types are, you're assuming a lot about what it is I'm doing. I've already got a version of the plugin that does what I need it to do, it's not worth going into on here, but I don't need to do anything with post types. At the moment I'm trying to convert my plugin to be usable by other people, which is why I'm trying to build an options page. Commented Apr 2, 2014 at 4:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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