Home › Forums › Backend Issues (wp-admin) › acf/pre_save_post not working on Backend
Hi,
I have the following in my functions.php file that executes when my front end form is submitted. I need this to work in the admin panel but it doesn’t. Does anyone know why? Do I need a another hook for this to work?
The function basically unzips a file on the server and saves the details.
The form says that it has posted but when I look, its not there. The zip file is in the media library but has not unzipped.
I have WP_DEBUG set to TRUE in my config file but get no errors.
function pre_save_post( $post_id ) {
// stop function if not a new post
if( $post_id !== 'new_post' ) {
return $post_id;
}
// vars
$title = $_POST['fields']['field_576bb1c1cf241'];
$attachment_id = $_POST['fields']['field_576bb2dfcf247'];
$file_name = basename( get_attached_file( $attachment_id ) );
$file_name_without_zip = preg_replace('/.zip$/', '', $file_name);
$timestamp = time();
// Create a new post
$post = array(
'post_status' => 'publish',
'post_type' => 'files',
'post_title' => $title,
);
//unzip file
require_once(ABSPATH .'/wp-admin/includes/file.php');
WP_Filesystem();
$destination = wp_upload_dir();
$uploads_path = $destination['path'];
$destination_path = $destination['basedir'] . '/powerpoint/' . $file_name_without_zip . '-' . $timestamp;
$zip_file = $uploads_path .'/'. $file_name;
$unzipfile = unzip_file( $zip_file, $destination_path);
// insert the post
$post_id = wp_insert_post( $post );
//override link to media
update_attached_file( $attachment_id, $destination_path . '/index.html' );
// return the new ID
return $post_id;
}
add_filter('acf/pre_save_post' , 'pre_save_post' );
The pre_save_post action/filter is only used on called on front end forms. You need to use the acf/save_post filter when dealing with the admin https://www.advancedcustomfields.com/resources/acfsave_post/
Hi, thanks for your reply.
It now seems to post without running the function and therefore not unzipping the file. If I change the following:
//$post_id = wp_insert_post( $post );
echo 'TESTING';
add_filter('acf/save_post' , 'pre_save_post', 1 );
It still posts but doesn’t unzip the file and doesn’t display ‘TESTING’.
I have debug set true but have no errors.
That’s because the $post_id on the back end will never == ‘new_post’. You also will not need to update or insert a post.
What you need is 2 function/filters
On the back end filter/function you only need to deal with the .zip file.
On the front end filter/function, insert the new post and then call the function that deals with the .zip file.
Either that or have to separate functions that do all the work and remove the check at the top and inserting the post from the backend function.
You must be logged in to reply to this topic.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.