how to get values from a form(form is in admin panel page) and put that values in separate variables

        Plugin Name: calc plugin 

    add_action('admin_menu', 'my_plugin_menu');

    function my_plugin_menu() {
        add_options_page('My Plugin Options', 'My Plugin', 8, 'your-unique-identifier', 'my_plugin_options');

    function my_plugin_options() {
        echo '<div class="wrap">';
        echo '<p>Enter Two locations </p>
              <form action="#" name="mapform" id="mapform">
                      Location A: <input type="text" name="A"><br />
                      Location B: <input type="text" name="B"><br />
                      <input type="submit" name="submit" value="Update">
        echo '</div>';
  • thanks for the edit badge ... i am just a beginner to plugin development – lucky May 9 '12 at 6:15
  • @RutwickGangurde can you help em with this prob ? – lucky May 9 '12 at 6:21

If you want to add an settings page, (or add options to and existing settings page) please use the settings api:

1. Register your settings and add a section:

add_action('admin_init', 'plugin_admin_init');
function plugin_admin_init(){
     register_setting( 'wpse51578_options_group', 'my_option_field_name', 'wpse51578_validation_callback' );
     add_settings_section('wpse51578_main_id', 'My Main Settings', 'wpse51578_main_settings_cb', 'wpse51578_option_page');

The 'my_option_field_name' int the register_settings is the name that the fields for your options should have: e.g.

        <input type="text" name="my_option_field name[A]"><br />
        <input type="text" name="my_option_field name[B]"><br />

The add_settings_section registers a settings section for your page. You can of course, have multiple sections on a page.

2. Output the form elements

The wpse51578_main_settings_cb callback function in add_settings_section is responsible for creating the output of that section. You can output the form elements in it (or user the api further with add_settings_field). No form tags should be used in this bit for this bit. For example:

function wpse51578_main_settings_cb(){
      $my_options = get_option('wpse51578_options_group',array());
      $A = ( isset($my_options['A']) ? esc_html($my_options['A']) : 'default-for-a';
      $B = ( isset($my_options['B']) ? esc_html($my_options['B']) : 'default-for-b';
       Location A <input type="text" value="<?php echo $A; ?>" name="my_option_field name[A]"><br />
       Location B <input type="text" value="<?php echo $B; ?>" name="my_option_field name[B]"><br />

3. Output your settings page

When you added you added your page, you defined a callback to output the page contents. This is what that function should do, (using the plugin_options_page if you've given it in the question:

function plugin_options_page() {
  <h2>My custom plugin</h2>
   Options relating to the Custom Plugin.

    <form action="options.php" method="post">
        <?php settings_fields('wpse51578_options_group'); ?>
        <?php do_settings_sections('wpse51578_option_page'); ?>

        <input name="Submit" type="submit" value="<?php esc_attr_e('Save Changes'); ?>" />


4. Validation

Now WordPress performs the nonce checks on your page, and handles (almost) all the processing of the forms. However - it needs you to do one thing - validate. When you register settings, you specify a validation callback function: wpse51578_validation_callback in this example.

This function gets passed an array of the values received from the form fields where the input name was my_option_field_name (as set also when you registered the settings). So for instance, you might expect the array:

     'A' => 'some-input', //value for A
     'B' => 'some-input-for b', //value for B

Its job is to check that array contains 'correct' values (what that means depends on context: e.g. if its an e-mail field, is the value actually an e-mail address?). No sanitation is required here (but would be where-ever you output the data).

function wpse51578_validation_callback( $dirty ){
     //$dirty is what you receive from the form

     $clean = array();
     //Sanitize data and pass it to the $clean array

     return $clean;

Other Resources:

The best tutorial on using the settings API is probably: http://ottodestruct.com/blog/2009/wordpress-settings-api-tutorial/

Please note that I've not tested the above, but it is correct in procedure, there just maybe typos or minor errors.

  • This does not address the initial question which is how to retrieve the form values from the settings page into a variable for a plugin. – Carey Estes Jul 17 '13 at 17:20
  • 1
    @CareyEstes See part 2, you use get_option() to get the options from the database to a variable. WordPress handles collecting values from form and saving them to database. You filter/validate that array as per step 4. – Stephen Harris Jul 17 '13 at 20:14
  • You are correct sir – Carey Estes Jul 18 '13 at 21:27

Use the init action to handle the posted form. See the codex for init for details and example.


 Plugin Name: calc plugin 

add_action('admin_menu', 'my_plugin_menu');

function my_plugin_menu() {
    add_options_page('My Plugin Options', 'My Plugin', 'administrator', 'your-unique-identifier', 'my_plugin_options');

function my_plugin_options() { ?>
    <div class="wrap">';
        <p>Enter Two locations </p>
        <form name="mapform" id="mapform" method="post">
            Location A: <input type="text" name="A"><br />
            Location B: <input type="text" name="B"><br />
            <input type="submit" name="my_submit" value="Update">
<?php }

add_action('init', 'my_plugin_handler');
function my_plugin_handler(){
        $val_a = $_POST['A'];
        $val_b = $_POST['B'];

        echo $val_a.', '.$val_b;
  • function process_post(){ if(isset($_POST['unique_hidden_field'])) { // process $_POST data here } } unique_hidden_field , dies it mean that field should must be hidden? – lucky May 9 '12 at 6:28
  • Check my code. Not necessarily a hidden field, could be any form field. – Rutwick Gangurde May 9 '12 at 6:32
  • yes the same thing i did. and it worked – lucky May 9 '12 at 6:48
  • new i want to take these two values and use them in google map to display two locations any idea ? – lucky May 9 '12 at 6:49
  • Then you may have to pass them to your javascript file. Read how to pass php variables to js files here: blog.rutwick.com/… – Rutwick Gangurde May 9 '12 at 7:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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