PHP code example of ayecode / wp-ayecode-settings-framework
1. Go to this page and download the library: Download ayecode/wp-ayecode-settings-framework 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/ */
ayecode / wp-ayecode-settings-framework example snippets
$config = array(
'sections' => array(
array(
'id' => 'general',
'name' => 'General Settings',
'icon' => 'fa-solid fa-gear',
'fields' => array(
array(
'id' => 'site_title',
'type' => 'text',
'label' => 'Site Title',
'description' => 'Enter your site title',
'default' => 'My Awesome Site'
),
array(
'id' => 'enable_feature',
'type' => 'toggle',
'label' => 'Enable Cool Feature',
'description' => 'Turn on the cool feature',
'default' => true
)
)
)
)
);
use AyeCode\SettingsFramework\Settings_Framework;
// Initialize the framework
new Settings_Framework($config, 'my_plugin_settings', array(
'plugin_name' => 'My Awesome Plugin',
'menu_title' => 'Plugin Settings',
'page_title' => 'My Plugin Settings'
));
// Get all settings
$settings = get_option('my_plugin_settings', array());
// Get specific setting
$site_title = $settings['site_title'] ?? 'Default Title';
array(
'id' => 'username',
'type' => 'text',
'label' => 'Username',
'placeholder' => 'Enter username...',
'
array(
'id' => 'enable_notifications',
'type' => 'toggle',
'label' => 'Enable Notifications',
'description' => 'Turn on email notifications',
'default' => false
)
array(
'id' => 'theme_style',
'type' => 'select',
'label' => 'Theme Style',
'options' => array(
'light' => 'Light Theme',
'dark' => 'Dark Theme',
'auto' => 'Auto (System)'
),
'default' => 'light'
)
array(
'id' => 'brand_color',
'type' => 'color',
'label' => 'Brand Color',
'default' => '#0073aa'
)
array(
'id' => 'max_items',
'type' => 'number',
'label' => 'Maximum Items',
'min' => 1,
'max' => 100,
'step' => 1,
'default' => 10
)
array(
'id' => 'advanced',
'name' => 'Advanced Settings',
'icon' => 'fa-solid fa-tools',
'subsections' => array(
array(
'id' => 'performance',
'name' => 'Performance',
'icon' => 'fa-solid fa-tachometer-alt',
'fields' => array(
// Performance fields here
)
),
array(
'id' => 'security',
'name' => 'Security',
'icon' => 'fa-solid fa-shield-alt',
'fields' => array(
// Security fields here
)
)
)
)
array(
'id' => 'google_maps_api_key',
'type' => 'password',
'label' => 'Google Maps API Key',
'searchable' => array('google', 'maps', 'api', 'key', 'geolocation')
)
array(
'id' => 'email_address',
'type' => 'email',
'label' => 'Email Address',
'
array(
'id' => 'custom_field',
'type' => 'text',
'label' => 'Custom Field',
'validate_callback' => 'my_custom_validator'
)
function my_custom_validator($value, $field) {
if (strlen($value) < 5) {
return new WP_Error('too_short', 'Value must be at least 5 characters');
}
return true;
}
// In your addon — replace 'my_page_slug' with the consuming plugin's $page_slug value
add_filter( 'ayecode_settings_framework_sections_my_page_slug', function( $sections, $option_name, $page_slug ) {
$sections[] = array(
'id' => 'my_addon',
'name' => 'My Addon Settings',
'icon' => 'fa-solid fa-puzzle-piece',
'fields' => array(
// Addon fields here
),
);
return $sections;
}, 10, 3 );
// After settings are saved
add_action('ayecode_settings_framework_saved', function($settings, $option_name) {
// Clear caches, trigger updates, etc.
}, 10, 2);
// After settings are reset
add_action('ayecode_settings_framework_reset', function($option_name) {
// Handle reset logic
});
// Inject or modify sections for a specific settings page (replace 'my_page_slug' with the target plugin's $page_slug)
add_filter( 'ayecode_settings_framework_sections_my_page_slug', function( $sections, $option_name, $page_slug ) {
// Modify or add sections
return $sections;
}, 10, 3 );