Forked from tjhole/WORDPRESS: Base64 Image to Wordpress Uploads directory
Last active
September 13, 2019 10:50
-
-
Save cyberwani/2b776e7de081640ba29f23a20c2eb49a to your computer and use it in GitHub Desktop.
WORDPRESS: Base64 Image to Wordpress Uploads directory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function tattoo_submit() { | |
if ( isset( $_POST['addtattoo'] ) ) { | |
$title = 'tattoo'; | |
// Upload dir. | |
$upload_dir = wp_upload_dir(); | |
$upload_path = str_replace( '/', DIRECTORY_SEPARATOR, $upload_dir['path'] ) . DIRECTORY_SEPARATOR; | |
$img_type = 'jpeg'; | |
$img_ext = 'jpg'; | |
// @new | |
$base64_img = $_POST['imageData']; | |
$img = str_replace( 'data:image/'.$img_type.';base64,', '', $base64_img ); | |
$img = str_replace( ' ', '+', $img ); | |
$decoded = base64_decode( $img) ; | |
$filename = $title . $img_ext; | |
$hashed_filename = md5( $filename . microtime() ) . '_' . $filename; | |
// @new | |
$image_upload = file_put_contents( $upload_path . $hashed_filename, $decoded ); | |
// HANDLE UPLOADED FILE. | |
if ( ! function_exists( 'wp_handle_sideload' ) ) { | |
require_once ABSPATH . 'wp-admin/includes/file.php'; | |
} | |
// Without that I'm getting a debug error!? | |
if ( ! function_exists( 'wp_get_current_user' ) ) { | |
require_once ABSPATH . 'wp-includes/pluggable.php'; | |
} | |
// @new | |
$file = array(); | |
$file['error'] = ''; | |
$file['tmp_name'] = $upload_path . $hashed_filename; | |
$file['name'] = $hashed_filename; | |
$file['type'] = 'image/png'; | |
$file['size'] = filesize( $upload_path . $hashed_filename ); | |
// upload file to server | |
// @new use $file instead of $image_upload. | |
$file_return = wp_handle_sideload( | |
$file, | |
array( | |
'test_form' => false, | |
) | |
); | |
$filename = $file_return['file']; | |
$attachment = array( | |
'post_mime_type' => $file_return['type'], | |
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ), | |
'post_content' => '', | |
'post_status' => 'inherit', | |
'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ), | |
); | |
$attach_id = wp_insert_attachment( $attachment, $filename, 289 ); | |
require_once ABSPATH . 'wp-admin/includes/image.php'; | |
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename ); | |
wp_update_attachment_metadata( $attach_id, $attach_data ); | |
$jsonReturn = array( | |
'Status' => 'Success', | |
); | |
// Save Post. | |
$title = '"Tattoo : '. $_POST['tatooInput']; | |
$my_post = array( | |
'post_title' => $title, | |
'post_status' => 'publish', | |
'post_author' => 1, | |
'post_type' => 'tattoo', | |
); | |
// Insert the post into the database. | |
$tattoo_id = wp_insert_post( $my_post ); | |
if ( $tattoo_id ){ | |
add_post_meta( $tattoo_id, 'text', $_POST['tatooInput'] ); | |
add_post_meta( $tattoo_id, 'image', $_POST['imageData'] ); | |
add_post_meta( $tattoo_id, 'image_id', $attach_id ); | |
exit( wp_safe_redirect( get_permalink( $tattoo_id ) ) ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment