最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

functions - Two Types of settings in WP - The Doubt

matteradmin8PV0评论

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

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

Share Improve this question asked Oct 18, 2018 at 12:21 Richa SharmaRicha Sharma 497 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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 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.

Post a comment

comment list (0)

  1. No comments so far