Save PostFREE

Docs Hooks & Helpers Save Post

#Save Post Hooks

ACF Extended is bundled with an advanced version of the famous acf/save_post hook. It allows developers to hook in any Post, User, Term and Options Pages save process, on the back-end and front-end.

#Global Save

It is possible to hook on all object save (Post, User, Term and Options Pages) using the global acfe/save. Usage example:

/*
 * ACFE Save
 * 
 * @string        $post_id  12 | term_46 | user_22 | my-options | comment_89
 * @object/array  $object   The corresponding WP Object (Post, Term, User, Options Page or Comment)
 */
 
action('acfe/save',               $post_id, $object);
action('acfe/save/id=12',         $post_id, $object);
action('acfe/save/id=term_46',    $post_id, $object);
action('acfe/save/id=user_22',    $post_id, $object);
action('acfe/save/id=my-options', $post_id, $object);
action('acfe/save/id=comment_89', $post_id, $object);
add_action('acfe/save', 'my_acfe_save', 10, 2);
function my_acfe_save($post_id, $object){

    // Retrieve the user input from "my_field" field
    get_field('my_field');

    // Retrieve "my_field" field value from DB
    get_field('my_field', $post_id);
    
    // Loop thru the user input from "my_repeater" field
    if(have_rows('my_repeater')):
        while(have_rows('my_repeater')): the_row();
             
             get_sub_field('my_sub_field');
             
        endwhile;
    endif;

}

#Global Pre-Save (before save to database)

Just like acf/save_post allows to hook in before the value is saved in the DB, ACF Extended allows to use acfe/pre_save hook, and still use functions like get_field() or have_rows() to interact with values in the $_POST['acf'] dataset. Usage example:

/*
 * ACFE Pre-Save
 * 
 * @string        $post_id  12 | term_46 | user_22 | my-options | comment_89
 * @object/array  $object   The corresponding WP Object (Post, Term, User, Options Page or Comment)
 */
 
action('acfe/pre_save',               $post_id, $object);
action('acfe/pre_save/id=12',         $post_id, $object);
action('acfe/pre_save/id=term_46',    $post_id, $object);
action('acfe/pre_save/id=user_22',    $post_id, $object);
action('acfe/pre_save/id=my-options', $post_id, $object);
action('acfe/pre_save/id=comment_89', $post_id, $object);

#Save Post

It is possible to target specifically post saves using the following syntax: acfe/save_post. This new hook also comes with a variation to target one specific post type.

Just like the Global Save hook, developers can use acfe/pre_save_post and all its variations to hook in before the values are saved in database. Usage example:

/*
 * ACFE Save Post Type
 * 
 * @string  $post_id  The Post ID
 * @object  $object   The WP Post Object
 */
 
action('acfe/save_post',                $post_id, $object);
action('acfe/save_post/id=12',          $post_id, $object);
action('acfe/save_post/post_type=page', $post_id, $object);
add_action('acfe/save_post/post_type=page', 'my_acfe_save_page', 10, 2);
function my_acfe_save_page($post_id, $object){
    
    // Retrieve the user input from "my_field" field
    get_field('my_field');

    // Retrieve "my_field" field value from DB
    get_field('my_field', $post_id);
    
    // Loop thru the user input from "my_repeater" field
    if(have_rows('my_repeater')):
        while(have_rows('my_repeater')): the_row();
             
             get_sub_field('my_sub_field');
             
        endwhile;
    endif;

}

#Save Term

It is possible to target specifically term saves using the following syntax: acfe/save_term. This new hook also comes with a variation to target one specific taxonomy.

Just like the Global Save hook, developers can use acfe/pre_save_term and all its variations to hook in before the values are saved in database. Usage example:

/*
 * ACFE Save Term
 * 
 * @string  $post_id  The ACF Term ID
 * @object  $object   The WP Term Object
 */
 
action('acfe/save_term',                   $post_id, $object);
action('acfe/save_term/id=term_46',        $post_id, $object);
action('acfe/save_term/taxonomy=category', $post_id, $object);
add_action('acfe/save_term/taxonomy=category', 'my_acfe_save_category', 10, 2);
function my_acfe_save_category($post_id, $object){
    
    // Retrieve the user input from "my_field" field
    get_field('my_field');

    // Retrieve "my_field" field value from DB
    get_field('my_field', $post_id);
    
    // Loop thru the user input from "my_repeater" field
    if(have_rows('my_repeater')):
        while(have_rows('my_repeater')): the_row();
             
             get_sub_field('my_sub_field');
             
        endwhile;
    endif;

}

#Save User

It is possible to target specifically user saves using the following syntax: acfe/save_user. This new hook also comes with a variation to target one specific role.

Just like the Global Save hook, developers can use acfe/pre_save_user and all its variations to hook in before the values are saved in database. Usage example:

/*
 * ACFE Save User
 * 
 * @string  $post_id  The ACF User ID
 * @object  $object   The WP User Object
 */
 
action('acfe/save_user',             $post_id, $object);
action('acfe/save_user/id=user_22',  $post_id, $object);
action('acfe/save_user/role=editor', $post_id, $object);
add_action('acfe/save_user/role=editor', 'my_acfe_save_editor', 10, 2);
function my_acfe_save_editor($post_id, $object){
    
    // Retrieve the user input from "my_field" field
    get_field('my_field');

    // Retrieve "my_field" field value from DB
    get_field('my_field', $post_id);
    
    // Loop thru the user input from "my_repeater" field
    if(have_rows('my_repeater')):
        while(have_rows('my_repeater')): the_row();
             
             get_sub_field('my_sub_field');
             
        endwhile;
    endif;

}

#Save Options Page

It is possible to target specifically options pages saves using the following syntax: acfe/save_option. This new hook also comes with a variation to target one specific options page slug.

Just like the Global Save hook, developers can use acfe/pre_save_option and all its variations to hook in before the values are saved in database. Usage example:

/*
 * ACFE Save Options Page
 * 
 * @string  $post_id  The ACF Options Page Post ID
 * @array   $object   The ACF Options Page Settings array
 */
 
action('acfe/save_option',                      $post_id, $object);
action('acfe/save_option/id=my-options',        $post_id, $object);
action('acfe/save_option/slug=my-options-page', $post_id, $object);
add_action('acfe/save_option/slug=my-options-page', 'my_acfe_save_options_page', 10, 2);
function my_acfe_save_options_page($post_id, $object){
    
    // Retrieve the user input from "my_field" field
    get_field('my_field');

    // Retrieve "my_field" field value from DB
    get_field('my_field', $post_id);
    
    // Loop thru the user input from "my_repeater" field
    if(have_rows('my_repeater')):
        while(have_rows('my_repeater')): the_row();
             
             get_sub_field('my_sub_field');
             
        endwhile;
    endif;

}

#Save Comment

It is possible to target specifically comments saves using the following syntax: acfe/save_comment.

Just like the Global Save hook, developers can use acfe/pre_save_comment and all its variations to hook in before the values are saved in database. Usage example:

/*
 * ACFE Save Comment
 * 
 * @string  $post_id  The ACF Comment ID
 * @object  $object   The WP Comment Object
 */
 
action('acfe/save_comment',               $post_id, $object);
action('acfe/save_comment/id=comment_89', $post_id, $object);
add_action('acfe/save_comment/id=comment_89', 'my_acfe_save_comment', 10, 2);
function my_acfe_save_comment($post_id, $object){
    
    // Retrieve the user input from "my_field" field
    get_field('my_field');

    // Retrieve "my_field" field value from DB
    get_field('my_field', $post_id);
    
    // Loop thru the user input from "my_repeater" field
    if(have_rows('my_repeater')):
        while(have_rows('my_repeater')): the_row();
             
             get_sub_field('my_sub_field');
             
        endwhile;
    endif;

}