Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Get my new setting working WordPress

  • SOLVED

I want to add a setting, but it's not saving in the database.

I need to be able to call it back in my theme

current code:

<?php
add_action('admin_init','vimeo_setup');

function vimeo_setup(){
add_settings_field('vimeo_id','Vimeo ID','display_vimeo','general');
}

function display_vimeo(){

echo '<input type="text" name="vimeo_id" id="vimeo_id" value="'.attribute_escape(get_option('vimeo_id')).'" class="regular-text code" /> size="30" style="width:85%" />';
echo '<p><small> Enter your Vimeo ID here.</small></p>';
}
?>

Answers (2)

2010-09-10

Denzel Chia answers:

Hi,

Do something like this,


<?php

add_action('admin_init', 'vimeo_sampleoptions_init' );
add_action('admin_menu', 'vimeo_sampleoptions_add_page');

// Init plugin options to white list our options
function vimeo_sampleoptions_init(){
register_setting( 'vimeo_sampleoptions_options', 'vimeo_sample', 'vimeo_sampleoptions_validate' );
}

// Add menu page
function vimeo_sampleoptions_add_page() {
add_options_page('Vimeo Sample Options', 'Sample Options', 'manage_options', 'vimeo_sampleoptions', 'vimeo_sampleoptions_do_page');
}

// Draw the menu page itself
function vimeo_sampleoptions_do_page() {
?>
<div class="wrap">
<h2>Vimeo Sample Options</h2>
<form method="post" action="options.php">
<?php settings_fields('vimeo_sampleoptions_options'); ?>
<?php $options = get_option('vimeo_sample'); ?>
<table class="form-table">
<tr valign="top"><th scope="row">Some text</th>
<td><input type="text" name="vimeo_sample[sometext]" value="<?php echo $options['sometext']; ?>" /></td>
</tr>
</table>
<p class="submit">
<input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
</p>
</form>
</div>
<?php
}

// Sanitize and validate input. Accepts an array, return a sanitized array.
function vimeo_sampleoptions_validate($input) {

// Say our option must be safe text with no HTML tags
$input['sometext'] = wp_filter_nohtml_kses($input['sometext']);

return $input;
}

?>


Thanks.


John Farrow comments:

this is over complicated. I simply need to know how to alter my own code to make it save, and how to call back the value in the theme.


Denzel Chia comments:

Try This,


<?php

add_action('admin_init','vimeo_setup');

function vimeo_setup(){
register_setting( 'plugin_options', 'plugin_options', 'plugin_options_validate' );
add_settings_field('vimeo_id','Vimeo ID','display_vimeo','general', $section = 'default');
}

function display_vimeo() {
$options = get_option('plugin_options');
echo "<input id='vimeo_id' name='plugin_options[text_string]' size='40' type='text' value='{$options['text_string']}' />";
}

?>


Thanks


Denzel Chia comments:

Sorry, got error,

Please try this

<?php

add_action('admin_init','vimeo_setup');



function vimeo_setup(){
register_setting( 'plugin_options', 'plugin_options' );
add_settings_field('vimeo_id','Vimeo ID','display_vimeo','general', $section = 'default');
}



function display_vimeo() {
$options = get_option('plugin_options');
echo "<input id='vimeo_id' name='plugin_options[text_string]' size='40' type='text' value='{$options['text_string']}' />";
}

?>


John Farrow comments:

$options is returning false.


John Farrow comments:

pretty sure it's returning false because it's not saving, and it's not saving because the update_option isn't hooked to the save.


Denzel Chia comments:

Hi,

I suggest you read this article yourself. Then rewrite your code accordingly.
This article is written by a WordPress employee.

http://ottodestruct.com/blog/2009/wordpress-settings-api-tutorial/

You need to create your own settings page for this to work. You cannot add into existing wordpress settings page.

Thanks.


John Farrow comments:

seems to me like if that is true, your first solution is still missing a save function


Denzel Chia comments:

Hi,

The settings api will auto save your settings into options table, once your setting is properly white-listed.

You need to register settings, then add settings, create your admin page, print your settings in it.

Why don't you copy and paste my first solution into your theme's functions.php and try it out?

Thanks.

2010-09-10

Tobias Nyholm answers:

To save and load some values from the database (wp_options) you need to know these lines:

To init the option field:

add_option("option_name","default_value");


To save option:

update_option("option_name","new_value");


To load option:
$value=get_option("option_name");


John Farrow comments:

how can I hook the update_option properly?


Tobias Nyholm comments:

I suppose you got a form of any kind to let a user or admin change the field in the database.
When posting that form, on the target page you get a request variable. Use that one with update_option.

This is a function that displays the form and handle the request.

function test(){

//hande post
if(isset($_POST['do']) && $_POST['do']=="update"){
update_option("option_name",$_POST['field_name']);
}

//write form
?>
<form action="" method="POST">
<input type="hidden" name="do" value="update" />
<input type="text" name="field_name" />
<input type="submit" value="Update" />
</form>

<?
}



John Farrow comments:

But I wish to hook this into the actual wordpress settings. So I need to know what hook gets fired when they save.


Tobias Nyholm comments:

No you don't. I think you have misunderstood something..
I suggest some reading in the codex. Start with this:

http://codex.wordpress.org/Options_API
http://codex.wordpress.org/Function_Reference/update_option


John Farrow comments:

I think I understand pretty well.

update_options needs to be run after the user has updated the field. As I have placed the field in the general settings page in the Admin area, I need a way to fire update_options when the user saves the page.

Wordpress uses hooks and filters to perform this task. I need to know what hook is fired when general settings are saves, in order to run the update_options method.


Tobias Nyholm comments:

You should not do that... as far as I know, there is no hook fired when the general options is saved. Just study the code. /wp-admin/options.php line 120


What you should do is to add a option page of your own. Write your own form and handle your own posts.

Wordpress is pretty simple if you do everything the "wordpress way".