Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <fieldset>
- <label for="postImage">Fetaured Image</label>
- <input type="file" name="featured" id="featured" size="50">
- <img name="frontend-image" id="frontend-image" width="150" height="150">
- </fieldset>
- (function($) {
- $(document).ready( function() {
- var file_frame; // variable for the wp.media file_frame
- // attach a click event (or whatever you want) to some element on your page
- $( '#featured' ).on( 'click', function( event ) {
- event.preventDefault();
- // if the file_frame has already been created, just reuse it
- if ( file_frame ) {
- file_frame.open();
- return;
- }
- file_frame = wp.media.frames.file_frame = wp.media({
- title: $( this ).data( 'uploader_title' ),
- button: {
- text: $( this ).data( 'uploader_button_text' ),
- },
- multiple: false // set this to true for multiple file selection
- });
- file_frame.on( 'select', function() {
- attachment = file_frame.state().get('selection').first().toJSON();
- // do something with the file here
- $( '#featured' ).hide();
- $( '#frontend-image' ).attr('src', attachment.url);
- });
- file_frame.open();
- });
- });
- })(jQuery);
- function createPost() {
- // fetaured image
- $uploaddir = wp_upload_dir();
- $file = $_FILES['featured'];
- $uploadfile = $uploaddir['path'] . '/' . basename( $file['name'] );
- move_uploaded_file( $file['tmp_name'] , $uploadfile );
- $filename = basename( $uploadfile );
- $wp_filetype = wp_check_filetype(basename($filename), null );
- $attachment = array(
- 'post_mime_type' => $wp_filetype['type'],
- 'post_title' => preg_replace('/.[^.]+$/', '', $filename),
- 'post_content' => '',
- 'post_status' => 'inherit',
- 'menu_order' => $_i + 1000
- );
- $attach_id = wp_insert_attachment( $attachment, $uploadfile );
- $featured_url = wp_get_attachment_url( $attach_id );
- //Get the ID of currently logged in user to set as post author
- $current_user = wp_get_current_user();
- $currentuserid = $current_user->ID;
- //Get the details from the form which was posted
- $postTitle = $_POST['postTitle'];
- $contentOfPost = $_POST['postContent'] ;
- $postcontentImage = $_POST['frontend-image'] ;
- $postSatus = 'publish'; // 'pending' - in case you want to manually aprove all posts;
- //Create the post in WordPress
- $post_info = array(
- 'post_title' => $postTitle,
- 'post_content' => $contentOfPost,
- 'post_status' => $postSatus ,
- 'post_author' => $currentuserid,
- 'post_mime_type' => $wp_filetype['type'],
- // 'post_mime_type' => $featured_url,
- // 'thumbnail_id' => $featured_url
- );
- $post_id = wp_insert_post( $post_info );
- update_post_meta($post_id,'_thumbnail_id',$attach_id);
- set_post_thumbnail( $post_id, $attachmentId );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement