admin管理员组文章数量:1316422
I created a custom meta text field for adding a body class to a specific page. I want to limit the text to only alpha characters.
Also, I am using to create the meta box fields.
The field works, but I can enter any characters; eg, {),;. That won't work for a body class.
Thanks
I created a custom meta text field for adding a body class to a specific page. I want to limit the text to only alpha characters.
Also, I am using https://github/webdevstudios/Custom-Metaboxes-and-Fields-for-WordPress to create the meta box fields.
The field works, but I can enter any characters; eg, {),;. That won't work for a body class.
Thanks
Share Improve this question asked May 12, 2015 at 16:13 user3257949user3257949 212 bronze badges 3 |1 Answer
Reset to default 1I finally figured this out. The Custom Meta Boxes and Fields plugin has a sanitize setting where you can add your own sanitize function or use one of the default wordpress sanitize functions. In my case, Wordpress already has a sanitize function that does exactly what I wanted. See the code below, specifically the line "'sanitization_cb' => 'sanitize_html_class',"
array(
'name' => __( 'Custom Body Class', 'cmb' ),
'id' => $prefix . 'wnd_bodyclass',
'type' => 'text_medium',
'sanitization_cb' => 'sanitize_html_class', // custom sanitization callback. see this page for details: https://codex.wordpress/Function_Reference/sanitize_text_field
),
array(
'name' => __( 'Custom Post Class', 'cmb' ),
'id' => $prefix . 'wnd_postclass',
'type' => 'text_medium',
),
本文标签: custom meta box text field how to limit to alpha or numeric only
版权声明:本文标题:custom meta box text field: how to limit to alpha or numeric only 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741982847a2408500.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
ctype_alnum
php/manual/en/function.ctype-alnum.php. It will check for only alpha numeric; and returntrue
orfalse
. – josh Commented May 12, 2015 at 16:19ctype_alnum
function.if(ctype_alnum($data)===true) {echo 'success'} else {echo 'try again'}
– josh Commented May 14, 2015 at 18:13