Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to render a media button in WordPress post edit, making it work for WordPress 3.x.
<?php
/**
* This assumes you'll be plugging in to media-upload.php in order to upload a file.
*
* @author Justin Endler
*/
// Get WordPress version.
$version = get_bloginfo('version');
if ( $version >= 3.5 ) {
// This filter name still works. media_buttons works as well.
add_filter( 'media_buttons_context', 'media_button_callback' );
} else {
add_filter( 'media_buttons_context', 'media_button_legacy_callback' );
}
/**
* Place the media button. WP versions up to 3.4.x
*
* @param $context
* Existing button text.
* @return $markup
*/
function media_button_legacy( $context ) {
global $post;
$button = ' %s' .
'<a title="My Media Button" href="' . admin_url('/media-upload.php?post_id=' . $post->ID . '&type=my_textdomain&TB_iframe=1') . '"></a>';
$markup = sprintf($context, $button);
return $markup;
}
/**
* Place the media button. WP versions 3.5 +
*/
public function media_button() {
global $post;
$button = '<a title="My Media Button" href="' . admin_url('/media-upload.php?post_id=' . $post->ID . '&type=my_textdomain&TB_iframe=1') . '"></a>';
echo $button;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.