Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

How to Add Settings Fields in WordPress Setting API?

I thought that Settings API is one of the complicated Topic in WordPress because of its complexity.. 

Each and everything is fine but i don't like the way that we add setting fields.

typically we do add the fields with following functions.

add_settings_field( 'id', 'Name of Field', 'callback-function', 'page', 'setting-section', $args  );

we we have to create a call back function for each field like this.

function callback-function() {

    // Something here.

}

the above code is fine if we have only few setting fields but if we need to deal with a lot of fields? because we have to add the setting field and callback function for every field and its time consuming job.

So all i want is that is there another good method for adding setting fields?

we just can forget the setting fields by not adding them and going straight in the form section but i want the process to be secure as possible. also i have tried adding settings fields and callback functions through loops but didn't work for me.

user-image
Question added by Abdul Rehman Anwer , Developer , Creative Advertising
Date Posted: 2016/01/12
nermin abdelmonem ebrahim hassan
by nermin abdelmonem ebrahim hassan , متدرب , Information Technology Institute - 9 month Diploma

You can add new settings fields (basically, an option in the wp_options database table but totally managed for you) to the existing WordPress pages using this function. Your callback function just needs to output the appropriate HTML input and fill it with the old value, the saving will be done behind the scenes. You can create your own sections on existing pages using add_settings_section()

Muhammad Umar Khalid
by Muhammad Umar Khalid , Web Developer , Vistabit

First we have to create a menu item on the admin panel that will access our theme settings page.

We can create a menu item using the WordPress Menu API. Here’s the code to create a menu item.

function theme_settings_page(){} function add_theme_menu_item() { add_menu_page("Theme Panel", "Theme Panel", "manage_options", "theme-panel", "theme_settings_page", null, ); } add_action("admin_menu", "add_theme_menu_item");

هاشم المشارقة
by هاشم المشارقة , Key Account Manager , Advanced United Systems Ltd. ( A member of Taj Holding Group)

To check the best answer, I suggest to return to the origin source of Wordpress in the page linked below:

https://codex.wordpress.org/Settings_API#Adding_Setting_Fields

add_settings_field( $id, $title, $callback, $page, $section = 'default', $args = array() )

Abdul Rehman Anwer
by Abdul Rehman Anwer , Developer , Creative Advertising

Thank you for taking time to answer my question and i appreciate it...  @nermin abdelmonem ebrahim hassan and @Muhammad Umar Khalid ...

but i think that is not what i am looking for. maybe my question is not clear on my topic and its confusing you guys..

i know the registration & every other necessary things for administration panels also i can deal with option tables. but i was looking for a way to simplify the process of add_settings_field() only.. i don't like repeating this code again & again for creating new fields. i just want to make this process quite simple while caring for sanitation. 

Here is a rough scenario of where i want some help.

Lets suppose we have input fields < Intup type="text" .....................>.

so we have to write a lines for adding the setting field like this

add_settings_field(...............);

add_settings_field(...............);

add_settings_field(...............);

so on until.

and also different callback functions for these inputs.

i am looking to simplify it .. means is there a way we can get that settings fields registered with writing less codes. maybe i am looking to put them into a loop. possibly foreach() or while() loop.

i am trying it with loops and its working but the problem is that each html input fields are printing twice on admin page.

 

More Questions Like This

Do you need help in adding the right keywords to your CV? Let our CV writing experts help you.