get_provider( $url ); if ( $provider === false && substr($url, 0, 5) === 'https' ) { $url = str_replace('https', 'http', $url); $provider = $oembed->get_provider( $url ); } if ( $provider === false ) { wp_send_json_error(); } $response = $oembed->fetch($provider, $url); if ( $response === false ) { wp_send_json_error(); } $attachment = array( 'post_parent' => $post_ID, 'post_title' => $response->title, 'post_content' => '', 'post_status' => 'inherit', 'post_author' => get_current_user_id(), 'post_type' => 'attachment', 'guid' => $url, 'post_mime_type'=> 'oembed/' . strtolower($response->provider_name) ); $attachment_id = wp_insert_post( $attachment ); if( ! is_int($attachment_id) ) { wp_send_json_error(); } $width = ( !empty($width) ? $width : 400 ); $height = ( !empty($height) ? $height : 400 ); update_post_meta($attachment_id, '_oembed_width', $width); update_post_meta($attachment_id, '_oembed_height', $height); if ( ! $attachment_js = wp_prepare_attachment_for_js( $attachment_id ) ) { wp_send_json_error(); } wp_send_json_success( $attachment_js ); } /** * Adds the width and height fields to the attachment details panel. * * @since 1.0 * */ function attachment_fields_to_edit($form_fields, $post) { $mime = get_post_mime_type($post->ID); $type = strtok($mime, '/'); if ($type == 'oembed') { $form_fields['width'] = array( 'label' => __('Width'), 'input' => 'text', 'value' => get_post_meta($post->ID, '_oembed_width', true) ); $form_fields['height'] = array( 'label' => __('Height'), 'input' => 'text', 'value' => get_post_meta($post->ID, '_oembed_height', true) ); } return $form_fields; } /** * Save the width and height field values to the database. * * @since 1.0 * */ function attachment_fields_to_save($post, $attachment) { if( isset($attachment['width']) ){ update_post_meta($post['ID'], '_oembed_width', $attachment['width']); } if( isset($attachment['height']) ){ update_post_meta($post['ID'], '_oembed_height', $attachment['height']); } return $post; } /** * Prints the attachment details media view template. * * @since 1.0 * */ function print_media_templates(){ ?>