Comments (4)
In the Customizer context, it's a text input that is tied to a JS object that eventually gets saved with PHP, but as with any Customizer control you associate the "control" to a Customizer "setting", and it's on the setting that you can specify a sanitize_callback()
.
If you take a look at https://github.com/BraadMartin/components/blob/master/customizer/customizer.php#L43 you'll see:
// Alpha Color Picker Test Setting
$wp_customize->add_setting(
'components_alpha_color_test_setting',
array(
'default' => '#0099CC',
'type' => 'theme_mod',
'capability' => 'edit_theme_options',
'transport' => 'postMessage'
)
);
// Alpha Color Picker Test Control
$wp_customize->add_control(
new Customize_Alpha_Color_Control(
$wp_customize,
'components_alpha_color_test_control',
array(
'label' => __( 'Alpha Color Picker Test', 'components' ),
'section' => 'components_test_section',
'settings' => 'components_alpha_color_test_setting',
'palette' => array(
'#3FADD7',
'rgba(0,0,0,0.5)',
'#666666',
'#F5f5f5',
'#333333',
'#404040',
'#2B4267'
)
)
)
);
So what we're doing here is defining a Customizer "setting" called components_alpha_color_test_setting
, then we're associating a "control" (the Alpha Color Picker control) with that specific setting. This happens in the settings
key on the array (third param passed to new Customize_Alpha_Color_Control()
).
So to add sanitization, you would specify a sanitize_callback
key on the second arg you pass when registering the setting (the array) like so:
$wp_customize->add_setting(
'components_alpha_color_test_setting',
array(
'default' => '#0099CC',
'type' => 'theme_mod',
'capability' => 'edit_theme_options',
'sanitize_callback' => 'some_sanitization_function', // <-- Change made here
'transport' => 'postMessage'
)
);
Then you would define function some_sanitization_function()
somewhere else in your code and that function would take the raw value of the setting when the user attempts to save it as an input and return the sanitized value as output. If you just want to check for a valid text input you could use sanitize_text_field
as your sanitize_callback
value, or if you want to ensure the input is a valid hex or RGBa color value you could write a function to handle that.
I hope this helps you. For more information on sanitizing settings in the Customizer check out https://developer.wordpress.org/themes/advanced-topics/customizer-api/#settings
from components.
Hello Naeemnur,
What would you like to sanitize? The repo you've opened this issue against contains several separate components. If you're referring to my Alpha Color Picker Customizer control, you can include a sanitize_callback
param on the config array you pass to $wp_customize->add_setting()
. See https://codex.wordpress.org/Class_Reference/WP_Customize_Manager/add_setting for more information on this.
If you are referring to the non-Customizer version of the Alpha Color Picker, the input there is just a regular text input, and it's up to you to save the value, so you're free to do any sanitizing you want before saving the value.
from components.
Hey Braad,
I was refering to the Alpha Color Picker Customizer control, so it's just a text field?
from components.
Thank you for the detailed response Braad, really appreciate it.
from components.
Related Issues (20)
- Preview not showing new color HOT 2
- How to include the transparency-grid.png? HOT 3
- k
- Multi-Color-Picker customizer control does not close HOT 1
- Error on Wordpress 4.7.2 - Uncaught ReferenceError: Color is not defined HOT 1
- Sanitize Callback HOT 3
- How to install? HOT 1
- Is this project still active? HOT 8
- Sanitize for disallowing HEX?
- Labels and notifications invisible fix HOT 3
- Not an issue HOT 1
- Licensing
- Label only Shows Within Customizer After Clicking on Input
- Standard wp-color-picker shown
- Sanitization, New controller on Wordpress Themes team!
- Alpha color picker customizer control not showing up how I expected
- Having trouble with "clear" button not triggering a refresh. HOT 2
- Color Picker not showing up HOT 4
- Implementation with wordpress 4.5.2 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from components.