• Resolved Seans0n

    (@seans0n)


    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: https://www.blah.com
    Author: Dux0r
    Author URI: https://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?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator Samuel Wood (Otto)

    (@otto42)

    www.remarpro.com Admin

    You’re right. That is geared entirely towards inserting options only. If you want to do your own thing, then do this:

    First, get rid of the action in the form entirely. Do this instead:
    <form action="" method="post">

    Next, use your own nonce name.
    wp_nonce_field('wplinkdir-nonce');

    Also, check your nonce if you’re going to have one.
    check_admin_referer( 'wplinkdir-nonce' );

    Another thing, don’t escape your own data or make your own DB calls. Let WordPress handle it for you:

    global $wpdb;
    $wpdb->query($wpdb->prepare("INSERT INTO $cat_table VALUES (%s,%s,%s,%s)", $value1, $value2, $value3, $value4));

    Use %d for number values, %s for strings. Return value of the query will be number of rows inserted/updated/deleted if successful.

    Next, don’t echo your form first. Check the submit first in the function, then echo the form afterwards. Something like this:

    function wplinkdir_options_page () {
    global $wpdb;
    if ( isset($_POST['submit']) )
    {
    	// are they allowed to do that?
    	if ( function_exists('current_user_can') && !current_user_can('manage_options') )
    		die(__('Cheatin’ uh?'));
    
    	// check the nonce
    	check_admin_referer( 'wplinkdir-nonce' );
    
    	// do your update stuff here
    }
    ?>
    <?php if ( !empty($_POST ) ) : ?>
    <div id="message" class="updated fade">
    <strong>Your setting has been saved message goes here....</strong>
    </div>
    <?php endif; ?>
    <div class="wrap">
    <h2>Settings Page</h2>
    <form action="" method="post">
    <?php wp_nonce_field('wplinkdir-nonce'); ?>
    ... blah, form...
    Thread Starter Seans0n

    (@seans0n)

    That’s absolutely perfect, you’ve helped me out again. Just wanted to say thanks this time =]

    Thank you Otto – I too am writing my first plugin – this was extremely helpful.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Issue With Options Pages and POST’ is closed to new replies.