Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. <fieldset>
  2. <label for="postImage">Fetaured Image</label>
  3. <input type="file" name="featured" id="featured" size="50">
  4. <img name="frontend-image" id="frontend-image" width="150" height="150">
  5. </fieldset>
  6.  
  7. (function($) {
  8.  
  9. $(document).ready( function() {
  10. var file_frame; // variable for the wp.media file_frame
  11.  
  12. // attach a click event (or whatever you want) to some element on your page
  13. $( '#featured' ).on( 'click', function( event ) {
  14. event.preventDefault();
  15.  
  16. // if the file_frame has already been created, just reuse it
  17. if ( file_frame ) {
  18. file_frame.open();
  19. return;
  20. }
  21.  
  22. file_frame = wp.media.frames.file_frame = wp.media({
  23. title: $( this ).data( 'uploader_title' ),
  24. button: {
  25. text: $( this ).data( 'uploader_button_text' ),
  26. },
  27. multiple: false // set this to true for multiple file selection
  28. });
  29.  
  30. file_frame.on( 'select', function() {
  31. attachment = file_frame.state().get('selection').first().toJSON();
  32.  
  33. // do something with the file here
  34. $( '#featured' ).hide();
  35. $( '#frontend-image' ).attr('src', attachment.url);
  36. });
  37.  
  38. file_frame.open();
  39. });
  40. });
  41.  
  42. })(jQuery);
  43.  
  44. function createPost() {
  45.  
  46. // fetaured image
  47. $uploaddir = wp_upload_dir();
  48. $file = $_FILES['featured'];
  49. $uploadfile = $uploaddir['path'] . '/' . basename( $file['name'] );
  50. move_uploaded_file( $file['tmp_name'] , $uploadfile );
  51. $filename = basename( $uploadfile );
  52. $wp_filetype = wp_check_filetype(basename($filename), null );
  53. $attachment = array(
  54. 'post_mime_type' => $wp_filetype['type'],
  55. 'post_title' => preg_replace('/.[^.]+$/', '', $filename),
  56. 'post_content' => '',
  57. 'post_status' => 'inherit',
  58. 'menu_order' => $_i + 1000
  59. );
  60. $attach_id = wp_insert_attachment( $attachment, $uploadfile );
  61. $featured_url = wp_get_attachment_url( $attach_id );
  62.  
  63. //Get the ID of currently logged in user to set as post author
  64. $current_user = wp_get_current_user();
  65. $currentuserid = $current_user->ID;
  66.  
  67. //Get the details from the form which was posted
  68. $postTitle = $_POST['postTitle'];
  69. $contentOfPost = $_POST['postContent'] ;
  70. $postcontentImage = $_POST['frontend-image'] ;
  71. $postSatus = 'publish'; // 'pending' - in case you want to manually aprove all posts;
  72.  
  73. //Create the post in WordPress
  74. $post_info = array(
  75. 'post_title' => $postTitle,
  76. 'post_content' => $contentOfPost,
  77. 'post_status' => $postSatus ,
  78. 'post_author' => $currentuserid,
  79. 'post_mime_type' => $wp_filetype['type'],
  80. // 'post_mime_type' => $featured_url,
  81. // 'thumbnail_id' => $featured_url
  82. );
  83. $post_id = wp_insert_post( $post_info );
  84.  
  85. update_post_meta($post_id,'_thumbnail_id',$attach_id);
  86. set_post_thumbnail( $post_id, $attachmentId );
  87.  
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement