0

After much unsuccessful searching, I'm hoping someone here can help me out. I am using Advanced Customs Fields (I assume most people are familiar) to create a post from the front end. I want to validate a field, and make sure there is a value before I save anything to the database.

I have this function, but the two meta values I'm checking always return false (possibly because the meta values haven't been saved yet?) and I can't figure out how to check them before saving the post. Any tips appreciated. Thanks.

//Create new Custom Design post from frontend
function my_pre_save_post( $post_id )
{
// check if this is to be a new post
if( $post_id != 'new' )
{
    return $post_id;
}
if (get_post_meta($post_id, 'upload_image', true) || get_post_meta($post_id, 'submit_link', true)) {

    // Create a new post
    global $current_user;
      get_currentuserinfo();

      $title = $current_user->user_email.'-'.get_date();

    $post = array(
        'post_status'  => 'publish' ,
        'post_title'  => $title,
        'post_type'  => 'custom-designs' ,
    );  

    // insert the post
    $post_id = wp_insert_post( $post ); 

    // update $_POST['return']
    $_POST['return'] = add_query_arg( array('post_id' => $post_id, 'submitted' => 'true', 'custom-error' => false), $_POST['return'] );    

    // return the new ID
    return $post_id;
} else {
    wp_redirect(add_query_arg(array('custom-error' => 'true'), $_POST['return'])); 
    exit;
}
} 
add_filter('acf/pre_save_post' , 'my_pre_save_post' );
1

In my experience, the only way to check the values of the fields is by directly accessing the $_POST vars:

if( ( isset( $_POST['upload_image'] ) && !empty( $_POST['upload_image'] ) )
  • Thanks for help. I tried that, but unfortunately ACF does not use the meta_key as the $_POST field, it uses a sort of random key and then saves it to the meta_value when saving the post. But using the $_POST vars is probably on the right track so I'm going to play around with it and see what I can get to work. – Eckstein Oct 13 '13 at 7:59
1

A couple years later and ACF has integrated this functionality into the plugin itself, so no need for this custom code in the first place. :)

0

I wrote a plugin to do validation for Advanced Custom Fields. It lets you do server side validation using either PHP code or regex, jQuery masked inputs, as well as unique value settings. Check the Support page for the beta version that support ACF5.

http://wordpress.org/extend/plugins/validated-field-for-acf/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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