Using WordPress ‘media_buttons_context’ PHP filter

The media_buttons_context WordPress PHP filter allows you to modify the legacy media buttons (pre-3.5.0). It is recommended to use the ‘media_buttons’ action instead.

Usage

add_filter('media_buttons_context', 'your_custom_function');
function your_custom_function($string) {
  // your custom code here
  return $string;
}

Parameters

  • $string (string): The media buttons context, default empty.

More information

See WordPress Developer Resources: media_buttons_context

Examples

Add a custom button to the media_buttons_context

add_filter('media_buttons_context', 'add_custom_media_button');
function add_custom_media_button($string) {
  $button = '<a href="#" class="button custom-media-button">Custom Media Button</a>';
  return $string . $button;
}

Remove the default Add Media button

add_filter('media_buttons_context', 'remove_add_media_button');
function remove_add_media_button($string) {
  return str_replace('Add Media', '', $string);
}

Replace the default Add Media button with a custom button

add_filter('media_buttons_context', 'replace_add_media_button');
function replace_add_media_button($string) {
  $button = '<a href="#" class="button custom-media-button">Custom Media Button</a>';
  return str_replace('Add Media', $button, $string);
}

Add a custom CSS class to the Add Media button

add_filter('media_buttons_context', 'add_custom_class_to_add_media_button');
function add_custom_class_to_add_media_button($string) {
  return str_replace('class="button add_media"', 'class="button add_media custom-class"', $string);
}

Append custom text to the Add Media button

add_filter('media_buttons_context', 'append_text_to_add_media_button');
function append_text_to_add_media_button($string) {
  return str_replace('Add Media', 'Add Media (Custom Text)', $string);
}