Using WordPress ‘media_buttons()’ PHP function

The media_buttons() WordPress PHP function adds the media button to the editor.

Usage

media_buttons( string $editor_id = 'content' );

A custom example showing input and output:

media_buttons( 'my_custom_editor' );

Parameters

  • $editor_id (string) – Optional. The ID of the editor where the media button will be added. Default is ‘content’.

More information

See WordPress Developer Resources: media_buttons

Examples

Add media button to a custom editor

This example shows how to add a media button to a custom editor with the ID ‘my_custom_editor’:

// Add media button to 'my_custom_editor'
media_buttons( 'my_custom_editor' );

Add media button to multiple custom editors

This example demonstrates how to add media buttons to multiple custom editors with different IDs:

// Add media buttons to different custom editors
media_buttons( 'first_editor' );
media_buttons( 'second_editor' );

Create a custom meta box with a media button

This example creates a custom meta box with a text area and a media button:

// Register the meta box
add_action( 'add_meta_boxes', 'my_custom_meta_box' );

function my_custom_meta_box() {
    add_meta_box( 'custom_meta_box_id', 'Custom Meta Box', 'custom_meta_box_callback', 'post' );
}

function custom_meta_box_callback( $post ) {
    // Display the text area and media button
    echo '<textarea id="custom_editor" name="custom_editor"></textarea>';
    media_buttons( 'custom_editor' );
}

Add media button to a custom form on an admin page

This example adds a media button to a custom form on an admin page:

function my_custom_admin_page() {
    // Display the form with a custom editor and media button
    echo '<form method="post">';
    echo '<textarea id="custom_editor" name="custom_editor"></textarea>';
    media_buttons( 'custom_editor' );
    echo '<input type="submit" value="Submit"></form>';
}

add_action( 'admin_menu', 'register_my_custom_admin_page' );

function register_my_custom_admin_page() {
    add_menu_page( 'My Custom Page', 'My Custom Page', 'manage_options', 'my_custom_page', 'my_custom_admin_page', '', 6 );
}

Add media button to the default editor

This example adds a media button to the default editor:

// Add media button to the default editor
media_buttons();