1

I was trying to learn some basics of wordpress plugin development, and I stumbled upon two types of settings.

1

add_settings_field(
    'custom_title',
    esc_html__('Custom Title', 'myplugin'),
    'myplugin_callback_field_text',
    'myplugin', 
    'myplugin_section_login', 
    [ 'id' => 'custom_title', 'label' => esc_html__('Custom title attribute for the logo link', 'myplugin') ]
);

add_settings_field(
    'custom_style',
    esc_html__('Custom Style', 'myplugin'),
    'myplugin_callback_field_radio',
    'myplugin', 
    'myplugin_section_login', 
    [ 'id' => 'custom_style', 'label' => esc_html__('Custom CSS for the Login screen', 'myplugin') ]
);

add_settings_field(
    'custom_message',
    esc_html__('Custom Message', 'myplugin'),
    'myplugin_callback_field_textarea',
    'myplugin', 
    'myplugin_section_login', 
    [ 'id' => 'custom_message', 'label' => esc_html__('Custom text and/or markup', 'myplugin') ]
);

add_settings_field(
    'custom_footer',
    esc_html__('Custom Footer', 'myplugin'),
    'myplugin_callback_field_text',
    'myplugin', 
    'myplugin_section_admin', 
    [ 'id' => 'custom_footer', 'label' => esc_html__('Custom footer text', 'myplugin') ]
);

add_settings_field(
    'custom_toolbar',
    esc_html__('Custom Toolbar', 'myplugin'),
    'myplugin_callback_field_checkbox',
    'myplugin', 
    'myplugin_section_admin', 
    [ 'id' => 'custom_toolbar', 'label' => esc_html__('Remove new post and comment links from the Toolbar', 'myplugin') ]
);

2

And in some other plugin I find it like this:

function paytm_settings_list(){
    $settings = array(
        array(
            'display' => 'Merchant ID',
            'name'    => 'paytm_merchant_id',
            'value'   => '',
            'type'    => 'textbox',
            'hint'    => 'Merchant ID'
        ),
        array(
            'display' => 'Merchant Key',
            'name'    => 'paytm_merchant_key',
            'value'   => '',
            'type'    => 'textbox',
            'hint'    => 'Merchant key'
        ),
        array(
            'display' => 'Website',
            'name'    => 'paytm_website',
            'value'   => '',
            'type'    => 'textbox',
            'hint'    => 'Website'
        ),
        array(
            'display' => 'Industry Type ID',
            'name'    => 'paytm_industry_type_id', 
            'value'   => '',
            'type'    => 'textbox',
            'hint'    => 'Industry Type ID'
        ),
        array(
            'display' => 'Channel ID',
            'name'    => 'paytm_channel_id',
            'value'   => '',
            'type'    => 'textbox',
            'hint'    => 'Channel ID e.g. WEB/WAP'
        ),
        array(
            'display' => 'Mode',
            'name'    => 'paytm_mode',
            'value'   => 'TEST',
            'values'  => array('TEST'=>'TEST','LIVE'=>'LIVE'),
            'type'    => 'select',
            'hint'    => 'Change the mode of the payments'
        ),

I am slightly confused that which is the correct method? Or if anyone above is obsolete?

I am a new comer please bear with me. Thanks Richa sharma

0

The 2nd example isn't a different type of setting, it's just an array of data that the plugin will probably turn into settings somewhere else in the code. I can't tell from just this code exactly what type of settings it will use or how it does that.

The actual two types of settings are the Settings API, and the Customize API. Your first block of code is using the Settings API.

The Settings API should be used for back-end admin settings pages. The Customize API should be used for adding settings for things that are visible on the front-end.

For theme options specifically, the wordpress.org theme repository requires use of the Customize API, and not the Settings API.

Another thing to consider is that 3rd-party plugins might have their own APIs for adding settings to them. WooCommerce, for example, has its own way of adding settings to the WooCommerce settings pages (although under the hood it's just the Settings API). Your 2nd block of code might be intended for use in the WooCommerce API.

  • Actually I was thinking why the second one is not using add_settings_field( – Richa Sharma Oct 18 '18 at 16:34
  • And I explained why. It’s not using anything. All you posted is an array of data. The settings actually being added must happen somewhere else in the codebase, and probably does use add_settings_field(). – Jacob Peattie Oct 18 '18 at 16:43
  • I was studying this plugin, but couldn't find this any where: add_settings_field() – Richa Sharma Oct 18 '18 at 17:00
  • Then it’s not using the WordPress APIs. You would need to ask it’s deverloper how and why it’s doing it the way it’s doing it. Like I said, some plugins might do things their own way. This is almost always a bad idea. Especially if you’re just starting out. – Jacob Peattie Oct 18 '18 at 17:03
  • They are registering settings. Is it possible that add_settings_field() is not necessary? what is the connection between register settings and add_settings_field() – Richa Sharma Oct 19 '18 at 18:27

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.