Support

Account

Home Forums Feature Requests Get post_id in validate_save_post

Solving

Get post_id in validate_save_post

  • It would be nice to have direct access to the post_id in the validate_save_post action, similar to how the save_post action works. I already call the save_post action from outside the loop, and I’d like to do the same with validate_save_post, needing to use that one because it runs before the fields are saved.

    For bonus points, I’d also really like post_id to be available in the validate_value filter. Calling something like get_field('location') is not possible in that filter from what I’ve seen… but I could get the value if I had the post ID. This is useful for comparing a new value to a previous value.

  • David – I figured out a workaround while using the validate_value filter, which is what is called on each field by validate_save_post so it should work there as well.

    I wrote up how to make the post_ID available in validate_value on my site. Inserting a acf[post_ID] value should work for you too, or if you want to give it a shot I finished up a beta version of my Validated Field plugin which gives you a UI to write validation rules as well as check for unique values, set input masks, etc. I just finished it a couple days ago as it required a major update for ACF 5.0, but it is working pretty well although I have already found a few small issues I plan to address shortly.

  • That’s a creative solution, albeit somewhat hacky. Unfortunately, my community site uses only ACF frontend forms to manage user-created posts, and edit_form_after_editor doesn’t get called on the frontend.

    I’m also not sure I’m in love with the idea of adding an extra input field to every editor page across WP, including media, forums, and even ACF field groups, regardless of whether ACF fields are on that post or not. It would be nice to see some functionality built into the core of the plugin.

    But since I am calling a frontend form, maybe there’s a way to add an extra input to the template itself, or possibly a parameter on the acf_form function. I haven’t played around with it yet but it’s worth a shot.

    P.S. @jsilver I couldn’t submit a comment to your site because of a recaptcha error.

  • David,

    For the acf/validate_save_post action this can easily be done, without “hacking”, using the built in edit_form_after_title action and add a hidden input field with the acf[post_id] name.

    The AJAX post on the post edit screen will serialize all acf named inputs, so in this way, you can easily use the post_id in one of your own acf/validate_save_post hooks. 🙂

    
    // acf/validate_save_post
    add_action( 'edit_form_after_title', 'devplus_edit_form_after_title' );
    function devplus_edit_form_after_title(){
    	global $post;
    	if( $post && isset( $post->ID ) ){
    ?>
    	<input type="hidden" name="acf[post_id]" value="<?php echo $post->ID; ?>" />
    <?php
    	}
    }
    

    Happy ACF coding!

    // T

  • Timo,

    That’s the solution I described, however it doesn’t work on the front-end form, only in the WP Admin. The “hacky” workaround is for inserting the post ID into the front-end form that is generated from acf_form().

    Justin

  • @jsilver,

    Excuse me mate, I didn’t really completely read your response or go to your article on the external site! My bad!

    Anyhow, my post describes what is needed in the admin, even without using an obsolete plugin for it. Should be interesting for devs just needing this in the admin.

  • I dont think my plugin is obsolete 🙂

    Its primary function is to handle validation, but it also does the exact same thing as your code internally, and also includes the workaround for the front-end forms. Definitely something you can do in your functions.php or elsewhere; I was responding to this post as this code in the Validated Field plugin to enable different advanced functionality.

Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘Get post_id in validate_save_post’ is closed to new replies.