Last active
August 29, 2015 14:03
-
-
Save dbranes/042ec65fec4a991c6381 to your computer and use it in GitHub Desktop.
Make wp_generate_attachment_metadata fast (http://www.wpquestions.com/question/showChrono/id/9671)
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 | |
/** | |
* heavy.php | |
* | |
* Script for generating meta data from POST inputs ( url, post_id, key ) | |
* | |
* version 0.0.1 | |
*/ | |
// Include WordPress: | |
define( 'WP_USE_THEMES', false ); | |
require( './wp-blog-header.php' ); // Edit this path to your needs, (relative or absolute path) | |
// Secret key: | |
$my_secret = 'ek4le8c4wh'; // Edit this to your needs. | |
// POST input parameters: | |
$input_post_id = filter_input( INPUT_POST, 'post_id', FILTER_SANITIZE_NUMBER_INT ); | |
$input_url = filter_input( INPUT_POST, 'url', FILTER_SANITIZE_URL ); | |
$input_secret = filter_input( INPUT_POST, 'secret', FILTER_SANITIZE_NUMBER_INT ); | |
// Validate input: | |
if( $mysecret != $input_secret | |
|| empty ( $input_url ) | |
|| empty ( $input_post_id ) | |
) | |
{ | |
die('No Access'); | |
} | |
//---------------- | |
// Your script: | |
//---------------- | |
// Get the path to the upload directory. | |
$wp_upload_dir = wp_upload_dir(); | |
// Image URL | |
$url = $input_url; | |
// Full PATH to image in Upload Directory | |
$filename = basename( $url ); | |
$file = $wp_upload_dir['path'] . "/$filename"; | |
// Full PATH to image in Upload Directory | |
$url2 = $wp_upload_dir['url'] . "/$filename"; | |
// The ID of the post this attachment is for. | |
$post_id = $input_post_id; | |
// Check the type of tile. We'll use this as the 'post_mime_type'. | |
$filetype = wp_check_filetype( $file ); | |
// Prepare an array of post data for the attachment. | |
$attachment = array( | |
'guid' => $url2, | |
'post_mime_type' => $filetype['type'], | |
'post_title' => preg_replace( '/\.[^.]+$/', '', $filename ), | |
'post_content' => '', | |
'post_parent' => $post_id, | |
); | |
// Make sure that this file is included, as wp_generate_attachment_metadata() depends on it. | |
require_once( ABSPATH . 'wp-admin/includes/image.php' ); | |
// Insert the attachment. | |
$attach_id = wp_insert_attachment( $attachment, $file, $post_id ); | |
if ( !is_wp_error($attach_id) ) { | |
// Generate the metadata for the attachment, and update the database record. | |
$attach_data = wp_generate_attachment_metadata( $attach_id, $file ); | |
wp_update_attachment_metadata( $attach_id, $attach_data ); | |
} | |
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 | |
//Multi upload field - Insert images into WP media library and link them to post ID | |
add_action( 'gform_after_submission_20', 'my_add_post_attachements', 10, 2 ); | |
function my_add_post_attachements( $entry, $form ) | |
{ | |
$submit = array(); | |
$submit = $entry["38"]; //The field ID of the multi upload | |
$submit = stripslashes($submit); | |
$submit = json_decode($submit, true); | |
// Edit this to your needs: | |
$my_secret = 'ek4le8c4wh'; | |
$my_heavy_script_url = 'http://example.com/heavy.php'; | |
// Loop: | |
if( function_exists( 'demo_async_curl_post' ) ) | |
{ | |
foreach ( $submit as $key => $value ) | |
{ | |
$post_fields = array( | |
'url' => $value, | |
'post_id' => $entry['post_id'], | |
'key' => $my_secret | |
); | |
demo_async_curl_post( $my_heavy_script_url, $post_fields ); | |
} | |
} | |
} | |
if( ! function_exists( 'demo_async_curl_post' ) ) | |
{ | |
function demo_async_curl_post( $url = '',$postfields = array() ) | |
{ | |
$curl = curl_init(); | |
curl_setopt( $curl, CURLOPT_URL, $url); | |
curl_setopt( $curl, CURLOPT_POST, TRUE); | |
curl_setopt( $curl, CURLOPT_POSTFIELDS, $postfields ); | |
curl_setopt( $curl, CURLOPT_USERAGENT, 'demo'); | |
curl_setopt( $curl, CURLOPT_TIMEOUT, 1 ); | |
$data = curl_exec( $curl ); | |
curl_close( $curl ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment