Support

Account

Home Forums Front-end Issues Displaying form on front end, location rules issues Reply To: Displaying form on front end, location rules issues

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