888888.888888.88""Yb..dP"Yb..8888b..Yb..dP.88b.88....db....8b....d8.88..dP""b8..dP"Y8
88__...88__...88__dP.dP...Yb..8I..Yb.YbdP..88Yb88...dPYb...88b..d88.88.dP...`".`Ybo."
88""...88""...88"Yb..Yb...dP..8I..dY..8P...88.Y88..dP__Yb..88YbdP88.88.Yb......o.`Y8b
88.....888888.88..Yb..YbodP..8888Y"..dP....88..Y8.dP""""Yb.88.YY.88.88..YboodP.8bodP'


88b.88.888888.888888.Yb........dP.dP"Yb..88""Yb.88..dP
88Yb88.88__.....88....Yb..db..dP.dP...Yb.88__dP.88odP.
88.Y88.88"".....88.....YbdPYbdP..Yb...dP.88"Yb..88"Yb.
88..Y8.888888...88......YP..YP....YbodP..88..Yb.88..Yb

November 28, 2008

I want to introduce you to a new function in 2.7 that might help to reduce the number of admin pages in your WordPress installation. The function allows plugin authors to extend the existing admin pages instead of creating new ones.

Here’s a quick example:

[php]
add_action('admin_init', 'add_my_settings_field');

function add_my_settings_field(){

// The fields are:
//the id the form field will use
//name to display on the page
//callback function
//the name of the page
//the section of the page to add the field to
add_settings_field('my_field_id' , 'My Field Caption' ,
'my_field_callback' , 'writing' , 'default');

//register the setting to make sure it gets checked
register_setting('writing','my_field_id');
}

function my_field_callback(){

//echo out the text field, drop down, or other type of field.
echo '
value="'.attribute_escape(get_option('my_field_id')).'" class="regular-text code" />';

}

In WordPress 2.7 admin pages are made of pages, sections and fields. In the above example I have added a new field to the writing page (Settings > Writing) in the default section.

This not only creates the table rows and table header for the form field, it also saves the value for you. The above code is everything you need to add the new question.

The value can be accessed again using get_option(‘my_field_id’).

You could just as easily add it to the remote_publishing section, by changing the last attribute in the add_settings_field function in the example to ‘remote_publishing’.

If you take a look at the options-writing.php page in wp-admin you will see that beneath each section there is a function, do_settings_fields, that lists the page and the section in its arguments; i.e. do_settings_fields(‘writing’, ‘remote_publishing’)

These tell you the settings you need to add fields in that part of the page.

I think this is a very important step in helping to reduce the clutter in WordPress, no longer will a new admin page be needed for a single setting if it could sit in an existing page.

Wpunlimited The Ultimate Wordpress Theme
Html5 wordpress?
3 comments
page 1141
Fun With Theme Widgets
Funy photo widget?
24 comments
page 867
Quick N Dirty Admin Login Screen
Login page css templates?
no comment
page 128
Wpunlimited The Ultimate Wordpress Theme
Html5 wordpress?
3 comments
page 1141
Quick N Dirty Category Redirection
Wordpress redirect category?
no comment
page 133
Post Image The Easy Peasy Way
Wordpress attach image to multiple posts?
26 comments
page 1065
Using Your Own Url Shortener
Wordpress clear rewrite rules?
4 comments
page 1190
Premium Ithemes Review Photo Gallery
Ithemes review?
4 comments
page 226
Updating Code Snippets Here
Wordpress fun plugins?
one comment
page 1338
Updating Code Snippets Here
Wordpress fun?
one comment
page 1338
Theming Habari Vs Wordpress
How to design a habari theme?
13 comments
page 440
Why I Ditched Disqus
Disqus email?
5 comments
page 1175
Html 5 Gallery
Themeatic html5?
6 comments
page 1305
Post Image The Easy Peasy Way
Wordpress first image?
26 comments
page 1065
Quick N Dirty Admin Login Screen
login screen using css?
no comment
page 128
Html 5 Gallery
Picture gallery html5?
6 comments
page 1305
Post Image The Easy Peasy Way
Wordpress first image gallery?
26 comments
page 1065
Post Image The Easy Peasy Way
Resizing images in thecontent?
26 comments
page 1065
Html 5 Gallery
Html 5 photo gallery?
6 comments
page 1305
Post Image The Easy Peasy Way
Inserting an image url in comments?
26 comments
page 1065
Post Image The Easy Peasy Way
Wp attachment functions?
26 comments
page 1065
Dont Mess With My Toot Toot
Wordpress custom content?
16 comments
page 599
Using Your Own Url Shortener
String shortner?
4 comments
page 1190
Quick N Dirty Admin Login Screen
My admin login page?
no comment
page 128
Using Your Own Url Shortener
How to build a shortner link?
4 comments
page 1190
Post Image The Easy Peasy Way
Get post attacments?
26 comments
page 1065
Theming Habari Vs Wordpress
Habari vs wordpress?
13 comments
page 440
Are Child Themes The Best Option
Wp child themes?
15 comments
page 1262
Upload From Url
Upload to url?
6 comments
page 326
Silence Is Golden
Silence is golden wordpress?
3 comments
page 213
How To Add Sidebars To A Theme
Html hot add a side bar?
10 comments
page 1053
How To Add Sidebars To A Theme
How to add picture to the wordpress sidebar on sidebarphp?
10 comments
page 1053
Fun With Sidebar Tabs Styling
Position of the tabs thematic?
2 comments
page 336
  1 query every 1486 seconds, updated 1 seconds ago.
Friday, 11am
 __
(__)
   `

 ala_747

Thank you very much for your answer :)

I've tried to do this but $this->adb value is always returning 'd' (the last item of my $as array).

I think I'll need to do it the old way (a new admin page) :(

Again, thanks for your time!

Friday, 10am
 __
(__)
   `

 Andrew Rickmann

What I usually do is to make sure the plugin and all its functions are contained within a class and then assigned the values to the class so the callback function can access them by using $this->myVar i.e.

class myPlugin
{

private $myVar = 10;

function myCallBack(){

$newVar = $this->myVar;

}

}

Friday, 8am
 __
(__)
   `

 ala_747

That's awesome! Thank you for sharing!!

But I'm trying to figure out how to pass an argument to the callback function. Actually, I don't know if it could be done :(

Eg.


$as = array('A', 'B', 'C', 'D');
$adb = null;

function my_new_settings(){
global $as;
foreach($as as $a):

$adb = sanitize_title($a);

register_setting('media',"{$adb}_1");
register_setting('media',"{$adb}_2");

add_settings_field("{$adb}" , "{$a} setting:" ,
'my_new_callback' , 'media' , 'default');

endforeach;
}

function my_new_callback(){
global $adb;

echo "<label for="{$adb}_1">Setting 1</label>
<input name="{$adb}_1" id="{$adb}_1" value="".attribute_escape(get_option("{$adb}_1"))."" class="small-text" type="text">
<label for="{$adb}_2">Setting 2</label>
<input name="{$adb}_2" id="{$adb}_2" value="".attribute_escape(get_option("{$adb}_2"))."" class="small-text" type="text">
";
}

add_action('admin_init', 'my_new_settings');

Maybe I'm missing something? Thanks in advance!