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()?

  • 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(); ?>