TransWikia.com

custom meta box text field: how to limit to alpha or numeric only

WordPress Development Asked by user3257949 on November 9, 2021

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.com/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

One Answer

I 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.org/Function_Reference/sanitize_text_field
        ),
            array(
            'name' => __( 'Custom Post Class', 'cmb' ),
            'id'   => $prefix . 'wnd_postclass',
            'type' => 'text_medium',
        ),

Answered by user3257949 on November 9, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP