Over the past couple of days I've been reading through the WordPress plugin docs and putting together my first plugin. I'm having some issues processing POST data from a form and acting on it.
I read this guide on Creating_Options_Pages but it seems geared towards updating existing values in the wp_options table, instead of creating new values in a custom table I've built myself.
A look at my code:
<?php
/*
Plugin Name: WP Link Directory
Version: 1.0
Plugin URI: http://www.blah.com
Author: Dux0r
Author URI: http://www.blah.com
Description: A Links Directory for WordPress
*/
register_activation_hook(__FILE__, 'wplinkdir_init');
add_action('admin_menu', 'wplinkdir_menu');
function wplinkdir_menu(){
add_menu_page('WP Link Directory', 'WP Link Directory', 8, __FILE__, 'wplinkdir_admin_page');
add_submenu_page(__FILE__, 'WP Link Directory','Main', 8, 'wplinkdir_main', 'wplinkdir_main_page');
add_submenu_page(__FILE__, 'WP Link Directory - Categories','Categories', 8, 'wplinkdir_categories', 'wplinkdir_category_page');
add_submenu_page(__FILE__, 'WP Link Directory - Options','Options', 8, 'wplinkdir_options', 'wplinkdir_options_page');
}
function wplinkdir_admin_page(){
echo 'I am some content.<br />';
}
function wplinkdir_main_page(){
echo 'This is the MAIN page.';
}
function wplinkdir_options_page(){
global $wpdb;
$cat_table = $wpdb->prefix."wplinkdir_cats";
$links_table = $wpdb->prefix."wplinkdir_links";
echo 'Add new category:<br /><br />
<form method="POST" action="options.php" name="new_category">';
wp_nonce_field('update-options');
echo '<table class="form-table">
<tr><td>Name:</td><td><input type="text" name="cat_name"></td></tr>
<tr><td colspan="2"><input type="submit" name="Submit" value="Add Category"></td></tr>
</table>
<input type="hidden" name="action" value="update" />
<input type="hidden" name="page_options" value="cat_name" />
</form>
</div>';
if($_POST['Submit']=='Add Category'){
$name=escape($_POST['name']);
$description=escape($_POST['description']);
mysql_query("INSERT INTO $cat_table VALUES ('','abc','def','')")or die('Problem: '.mysql_error());
echo 'New category added.';
}
}
What happens above is that when I hit Add Category it adds 3 new values to the wp_options table (action = 'update' & cat_name = '') instead of my if($_POST['Submit']=='Add Category'){ code.
Am I going about this the wrong way? Should I process the form using my own custom php file instead?