PHP code example of wordpressvn / wp-settings
1. Go to this page and download the library: Download wordpressvn/wp-settings library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
wordpressvn / wp-settings example snippets
use WPVNTeam\WPSettings\WPSettings;
$settings = new WPSettings(__('My Plugin Settings'));
$tab = $settings->add_tab(__( 'General', 'textdomain'));
$section = $tab->add_section('MailChimp');
$section->add_option('text', [
'name' => 'mailchimp_api_key',
'label' => __('API Key', 'textdomain')
]);
$settings->make();
$settings = new WPSettings(__('My Plugin Settings'));
$settings->set_capability('manage_options');
$settings->set_option_name('my_plugin_options');
$settings->set_menu_icon('dashicons-admin-generic');
$settings->set_menu_position(5);
$settings->set_menu_parent_slug('options-general.php');
$settings->add_tab(__( 'General', 'textdomain'));
$tab->add_section('Section 1');
$tab->add_section('Section 1', ['as_link' => true]);
$section->add_option('text', [
'name' => 'option_1',
'label' => __('Option 1', 'textdomain')
]);
$section->add_option('textarea', [
'name' => 'option_1',
'label' => __('Option 1', 'textdomain'),
]);
$section->add_option('checkbox', [
'name' => 'option_1',
'label' => __('Option 1', 'textdomain')
]);
$section->add_option('select', [
'name' => 'option_1',
'label' => __( 'Option 1', 'textdomain' ),
'options' => [
'value_1' => 'Label 1',
'value_2' => 'Label 2'
]
]);
$section->add_option('select2', [
'name' => 'option_1',
'label' => __('Option 1', 'textdomain'),
'options' => [
'value_1' => 'Label 1',
'value_2' => 'Label 2'
]
] );
$section->add_option('wp-editor', [
'name' => 'option_1',
'label' => __('Option 1', 'textdomain')
] );
$section->add_option('code-editor', [
'name' => 'option_1',
'label' => __('Option 1', 'textdomain')
] );
$section->add_option('color', [
'name' => 'option_1',
'label' => __('Option 1', 'textdomain')
] );
$section->add_option('media', [
'name' => 'option_1',
'label' => __('Option 1', 'textdomain')
] );
$section->add_option('image', [
'name' => 'option_1',
'label' => __('Option 1', 'textdomain')
] );
$section->add_option('video', [
'name' => 'option_1',
'label' => __('Option 1', 'textdomain')
] );
$section->add_option('text', [
'name' => 'mailchimp_api_key',
'label' => __('API Key', 'textdomain'),
'validate' => [
[
'feedback' => __('Your API key is too short.', 'textdomain'),
'callback' => function($value) {
return strlen($value) > 35;
}
]
]
]);
$section->add_option('text', [
'name' => 'mailchimp_api_key',
'label' => __('API Key', 'textdomain'),
'santitize' => function($value) {
return sanitize_key($value);
}
]);
$tab = $settings->add_tab(__( 'General', 'textdomain'))
->option_level();
$section = $tab->add_section('Example', ['as_link' => true])
->option_level();
add_filter('wp_settings_option_type_map', function($options){
$options['custom'] = YourCustomOption::class;
return $options;
});
use WPVNTeam\WPSettings\Options\OptionAbstract;
class YourCustomOption extends OptionAbstract
{
public $view = 'custom-option';
public function render()
{
echo 'Your custom option HTML';
}
}
$settings->add_option('custom-option', [
'name' => 'your_option_name',
'label' => __('Your label')
]);