Advertisement
Guest User

Upload Form

a guest
Jul 31st, 2015
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.63 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  
  5. Template Name: Single File Upload
  6.  
  7. */
  8.  
  9. function UploadFile() {
  10.  
  11.     if ( ! ( is_user_logged_in() || current_user_can('publish_posts') ) ) {
  12.         return;
  13.     }
  14.     acf_form_head();
  15. }
  16. add_action( 'get_header', 'UploadFile', 1 );
  17.  
  18. /* ----------------------------------------------------------------------- */
  19. /*                                                                         */
  20. /* ----------------------------------------------------------------------- */
  21.  
  22. function UploadFileForm() {
  23.  
  24.     if ( ! ( is_user_logged_in()|| current_user_can('publish_posts') ) ) {
  25.  
  26.     }
  27.  
  28.     $new_post = array(
  29.         'post_id'            => 'new', // Create a new post
  30.         'field_groups'       => array(36), // Create post field group ID(s)
  31.         'form'               => true,
  32.         'return'             => '%post_url%', // Redirect to new post url
  33.         'html_before_fields' => '',
  34.         'html_after_fields'  => '',
  35.         'submit_value'       => 'Upload File',
  36.         'updated_message'    => 'Success!',
  37.         'uploader' => 'basic'
  38.     );
  39.     acf_form( $new_post );
  40. }
  41. add_action( 'entry_content', 'UploadFileForm' );
  42.  
  43. /* ----------------------------------------------------------------------- */
  44. /*                                                                         */
  45. /* ----------------------------------------------------------------------- */
  46.  
  47. function PreSaveForm( $post_id ) {
  48.  
  49.     // check if this is to be a new post
  50.     if( $post_id != 'new' ) {
  51.         return $post_id;
  52.     }
  53.  
  54.     // Create a new post
  55.     $post = array(
  56.         'post_type'     => 'file', // Your post type ( post, page, custom post type )
  57.         'post_status'   => 'publish', // (publish, draft, private, etc.)
  58.         'post_title'    => wp_strip_all_tags($_POST['acf']['field_55ba5b162db22']), // Post Title ACF field key
  59.     );
  60.  
  61.     // insert the post
  62.     $post_id = wp_insert_post( $post );
  63.     // Save the fields to the post
  64.     do_action( 'acf/save_post' , $post_id );
  65.  
  66.     return $post_id;
  67. }
  68. add_filter('acf/pre_save_post' , 'PreSaveForm' );
  69.  
  70. /* ----------------------------------------------------------------------- */
  71. /*                                                                         */
  72. /* ----------------------------------------------------------------------- */
  73.  
  74. function PostImagePreview( $post_id ) {
  75.  
  76.  
  77.     if( empty($_POST['acf']) ) {
  78.         return;
  79.     }
  80.  
  81.     $image = $_POST['acf']['field_55ba5c632db24'];
  82.  
  83.     if ( empty($image) ) {
  84.         return;
  85.     }
  86.  
  87.  
  88.     add_post_meta( $post_id, 'post-thumbnails', $image );
  89. }
  90. add_action( 'acf/save_post', 'PostImagePreview');
  91.  
  92.  
  93. get_header(); ?>
  94.  
  95.  
  96. <?php get_template_part('template/header/loggedin-header'); ?>
  97.         <main id="PKA-Content">
  98.  
  99.             <?php get_template_part('template/header/header-breadcrumbs') ?>
  100.  
  101.             <div class="Page-Upload">
  102.                 <div class="container">
  103.                     <div class="row">
  104.                         <div class="col-md-8">
  105.                         <?php if ( have_posts() ) : ?>
  106.                             <?php while ( have_posts() ) : the_post(); ?>
  107.  
  108.                                 <?php UploadFileForm(); ?>                              
  109.  
  110.                             <?php endwhile; endif ?>
  111.                         </div>
  112.  
  113.                         <aside class="col-md-4">
  114.                            
  115.                         </aside><!-- Sidebar -->
  116.                     </div>
  117.                 </div>
  118.             </div>
  119.  
  120.         </main><!-- PKA-Content End -->
  121.  
  122.     <?php get_template_part('template/footer/loggedin-footer'); ?>
  123.  
  124.  
  125. <?php get_footer(); ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement