Advertisement
Guest User

Untitled

a guest
Oct 1st, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. function tattoo_submit() {
  2.  
  3. if (isset($_POST["addtattoo"])) {
  4.  
  5. $title = "Tattoo : ". $_POST["tatooInput"];
  6.  
  7. $my_post = array(
  8. 'post_title' => $title,
  9. 'post_status' => 'publish',
  10. 'post_author' => 1,
  11. 'post_type' =>'tattoo'
  12. );
  13.  
  14. $upload_dir = wp_upload_dir();
  15.  
  16. // @new
  17. $upload_path = str_replace( '/', DIRECTORY_SEPARATOR, $upload_dir['path'] ) . DIRECTORY_SEPARATOR;
  18.  
  19. $img = $_POST['imageData'];
  20. $img = str_replace('data:image/png;base64,', '', $img);
  21. $img = str_replace(' ', '+', $img);
  22.  
  23. $decoded = base64_decode($img) ;
  24.  
  25. $filename = 'tattoo.png';
  26.  
  27. $hashed_filename = md5( $filename . microtime() ) . '_' . $filename;
  28.  
  29. // @new
  30. $image_upload = file_put_contents( $upload_path . $hashed_filename, $decoded );
  31.  
  32. //HANDLE UPLOADED FILE
  33. if( !function_exists( 'wp_handle_sideload' ) ) {
  34.  
  35. require_once( ABSPATH . 'wp-admin/includes/file.php' );
  36.  
  37. }
  38.  
  39. // Without that I'm getting a debug error!?
  40. if( !function_exists( 'wp_get_current_user' ) ) {
  41.  
  42. require_once( ABSPATH . 'wp-includes/pluggable.php' );
  43.  
  44. }
  45.  
  46. // @new
  47. $file = array();
  48. $file['error'] = '';
  49. $file['tmp_name'] = $upload_path . $hashed_filename;
  50. $file['name'] = $hashed_filename;
  51. $file['type'] = 'image/png';
  52. $file['size'] = filesize( $upload_path . $hashed_filename );
  53.  
  54. // upload file to server
  55. // @new use $file instead of $image_upload
  56. $file_return = wp_handle_sideload( $file, array( 'test_form' => false ) );
  57.  
  58. $filename = $file_return['file'];
  59. $attachment = array(
  60. 'post_mime_type' => $file_return['type'],
  61. 'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
  62. 'post_content' => '',
  63. 'post_status' => 'inherit',
  64. 'guid' => $wp_upload_dir['url'] . '/' . basename($filename)
  65. );
  66. $attach_id = wp_insert_attachment( $attachment, $filename, 289 );
  67. require_once(ABSPATH . 'wp-admin/includes/image.php');
  68. $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
  69. wp_update_attachment_metadata( $attach_id, $attach_data );
  70. $jsonReturn = array(
  71. 'Status' => 'Success'
  72. );
  73.  
  74. // Insert the post into the database
  75. $tattoo_ID = wp_insert_post( $my_post );
  76.  
  77. if ( $tattoo_ID ){
  78. add_post_meta($tattoo_ID, 'text', $_POST["tatooInput"] );
  79. add_post_meta($tattoo_ID, 'image', $_POST["imageData"] );
  80. add_post_meta($tattoo_ID, 'image_ID', $attach_id );
  81. exit( wp_redirect( get_permalink($tattoo_ID) ) );
  82.  
  83. }
  84.  
  85. }
  86.  
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement