Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I need to call this bit of PHP when a file is uploaded, deleted or edited.

$output = shell_exec("bash ./upload.sh"); echo $output;

I can't seem to find the right code to execute this when any of the above happen. Examples and help would be greatly appreciated.

share|improve this question

2 Answers 2

You don't say if the upload is in admin or on the user side. User side is probably via a plugin, so I have no idea what you are using.

Admin side has a number of hooks related to uploads. Some of them are listed below. Many WP hooks have little or no documentation, so you may have to Read the Source®. HTH

./wp-app.php:           do_action('wp_create_file_in_uploads', $file); // replicate
./wp-admin/includes/media.php:do_action('admin_enqueue_scripts', 'media-upload-popup');
./wp-admin/includes/media.php:do_action('admin_print_styles-media-upload-popup');
./wp-admin/includes/media.php:do_action('admin_print_scripts-media-upload-popup');
./wp-admin/includes/media.php:do_action('admin_head-media-upload-popup');
./wp-admin/includes/media.php:do_action('pre-upload-ui');
./wp-admin/includes/media.php:<?php do_action('pre-flash-upload-ui'); ?>
./wp-admin/includes/media.php:<?php do_action('post-flash-upload-ui'); ?>
./wp-admin/includes/media.php:<?php do_action('pre-html-upload-ui'); ?>
./wp-admin/includes/media.php:<?php do_action('post-html-upload-ui', $flash); ?>
./wp-admin/includes/media.php:<?php do_action('post-upload-ui'); ?>
./wp-admin/custom-background.php:               do_action('wp_create_file_in_uploads', $file, $id); // For replication
./wp-admin/upload.php:<?php do_action('restrict_manage_posts'); ?>
./wp-admin/custom-header.php:                   do_action('wp_create_file_in_uploads', $file, $id); // For replication
./wp-admin/media-upload.php:            do_action("media_upload_$type");
./wp-admin/media-upload.php:            do_action("media_upload_$tab");
share|improve this answer
    
The script runs on the server.. it's a upload.sh script that runs commands to upload to amazon S3 using s3sync. –  user4406 Apr 4 '11 at 4:40
    
I had it working in just a .php file that if I loaded it would call the script and echo the results into the browser (what files were uploaded/deleted) but I can't seem to find a hook that would trigger the script after a file is changed.. as it is now I'm having to manually run the script. I could setup a cron job but it seems like a waste of LISTs. I would rather just run it when something needs to get upload/deleted. I was surprised how easy it was to rewrite the urls for S3.. I made a plugin with like 5 lines of code that loads all of my uploads from S3. –  user4406 Apr 4 '11 at 4:46
    
If anyone is interested.. here is the plugin for rewriting the image URLs. <?php /* Plugin Name: Rewrite Plugin URI: SudoWorks.com Description: Rewrites your image paths so that you can use a CDN. Version: 2.4 Author: SudoWorks Author URI: SudoWorks.com */ function rewrite_url($rewrite) { $rewrite = str_replace('yoursite.com/wp-content/uploads/', 'media.yoursite.com/wp-content/uploads/', $rewrite); return $rewrite; } add_filter('wp_get_attachment_url', 'rewrite_url'); ?> Just set a S3 bucket of media.yoursite.com and point a cname at amazonaws.com. done. –  user4406 Apr 4 '11 at 4:53

Using the wp_generate_attachment_metadata filter seems to work OK:

function my_after_upload($metadata, $attachment_id) {
    // Your code here
}
add_filter( 'wp_generate_attachment_metadata', 'my_after_upload', 10, 2 );
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.