This is a library for creators of WordPress themes & plugins that makes it simple to include Media Uploader in WordPress admin panel. It's a fairly common client's requirement so why not make it simple.
It creates hidden input field with value set to the id of chosen attachment.
You can include it in any place (in WordPress admin panel) you want - meta boxes, option pages, you name it.
new HdkMediaUploader( array(
'name' => 'your_input_name', # <input name>
'attachment_id' => $current_attachment_id, # <input value>, empty if not set
'button_text' => 'Upload',
'button_class' => 'button'
) );
Let's assume we create a test
theme.
- Clone/copy repo contents to
/wp-content/themes/test/lib/hdk-media-uploader/
- Include
HdkMediaUploader.php
file, for example:
include_once( get_stylesheet_directory() . '/lib/hdk-media-uploader/HdkMediaUploader.php' );
- Use it inside your form:
new HdkMediaUploader( array(
'name' => 'your_input_name', # <input name>
'attachment_id' => $current_attachment_id, # <input value>
) );
Let's assume we create a test
plugin.
- Clone/copy repo contents to
/wp-content/plugins/test/lib/hdk-media-uploader/
- Include
HdkMediaUploader.php
file, for example:
include_once( plugin_dir_path( __FILE__ ) . '/lib/hdk-media-uploader/HdkMediaUploader.php' );
- Use it inside your form:
new HdkMediaUploader( array(
'name' => 'your_input_name', # <input name>
'attachment_id' => $current_attachment_id, # <input value>
) );
If you don't want to use /lib/hdk-media-uploader/
path, you're in luck. You can paste it wherever you want and use this one-line setup before creating instance:
HdkMediaUploader :: set_directory_url( 'your-url-for-this-repo' )
After this you can use it like you normally would.
Remember: url is not a path.
Path: http://codex.wordpress.org/Function_Reference/get_stylesheet_directory
URL: http://codex.wordpress.org/Function_Reference/get_stylesheet_directory_uri
- Adding image sets input's value to chosen attachment's ID
- Removing image unsets this input - changes must be saved. Removing image does not remove image from Media Library.
Most of the credit goes to Mike Jolley: http://mikejolley.com/2012/12/using-the-new-wordpress-3-5-media-uploader-in-plugins/