WordPress: Adding a form parameter to media uploads
Say you want to get a form parameter to pass to the media uploader so you can test for it in the $_POST on your wp_handle_upload filter…
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<code>/** * Add checkbox to set autocreate on upload */ function fifteenccgal_autocreate_post_on_image_upload_form_tweaks() { ?> <label style="display: inline-block; padding: 5px 8px; border: 3px solid lightgrey; border-radius: 1em"> <input type="checkbox" id="fifcc_autocreate" name="fifcc_autocreate" value="1" checked="<?php echo (FIFTEENCCGAL_AUTOCREATE_DEFAULT_STATE ? 'checked' : '')?>" /> &nbsp;Auto-create a post for each uploaded photo (JPG)? </label><br/><br/> <script type="text/javascript"> jQuery(function ($) { $('#fifcc_autocreate').change(function (e) { var post_params = swfu.settings.post_params; post_params.fifcc_autocreate = ($('#fifcc_autocreate:checked').val() ? '1' : '0'); swfu.setPostParams(post_params); }) }); </script> <?php } add_action('post-upload-ui', 'fifteenccgal_autocreate_post_on_image_upload_form_tweaks'); </code> |
Getting the form field in is no problem but you also need to hack into the SWFUploader instance’s post parameters. This MUST be done via the setPostParams() method. Setting swfu.settings.post_params directly doesn’t work.
You might also wish to set a default value on load. WordPress provides a suitable hook…
|
1 2 3 4 5 6 7 8 9 10 |
<code>/** * Initialise the value to reflect the checkbox default */ function fifteenccgal_autocreate_post_on_image_upload_post_params_init($params) { $params['fifcc_autocreate'] = FIFTEENCCGAL_AUTOCREATE_DEFAULT_STATE; return $params; } add_filter('swfupload_post_params', 'fifteenccgal_autocreate_post_on_image_upload_post_params_init'); </code> |


