Skip to content

Instantly share code, notes, and snippets.

@shaunpalmer
Last active September 15, 2024 14:41
Show Gist options
  • Save shaunpalmer/c11924d783559a2da9a40a9a44cba859 to your computer and use it in GitHub Desktop.
Save shaunpalmer/c11924d783559a2da9a40a9a44cba859 to your computer and use it in GitHub Desktop.
👸🏻: Let's break down your ideas based on the **"Register and add settings page"** file you shared and explore ways to incorporate licensing, custom post types, and toggles on the settings page.
### Key Points from the File:
1. **Settings Page Structure**:
- You have a clear framework to add settings, which includes registering options, adding fields, and sanitizing input.
- This is an excellent base for further expansion.
2. **What You Want**:
- **Licensing/Key Registration**: You want a licensing system where you can store and validate a key.
- **Custom Post Types**: A section to manage custom post types.
- **General Settings**: A first column for turning on/off features like Gutenberg, custom blocks, etc.
- **Toggle Switches**: Display options as toggle switches instead of traditional checkboxes for a more modern interface.
---
### Suggested Features and Enhancements:
#### 1. **Licensing and Key Registration**:
- **Add a Section for License Key**: We'll add a field where users can input their license key and save it in the options table.
- **Validate License**: You can create a function that pings an external API to validate the license key and store the status.
Here's how you could add that:
```php
public function page_init()
{
register_setting(
'my_option_group', // Option group
'my_option_name', // Option name
array( $this, 'sanitize' ) // Sanitize
);
add_settings_section(
'license_section_id', // ID
'License Key Settings', // Title
array( $this, 'print_license_section_info' ), // Callback
'my-setting-admin' // Page
);
add_settings_field(
'license_key',
'License Key',
array( $this, 'license_key_callback' ),
'my-setting-admin',
'license_section_id'
);
}
public function print_license_section_info() {
print 'Enter your license key below:';
}
public function license_key_callback() {
printf(
'<input type="text" id="license_key" name="my_option_name[license_key]" value="%s" />',
isset( $this->options['license_key'] ) ? esc_attr( $this->options['license_key']) : ''
);
}
```
You can then add validation logic by pinging a licensing server or API whenever the key is saved.
---
#### 2. **Custom Post Types Section**:
- **Dynamic Post Types**: Allow the admin to define new custom post types dynamically on the settings page.
- You can add a text field or dropdown to let users create custom post types without coding.
Example:
```php
add_settings_section(
'custom_post_types_section',
'Custom Post Types',
array( $this, 'custom_post_types_info' ),
'my-setting-admin'
);
add_settings_field(
'post_type_name',
'Custom Post Type Name',
array( $this, 'post_type_name_callback' ),
'my-setting-admin',
'custom_post_types_section'
);
```
In the callback function, you can offer an input field where users enter the name of the post type and validate it using custom logic. When a new post type is created, you can dynamically register it using `register_post_type`.
---
#### 3. **General Settings with Toggle Switches**:
- **Switch for Gutenberg/Other Features**: This section allows admins to enable/disable certain features like Gutenberg blocks.
- Use modern UI elements like **toggle switches** instead of checkboxes for the settings. You can achieve this with CSS or by using an existing library.
Add a field like this:
```php
add_settings_field(
'enable_gutenberg',
'Enable Gutenberg',
array( $this, 'gutenberg_toggle_callback' ),
'my-setting-admin',
'general_settings_section'
);
public function gutenberg_toggle_callback() {
$checked = isset( $this->options['enable_gutenberg'] ) ? 'checked' : '';
echo '<label class="switch">
<input type="checkbox" name="my_option_name[enable_gutenberg]" ' . $checked . '>
<span class="slider round"></span>
</label>';
}
```
You can style the checkbox as a toggle switch using CSS:
```css
.switch {
position: relative;
display: inline-block;
width: 60px;
height: 34px;
}
.switch input {
opacity: 0;
width: 0;
height: 0;
}
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
transition: .4s;
}
.slider.round {
border-radius: 34px;
}
.slider.round:before {
border-radius: 50%;
}
input:checked + .slider {
background-color: #2196F3;
}
input:checked + .slider:before {
transform: translateX(26px);
}
```
---
### 4. **Tabular Layout for Settings**:
- Divide the settings page into **tabs** for each section: General, Custom Post Types, and Licensing.
- You can implement tabs using jQuery UI or by creating simple CSS tabs to separate the settings neatly.
---
### Summary of Enhancements:
1. **License Key Management**: Add a section to register and validate license keys.
2. **Dynamic Custom Post Types**: Allow users to define custom post types from the settings.
3. **General Settings with Toggle Switches**: Add toggle switches for enabling/disabling features (e.g., Gutenberg blocks).
4. **Tabs for Settings**: Organize settings into tabs for better UX.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment