media_buttons

函式
media_buttons ( $editor_id = 'content' )
引數
  • (string) $editor_id
    Required:
    Default: 'content'
定義位置
相關方法
_media_buttonsubmit_buttonget_submit_buttonmedia_upload_tabsget_media_items
引入
2.5.0
棄用
-

media_buttons函式用來在WordPress編輯器中新增一個”新增媒體”按鈕: 當該按鈕被點選時,它會開啟媒體上傳器,允許使用者上傳或選擇媒體檔案插入到文章或頁面。

將媒體按鈕新增到編輯器中。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function media_buttons( $editor_id = 'content' ) {
static $instance = 0;
$instance++;
$post = get_post();
if ( ! $post && ! empty( $GLOBALS['post_ID'] ) ) {
$post = $GLOBALS['post_ID'];
}
wp_enqueue_media( array( 'post' => $post ) );
$img = '<span class="wp-media-buttons-icon"></span> ';
$id_attribute = 1 === $instance ? ' id="insert-media-button"' : '';
printf(
'<button type="button"%s class="button insert-media add_media" data-editor="%s">%s</button>',
$id_attribute,
esc_attr( $editor_id ),
$img . __( 'Add Media' )
);
/**
* Filters the legacy (pre-3.5.0) media buttons.
*
* Use {@see 'media_buttons'} action instead.
*
* @since 2.5.0
* @deprecated 3.5.0 Use {@see 'media_buttons'} action instead.
*
* @param string $string Media buttons context. Default empty.
*/
$legacy_filter = apply_filters_deprecated( 'media_buttons_context', array( '' ), '3.5.0', 'media_buttons' );
if ( $legacy_filter ) {
// #WP22559. Close <a> if a plugin started by closing <a> to open their own <a> tag.
if ( 0 === stripos( trim( $legacy_filter ), '</a>' ) ) {
$legacy_filter .= '</a>';
}
echo $legacy_filter;
}
}
function media_buttons( $editor_id = 'content' ) { static $instance = 0; $instance++; $post = get_post(); if ( ! $post && ! empty( $GLOBALS['post_ID'] ) ) { $post = $GLOBALS['post_ID']; } wp_enqueue_media( array( 'post' => $post ) ); $img = '<span class="wp-media-buttons-icon"></span> '; $id_attribute = 1 === $instance ? ' id="insert-media-button"' : ''; printf( '<button type="button"%s class="button insert-media add_media" data-editor="%s">%s</button>', $id_attribute, esc_attr( $editor_id ), $img . __( 'Add Media' ) ); /** * Filters the legacy (pre-3.5.0) media buttons. * * Use {@see 'media_buttons'} action instead. * * @since 2.5.0 * @deprecated 3.5.0 Use {@see 'media_buttons'} action instead. * * @param string $string Media buttons context. Default empty. */ $legacy_filter = apply_filters_deprecated( 'media_buttons_context', array( '' ), '3.5.0', 'media_buttons' ); if ( $legacy_filter ) { // #WP22559. Close <a> if a plugin started by closing <a> to open their own <a> tag. if ( 0 === stripos( trim( $legacy_filter ), '</a>' ) ) { $legacy_filter .= '</a>'; } echo $legacy_filter; } }
function media_buttons( $editor_id = 'content' ) {
	static $instance = 0;
	$instance++;

	$post = get_post();

	if ( ! $post && ! empty( $GLOBALS['post_ID'] ) ) {
		$post = $GLOBALS['post_ID'];
	}

	wp_enqueue_media( array( 'post' => $post ) );

	$img = '<span class="wp-media-buttons-icon"></span> ';

	$id_attribute = 1 === $instance ? ' id="insert-media-button"' : '';

	printf(
		'<button type="button"%s class="button insert-media add_media" data-editor="%s">%s</button>',
		$id_attribute,
		esc_attr( $editor_id ),
		$img . __( 'Add Media' )
	);

	/**
	 * Filters the legacy (pre-3.5.0) media buttons.
	 *
	 * Use {@see 'media_buttons'} action instead.
	 *
	 * @since 2.5.0
	 * @deprecated 3.5.0 Use {@see 'media_buttons'} action instead.
	 *
	 * @param string $string Media buttons context. Default empty.
	 */
	$legacy_filter = apply_filters_deprecated( 'media_buttons_context', array( '' ), '3.5.0', 'media_buttons' );

	if ( $legacy_filter ) {
		// #WP22559. Close <a> if a plugin started by closing <a> to open their own <a> tag.
		if ( 0 === stripos( trim( $legacy_filter ), '</a>' ) ) {
			$legacy_filter .= '</a>';
		}
		echo $legacy_filter;
	}
}

常見問題

FAQs
檢視更多 >