Skip to content

Instantly share code, notes, and snippets.

@danielpataki
Last active June 1, 2020 03:26
Show Gist options
  • Save danielpataki/e55e8fe6a7757841116d to your computer and use it in GitHub Desktop.
Save danielpataki/e55e8fe6a7757841116d to your computer and use it in GitHub Desktop.
Uploading With WordPress
add_action( 'wp_ajax_nopriv_submit_content', 'my_submission_processor' );
add_action( 'wp_ajax_submit_content', 'my_submission_processor' );
function my_submission_processor() {
// Handle the form in here
}
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php
// Post thumbnail.
twentyfifteen_post_thumbnail();
?>
<header class="entry-header">
<?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
</header><!-- .entry-header -->
<div class="entry-content">
<?php the_content(); ?>
<?php
wp_link_pages( array(
'before' => '<div class="page-links"><span class="page-links-title">' . __( 'Pages:', 'twentyfifteen' ) . '</span>',
'after' => '</div>',
'link_before' => '<span>',
'link_after' => '</span>',
'pagelink' => '<span class="screen-reader-text">' . __( 'Page', 'twentyfifteen' ) . ' </span>%',
'separator' => '<span class="screen-reader-text">, </span>',
) );
?>
<!-- Form Goes Here -->
</div><!-- .entry-content -->
<?php edit_post_link( __( 'Edit', 'twentyfifteen' ), '<footer class="entry-footer"><span class="edit-link">', '</span></footer><!-- .entry-footer -->' ); ?>
</article><!-- #post-## -->
<form action="<?php echo admin_url( 'admin-ajax.php' ) ?>" method="post" enctype="multipart/form-data">
<?php wp_nonce_field( 'submit_content', 'my_nonce_field' ); ?>
<p>
<label><input type="text" name="post_title" placeholder="Enter a Title"></label>
</p>
<p>
<label><textarea rows='5' name="post_content" placeholder="Enter a description"></textarea></label>
</p>
<p>
<input type='file' name='image' accept='image/*'>
</p>
<p>
<input type='hidden' name='action' value='submit_content'>
<input type='submit' value='Submit Content'>
</p>
</form>
<?php
add_action( 'wp_enqueue_scripts', 'enqueue_parent_styles' );
function enqueue_parent_styles() {
wp_enqueue_style( 'parent-style', get_template_directory_uri().'/style.css' );
}
function my_submission_processor() {
$post_data = array(
'post_title' => $_POST['post_title'],
'post_content' => $_POST['post_content'],
'post_status' => 'draft'
);
$post_id = wp_insert_post( $post_data );
my_upload_function( $_FILE['image'], $post_id, true );
wp_redirect( site_url() . '/thank-you/' );
die();
}
function my_upload_function( $file, $post_id = 0 , $set_as_featured == false ) {
$upload = wp_upload_bits( $file['name'], null, file_get_contents( $file['tmp_name'] ) );
$wp_filetype = wp_check_filetype( basename( $upload['file'] ), null );
$wp_upload_dir = wp_upload_dir();
$attachment = array(
'guid' => $wp_upload_dir['baseurl'] . _wp_relative_upload_path( $upload['file'] ),
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', basename( $upload['file'] )),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $upload['file'], $post_id );
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attach_data = wp_generate_attachment_metadata( $attach_id, $upload['file'] );
wp_update_attachment_metadata( $attach_id, $attach_data );
if( $set_as_featured == true ) {
update_post_meta( $post_id, '_thumbnail_id', $attach_id );
}
}
/*
Theme Name: Twenty Fifteen Upload Test
Theme URI: http://danielpataki.com
Description: A test for upload functions
Author: Daniel Pataki
Author URI: http://danielpataki.com
Template: twentyfifteen
Version: 1.0.0
*/
function my_submission_processor() {
$post_data = array(
'post_title' => $_POST['post_title'],
'post_content' => $_POST['post_content'],
'post_status' => 'draft'
);
$post_id = wp_insert_post( $post_data );
wp_redirect( site_url() . '/thank-you/' );
die();
}
/**
* Template Name: Submission Page
*/
<?php
/**
* Template Name: Submission Page
*/
get_header(); ?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php
// Start the loop.
while ( have_posts() ) : the_post();
// Include the page content template.
get_template_part( 'content', 'submit-page' );
// End the loop.
endwhile;
?>
<!-- Our Form Will Go Here -->
</main><!-- .site-main -->
</div><!-- .content-area -->
<?php get_footer(); ?>
function my_submission_processor() {
$post_data = array(
'post_title' => $_POST['post_title'],
'post_content' => $_POST['post_content'],
'post_status' => 'draft'
);
$post_id = wp_insert_post( $post_data );
$upload = wp_upload_bits( $_FILES['image']['name'], null, file_get_contents( $_FILES['image']['tmp_name'] ) );
$wp_filetype = wp_check_filetype( basename( $upload['file'] ), null );
$wp_upload_dir = wp_upload_dir();
$attachment = array(
'guid' => $wp_upload_dir['baseurl'] . _wp_relative_upload_path( $upload['file'] ),
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', basename( $upload['file'] )),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $upload['file'], $post_id );
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attach_data = wp_generate_attachment_metadata( $attach_id, $upload['file'] );
wp_update_attachment_metadata( $attach_id, $attach_data );
update_post_meta( $post_id, '_thumbnail_id', $attach_id );
wp_redirect( site_url() . '/thank-you/' );
die();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment