Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Have you ever tried http://www.wproots.com/ultimate-guide-to-meta-boxes-in-wordpress/ ? It's good classes to create metabox. But I'm having problem with the validation.

I tried the sanitize_callback parameter to sanitize URL, so I used esc_url but the field still accepted all values. What is the problem here?

Here's my code

add_smart_meta_box( 'themename_slides_url', array(

    'title'     => __( 'Slides Options', 'themename' ),
    'pages'     => array( 'slides' ),
    'context'   => 'normal',
    'priority'  => 'high',
    'fields'    => array(
            array(
            'name' => __( 'Slides URL', 'themename' ),
            'id' => 'slide_url',
            'default' => '',
            'desc' => __( 'Type the URL of the slide', 'themename' ),
            'type' => 'text',
            'sanitize_callback' => 'themename_sanitize_url'
            ),
        )
    )

)


function themename_sanitize_url( $new, $meta_key, $id ) {

    if( esc_url( $new ) )
        return $new;

}

Any idea ?

share|improve this question

1 Answer 1

esc_url() is not a validator. Use PHP’s filter functions for that.

Example:

// $url is now an URL or FALSE
$url = filter_var( 'http://example.com', FILTER_VALIDATE_URL );

if ( $url )
    # update post meta
else
    # delete post meta
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.