defiant
defiant

Reputation: 3341

Saving radio button options to access them later in wordpress plugin

This is my code for the radio buttons. I would like them to be displayed in the settings page and save it for future use.

<form method="post" action="options.php">  
<?php 
    wp_nonce_field('update-options'); 
    $rsp_position = get_option('rsp_position');
    if(empty($rsp_position)){
        $rsp_position = "true";
    } else {
        $rsp_position = get_option('rsp_position');
    }
?>  
<p><strong>Ad Placing:</strong><br />

<input type="radio" id="1" name="rsp_position" <?php if($rsp_position == 'true') echo 'checked="checked"'; ?> value="true" />yes <br />
<input type="radio" id="2" name="rsp_position" <?php if($rsp_position == 'false') echo 'checked="checked"'; ?> value="flase" />no <br />

<input type="text" name="publisher-id" size="45" value="<?php echo get_option('publisher-id'); ?>" />  
</p>  
<p><input type="submit" name="Submit" value="Save" /></p>  
<input type="hidden" name="action" value="update" />  
<input type="hidden" name="page_options" value="publisher-id" /> 
<?php 
    settings_fields( 'save_position' ); 
    register_setting( 'save_position', 'rsp_position');
?>

Upvotes: 1

Views: 2606

Answers (1)

Mark
Mark

Reputation: 3035

You're going along the right lines, but your calls to settings_fields and register_setting aren't in the right place.

Read Otto's tutorial on this, it's a good introduction to the settings API.

Basically, add an admin_init action and in the callback register the setting and use add_settings_field to create the form items.

Then in your add_options_page callback you make the call to settings_fields to display the entries created with add_settings_field.

If you're still struggling having worked through Otto's tutorial, post back again.

EDIT: Following pastebin entry.

The failure to save is a result of your validation function not returning the expected value. The preg_match is not finding the right value and turning the string to '' (nothing) before saving - so it looks like it's not saving, when in fact it's saving nothing.

You could also check out the following functions which may be useful.

Upvotes: 1

Related Questions