Skip to content

Instantly share code, notes, and snippets.

@hissy
hissy / gist:7352933
Created Nov 7, 2013
[WordPress] Add file to media library programmatically
View gist:7352933
<?php
$file = '/path/to/file.png';
$filename = basename($file);
$upload_file = wp_upload_bits($filename, null, file_get_contents($file));
if (!$upload_file['error']) {
$wp_filetype = wp_check_filetype($filename, null );
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_parent' => $parent_post_id,
View Gydo.js
var file_frame;
// "mca_tray_button" is the ID of my button that opens the Media window
jQuery('#mca_tray_button').live('click', function( event ){
event.preventDefault();
if ( file_frame ) {
file_frame.open();
return;