PHP code example of whaze / path-of-settings
1. Go to this page and download the library: Download whaze/path-of-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/ */
whaze / path-of-settings example snippets
// 1. Include Composer autoloader
ine!)
\PathOfSettings\PathOfSettings::getInstance()->init();
// 3. Register your settings
add_action('pos_register_pages', function() {
// Create a settings page
pos_register_page('my-settings', [
'title' => __('My Settings', 'textdomain'),
'menu_title' => __('Settings', 'textdomain'),
]);
// Add fields
pos_add_field('my-settings', 'text', 'api_key', [
'label' => __('API Key', 'textdomain'),
'description' => __('Enter your API key', 'textdomain'),
'
/**
* Plugin Name: My Awesome Plugin
*/
// Include Composer autoloader
thOfSettings\PathOfSettings::getInstance()->init();
});
// Register plugin settings
add_action('pos_register_pages', function() {
pos_register_page('my-plugin-settings', [
'title' => __('My Plugin Settings', 'my-plugin'),
'capability' => 'manage_options',
]);
pos_add_field('my-plugin-settings', 'text', 'api_key', [
'label' => __('API Key', 'my-plugin'),
'
// In your theme's functions.php
gs\PathOfSettings::getInstance()->init();
// Register theme options
add_action('pos_register_pages', function() {
pos_register_page('theme-options', [
'title' => __('Theme Options', 'my-theme'),
'capability' => 'edit_theme_options',
]);
pos_add_field('theme-options', 'text', 'primary_color', [
'label' => __('Primary Color', 'my-theme'),
'placeholder' => '#007cba',
]);
pos_add_field('theme-options', 'textarea', 'custom_css', [
'label' => __('Custom CSS', 'my-theme'),
'rows' => 8,
]);
});
// Use in your theme
function get_theme_primary_color() {
return pos_get_setting('theme-options', 'primary_color', '#007cba');
}
pos_register_page('my-settings', [
'title' => 'My Settings Page',
'menu_title' => 'Settings',
'capability' => 'manage_options',
]);
$api_key = pos_get_setting('my-settings', 'api_key', '');
$is_enabled = pos_get_setting('my-settings', 'enable_feature', false);
pos_add_field('page-id', 'text', 'field-id', [
'label' => 'Text Input',
'description' => 'Enter some text',
'placeholder' => 'Type here...',
'
pos_add_field('page-id', 'textarea', 'field-id', [
'label' => 'Long Text',
'description' => 'Enter longer content',
'rows' => 5,
'placeholder' => 'Enter your content here...',
]);
pos_add_field('page-id', 'select', 'field-id', [
'label' => 'Choose Option',
'description' => 'Select an option from the dropdown',
'options' => [
'option1' => 'Option 1',
'option2' => 'Option 2',
'option3' => 'Option 3',
],
'default' => 'option1',
]);
pos_add_field('page-id', 'checkbox', 'field-id', [
'label' => 'Enable Feature',
'description' => 'Check to enable this feature',
'default' => false,
]);
use PathOfSettings\Core\Contracts\FieldInterface;
class CustomField implements FieldInterface {
// Implement // ... other methods
}
// Register the field type
add_action('init', function() {
$registry = \PathOfSettings\Core\Registries\FieldsRegistry::getInstance();
$registry->register('custom', CustomField::class);
});
\PathOfSettings\PathOfSettings::getInstance()->init([
'version' => '2.1.0', // Custom version
'path' => plugin_dir_path(__FILE__), // For custom textdomain loading
'url' => plugin_dir_url(__FILE__),
'file' => __FILE__,
]);
add_filter('pos_before_save_settings', function($settings, $pageId) {
// Modify settings before saving
if ($pageId === 'my-settings') {
$settings['api_key'] = strtoupper($settings['api_key']);
}
return $settings;
}, 10, 2);
add_action('pos_after_save_settings', function($settings, $pageId) {
// Do something after settings are saved
if ($pageId === 'my-settings') {
// Clear cache, send notification, etc.
}
}, 10, 2);
/**
* Plugin Name: Simple Contact Form
*/
loaded', function() {
\PathOfSettings\PathOfSettings::getInstance()->init();
});
// Register contact form settings
add_action('pos_register_pages', function() {
pos_register_page('contact-form-settings', [
'title' => __('Contact Form Settings', 'simple-contact-form'),
'menu_title' => __('Contact Form', 'simple-contact-form'),
]);
// Email settings
pos_add_field('contact-form-settings', 'text', 'recipient_email', [
'label' => __('Recipient Email', 'simple-contact-form'),
'description' => __('Email address to receive form submissions', 'simple-contact-form'),
'Form Style', 'simple-contact-form'),
'options' => [
'default' => __('Default', 'simple-contact-form'),
'modern' => __('Modern', 'simple-contact-form'),
'minimal' => __('Minimal', 'simple-contact-form'),
],
'default' => 'default',
]);
});
// Use settings in your plugin
function scf_get_recipient_email() {
return pos_get_setting('contact-form-settings', 'recipient_email', get_admin_email());
}
function scf_is_phone_