I want to know if this possible also.
Could we get a response π
Thanks for your response. I made the changes as outlined above and it still doesn’t work. The post is added fine but when I update the post it still tries to run the code when I don’t want it to.
I added a new post and then editied the post and this produced the following in the error log:
[20-Oct-2016 09:47:25 UTC] POST ID:107
[20-Oct-2016 09:47:25 UTC] POST TYPE:files
[20-Oct-2016 09:47:50 UTC] POST ID:107
[20-Oct-2016 09:47:50 UTC] POST TYPE:files
[20-Oct-2016 09:47:51 UTC] PHP Warning: filesize(): stat failed for /home/webcouk/public_html/appv2/wp-content/uploads/powerpoint/index.html-1476956870/index.html in /home/webcouk/public_html/appv2/wp-content/plugins/advanced-custom-fields/core/fields/file.php on line 81
My code is now as below:
functions.php
//unzip powerpoint html files before posting form
function acf_save_post_type_filter( $post_id ) {
error_log('POST ID:' . $post_id);
error_log('POST TYPE:' . get_post_type($post_id));
if (!is_numeric($post_id) || get_post_type($post_id) != 'files') {
return;
}
// 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();
//if( $post_id == 'ppt_post') { //what should go here to ensure the below only runs on a new post not an update?
//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);
//override link to media
update_attached_file( $attachment_id, $destination_path . '/index.html' );
//}
// return the new ID
return $post_id;
}
add_filter('acf/save_post' , 'acf_save_post_type_filter', 1 );
add-files-form.php
<?php
/**
* Template Name: Add Files Form
* The template for displaying front-end submission form to add new files to wordpress media area.
* Uses Advanced Custom Fields plugin
* @package mobile-friendly
*/
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
acf_form_head();
get_header(); ?>
<div id="primary">
<div id="content" role="main">
<?php /* The loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php
if(isset($_GET['updated'])){
if($_GET['updated'] == 'true'){
echo '<p style="color:green;font-weight:bold">The file details have been submitted.</p>';
}
}
?>
<?php
/*acf_form(array(
'id' => 'Add files form',
'post_id' => 'ppt_post',
'field_groups' => array( 7 ),
'submit_value' => 'Add file',
));*/
acf_form(array(
'post_id' => 'new_post',
'new_post' => array(
'post_type' => 'files',
'post_status' => 'publish'
),
'submit_value' => 'Add file'
));
?>
<?php endwhile; ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_footer(); ?>
I am sorry I am still very confused by this. Can you help clarify what bit of code needs to go in which function? I only want to use this form in the admin not on the front end.
I simply need to unzip a file if its a new post being added. If its being updated, then I don’t want to unzip any file or do anything with it other than save the revised fields in the form. Therefore there shouldn’t be any code needed if the form is updated.
Its not complex at all so Ive no idea why two functions would be needed if I only want to do this in the admin panel.
I am so confused right now and feel like Im going round in circles. Please refer to this previous topic where you advised me to use acf/save_post
https://support.advancedcustomfields.com/forums/topic/acfpre_save_post-not-working-on-backend/
Many thanks
I have just realised we may be getting our wires crossed. I am using the acf/save_post filter. The name of my function is pre_save_post. Should have named my function something better, sorry!
My full code is below:
functions.php
//unzip powerpoint html files before posting form
function pre_save_post( $post_id ) {
error_log('POST ID:' . $post_id);
/*if( $post_id != 'ppt_post' ) { //this stops the code below from working
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();
if( $post_id == 'ppt_post') { //what should go here to ensure the below only runs on a new post not an update?
//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);
//override link to media
update_attached_file( $attachment_id, $destination_path . '/index.html' );
}
// return the new ID
return $post_id;
}
add_filter('acf/save_post' , 'pre_save_post', 1 );
add-files.form.php
<?php
/**
* Template Name: Add Files Form
* The template for displaying front-end submission form to add new files to wordpress media area.
* Uses Advanced Custom Fields plugin
* @package mobile-friendly
*/
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
acf_form_head();
get_header(); ?>
<div id="primary">
<div id="content" role="main">
<?php /* The loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php
if(isset($_GET['updated'])){
if($_GET['updated'] == 'true'){
echo '<p style="color:green;font-weight:bold">The file details have been submitted.</p>';
}
}
?>
<?php
acf_form(array(
'id' => 'Add files form',
'post_id' => 'ppt_post',
'field_groups' => array( 7 ),
'submit_value' => 'Add file',
));
?>
<?php endwhile; ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_footer(); ?>
Hope this clarifies it a bit better.
There is defo no other pre_save_post() function anywhere in my code.
Can I refer you to something you stated in another topic?,
“$post_id on the back end will never == βnew_postβ.”
Does this have anything to do with my issue? Just to clarify I am running this on the backend, not the front end.
Yes exactly, but as mentioned, $post_id is always an actual ID, never a string regardless of what I set in the acf_form() function, so thats why that line doesn’t work.
Ive printed $post_id to the error log and it is numeric, even when new.
OK, thanks for your help.
I have checked what I set for acf_form() and it was set to ‘new_post’. I amended my code accordingly and this didn’t work so I did what you suggested and created a unique string as set out below but this also doesn’t work….
acf_form(array(
'id' => 'Add files form',
'post_id' => 'ppt_post',
'field_groups' => array( 7 ),
'submit_value' => 'Add file',
));
So I amended my code to pre_save_post() code to read,
if( $post_id == 'ppt_post') {
It just ignores the code within the line above.
Thank you for your help, much appreciated π
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.
yes thats right
<?php
/**
* Template Name: Add Files Form
* The template for displaying front-end submission form to add new files to wordpress media area.
* Uses Advanced Custom Fields plugin
* @package mobile-friendly
*/
acf_form_head();
get_header(); ?>
<div id="primary">
<div id="content" role="main">
<?php /* The loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php
if(isset($_GET['updated'])){
if($_GET['updated'] == 'true'){
echo '<p style="color:green;font-weight:bold">The file details have been submitted.</p>';
}
}
?>
<?php
acf_form(array(
'id' => 'Add files form',
'post_id' => 'new_post',
'field_groups' => array( 7 ),
'submit_value' => 'Add file',
));
?>
<?php endwhile; ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_footer(); ?>
functions.php:
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;
}
Hope that helps.
Hi James,
I have started from scratch and followed the instructions from the link you sent above.
All seems to be working now, thanks for your help π
Many thanks for your help π
Thanks for your reply. I’m not sure how this relates to my problem of the form location settings. Can you please clarify?
I have the form fully working on the front end and submitting without any issues. My query is regarding the location as it works on the front end but also shows up on all other forms in the admin panel.
Many thanks
I realised I was reading the documentation for the wrong version! Its very confusing and should be made clearer. As far as I can see V5 isn’t even available yet so why does the documentation default to v5?
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.