Support

Account

Home Forums Backend Issues (wp-admin) how to detect if post is new or edited with pre_save_post()? Reply To: how to detect if post is new or edited with pre_save_post()?

  • 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.