Home › Forums › Backend Issues (wp-admin) › Triggering the Advanced Custom Fields (ACF) \'acf/save_post\' Action › Reply To: Triggering the Advanced Custom Fields (ACF) \'acf/save_post\' Action
You cannot do what you are attempting to do with only a server side action run when the post is saved.
In addition it is the WP save_post action that triggers the acf/save_post action. If you insert or update a post in any way by using wp_insert_post() or wp_update_post() this will trigger will cause ACF to update fields if any ACF fields are submitted. ACF looks at $_POST['acf']
and if it contains anything it save the content to the post ID that was just added or updated. ACF is triggered by the save_post
WP hook.
Sorry, I am at a loss as how I can explain this further.
Here is an example. Let’s say that I want to trigger a special message about the post being saved.
In my functions.php file I would add something like
add_action('acf/save_post', 'my_special_message_function', 20);
function my_special_message_function($post_id) {
// acf saved the post, set a flag in the DB
update_post_meta($post_id, 'my_special_message_flag', 1);
}
Then in my template that outputs the code where this message might appear I add
$flag = get_post_meta($post->ID, 'my_special_message_flag', true);
if ($flag == 1) {
// show the special message
echo 'The Post Was Saved';
// unset the flag so it is not triggered again
delete_post_meta($post->ID, 'my_special_message_flag');
}
The same would be true if you want to determine what the output will be during the save action. The only difference would be that you would have to temporarily store all of the content that you want to be shown, and this would include the entire <script>
tag if you wanted to add java script to the page.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.