wp_enqueue_media

Enqueues all scripts, styles, settings, and templates necessary to use all media JS APIs.

Description

wp_enqueue_media( (array) $args = array() ); 

Parameters (1)

0. $args — Optional. (array) => array()
Arguments for enqueuing media scripts.

Options

    ;        


    Usage

    1. if ( !function_exists( 'wp_enqueue_media' ) ) { 
    2. require_once ABSPATH . WPINC . '/media.php'; 
    3.  
    4. // Arguments for enqueuing media scripts. 
    5. $args = array(); 
    6.  
    7. // NOTICE! Understand what this does before running. 
    8. $result = wp_enqueue_media($args); 
    9.  

    Defined (1)

    The function is defined in the following location(s).

    /wp-includes/media.php  
    1. function wp_enqueue_media( $args = array() ) { 
    2. // Enqueue me just once per page, please. 
    3. if ( did_action( 'wp_enqueue_media' ) ) 
    4. return; 
    5.  
    6. global $content_width, $wpdb, $wp_locale; 
    7.  
    8. $defaults = array( 
    9. 'post' => null,  
    10. ); 
    11. $args = wp_parse_args( $args, $defaults ); 
    12.  
    13. // We're going to pass the old thickbox media tabs to `media_upload_tabs` 
    14. // to ensure plugins will work. We will then unset those tabs. 
    15. $tabs = array( 
    16. // handler action suffix => tab label 
    17. 'type' => '',  
    18. 'type_url' => '',  
    19. 'gallery' => '',  
    20. 'library' => '',  
    21. ); 
    22.  
    23. /** This filter is documented in wp-admin/includes/media.php */ 
    24. $tabs = apply_filters( 'media_upload_tabs', $tabs ); 
    25. unset( $tabs['type'], $tabs['type_url'], $tabs['gallery'], $tabs['library'] ); 
    26.  
    27. $props = array( 
    28. 'link' => get_option( 'image_default_link_type' ), // db default is 'file' 
    29. 'align' => get_option( 'image_default_align' ), // empty default 
    30. 'size' => get_option( 'image_default_size' ), // empty default 
    31. ); 
    32.  
    33. $exts = array_merge( wp_get_audio_extensions(), wp_get_video_extensions() ); 
    34. $mimes = get_allowed_mime_types(); 
    35. $ext_mimes = array(); 
    36. foreach ( $exts as $ext ) { 
    37. foreach ( $mimes as $ext_preg => $mime_match ) { 
    38. if ( preg_match( '#' . $ext . '#i', $ext_preg ) ) { 
    39. $ext_mimes[ $ext ] = $mime_match; 
    40. break; 
    41.  
    42. /** 
    43. * Allows showing or hiding the "Create Audio Playlist" button in the media library. 
    44. * By default (if this filter returns `null`), a query will be run to 
    45. * determine whether the media library contains any audio items. This 
    46. * query is expensive for large media libraries, so it may be desirable for 
    47. * sites to override this behavior. 
    48. * @since 4.7.4 
    49. * @link https://core.trac.wordpress.org/ticket/31071 
    50. * @param bool|null Whether to show the button, or `null` for default behavior. 
    51. */ 
    52. $show_audio_playlist = apply_filters( 'media_library_show_audio_playlist', null ); 
    53. if ( null === $show_audio_playlist ) { 
    54. $show_audio_playlist = $wpdb->get_var( " 
    55. SELECT ID 
    56. FROM $wpdb->posts 
    57. WHERE post_type = 'attachment' 
    58. AND post_mime_type LIKE 'audio%' 
    59. LIMIT 1 
    60. " ); 
    61.  
    62. /** 
    63. * Allows showing or hiding the "Create Video Playlist" button in the media library. 
    64. * By default (if this filter returns `null`), a query will be run to 
    65. * determine whether the media library contains any video items. This 
    66. * query is expensive for large media libraries, so it may be desirable for 
    67. * sites to override this behavior. 
    68. * @since 4.7.4 
    69. * @link https://core.trac.wordpress.org/ticket/31071 
    70. * @param bool|null Whether to show the button, or `null` for default behavior. 
    71. */ 
    72. $show_video_playlist = apply_filters( 'media_library_show_video_playlist', null ); 
    73. if ( null === $show_video_playlist ) { 
    74. $show_video_playlist = $wpdb->get_var( " 
    75. SELECT ID 
    76. FROM $wpdb->posts 
    77. WHERE post_type = 'attachment' 
    78. AND post_mime_type LIKE 'video%' 
    79. LIMIT 1 
    80. " ); 
    81.  
    82. /** 
    83. * Allows overriding the list of months displayed in the media library. 
    84. * By default (if this filter does not return an array), a query will be 
    85. * run to determine the months that have media items. This query can be 
    86. * expensive for large media libraries, so it may be desirable for sites to 
    87. * override this behavior. 
    88. * @since 4.7.4 
    89. * @link https://core.trac.wordpress.org/ticket/31071 
    90. * @param array|null An array of objects with `month` and `year` 
    91. * properties, or `null` (or any other non-array value) 
    92. * for default behavior. 
    93. */ 
    94. if ( ! is_array( $months ) ) { 
    95. $months = $wpdb->get_results( $wpdb->prepare( " 
    96. SELECT DISTINCT YEAR( post_date ) AS year, MONTH( post_date ) AS month 
    97. FROM $wpdb->posts 
    98. WHERE post_type = %s 
    99. ORDER BY post_date DESC 
    100. ", 'attachment' ) ); 
    101. foreach ( $months as $month_year ) { 
    102. $month_year->text = sprintf( __( '%1$s %2$d' ), $wp_locale->get_month( $month_year->month ), $month_year->year ); 
    103.  
    104. $settings = array( 
    105. 'tabs' => $tabs,  
    106. 'tabUrl' => add_query_arg( array( 'chromeless' => true ), admin_url('media-upload.php') ),  
    107. 'mimeTypes' => wp_list_pluck( get_post_mime_types(), 0 ),  
    108. /** This filter is documented in wp-admin/includes/media.php */ 
    109. 'captions' => ! apply_filters( 'disable_captions', '' ),  
    110. 'nonce' => array( 
    111. 'sendToEditor' => wp_create_nonce( 'media-send-to-editor' ),  
    112. ),  
    113. 'post' => array( 
    114. 'id' => 0,  
    115. ),  
    116. 'defaultProps' => $props,  
    117. 'attachmentCounts' => array( 
    118. 'audio' => ( $show_audio_playlist ) ? 1 : 0,  
    119. 'video' => ( $show_video_playlist ) ? 1 : 0,  
    120. ),  
    121. 'embedExts' => $exts,  
    122. 'embedMimes' => $ext_mimes,  
    123. 'contentWidth' => $content_width,  
    124. 'months' => $months,  
    125. 'mediaTrash' => MEDIA_TRASH ? 1 : 0,  
    126. ); 
    127.  
    128. $post = null; 
    129. if ( isset( $args['post'] ) ) { 
    130. $post = get_post( $args['post'] ); 
    131. $settings['post'] = array( 
    132. 'id' => $post->ID,  
    133. 'nonce' => wp_create_nonce( 'update-post_' . $post->ID ),  
    134. ); 
    135.  
    136. $thumbnail_support = current_theme_supports( 'post-thumbnails', $post->post_type ) && post_type_supports( $post->post_type, 'thumbnail' ); 
    137. if ( ! $thumbnail_support && 'attachment' === $post->post_type && $post->post_mime_type ) { 
    138. if ( wp_attachment_is( 'audio', $post ) ) { 
    139. $thumbnail_support = post_type_supports( 'attachment:audio', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:audio' ); 
    140. } elseif ( wp_attachment_is( 'video', $post ) ) { 
    141. $thumbnail_support = post_type_supports( 'attachment:video', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:video' ); 
    142.  
    143. if ( $thumbnail_support ) { 
    144. $featured_image_id = get_post_meta( $post->ID, '_thumbnail_id', true ); 
    145. $settings['post']['featuredImageId'] = $featured_image_id ? $featured_image_id : -1; 
    146.  
    147. if ( $post ) { 
    148. $post_type_object = get_post_type_object( $post->post_type ); 
    149. } else { 
    150. $post_type_object = get_post_type_object( 'post' ); 
    151.  
    152. $strings = array( 
    153. // Generic 
    154. 'url' => __( 'URL' ),  
    155. 'addMedia' => __( 'Add Media' ),  
    156. 'search' => __( 'Search' ),  
    157. 'select' => __( 'Select' ),  
    158. 'cancel' => __( 'Cancel' ),  
    159. 'update' => __( 'Update' ),  
    160. 'replace' => __( 'Replace' ),  
    161. 'remove' => __( 'Remove' ),  
    162. 'back' => __( 'Back' ),  
    163. /** translators: This is a would-be plural string used in the media manager. 
    164. If there is not a word you can use in your language to avoid issues with the 
    165. lack of plural support here, turn it into "selected: %d" then translate it. 
    166. */ 
    167. 'selected' => __( '%d selected' ),  
    168. 'dragInfo' => __( 'Drag and drop to reorder media files.' ),  
    169.  
    170. // Upload 
    171. 'uploadFilesTitle' => __( 'Upload Files' ),  
    172. 'uploadImagesTitle' => __( 'Upload Images' ),  
    173.  
    174. // Library 
    175. 'mediaLibraryTitle' => __( 'Media Library' ),  
    176. 'insertMediaTitle' => __( 'Insert Media' ),  
    177. 'createNewGallery' => __( 'Create a new gallery' ),  
    178. 'createNewPlaylist' => __( 'Create a new playlist' ),  
    179. 'createNewVideoPlaylist' => __( 'Create a new video playlist' ),  
    180. 'returnToLibrary' => __( '← Return to library' ),  
    181. 'allMediaItems' => __( 'All media items' ),  
    182. 'allDates' => __( 'All dates' ),  
    183. 'noItemsFound' => __( 'No items found.' ),  
    184. 'insertIntoPost' => $post_type_object->labels->insert_into_item,  
    185. 'unattached' => __( 'Unattached' ),  
    186. 'trash' => _x( 'Trash', 'noun' ),  
    187. 'uploadedToThisPost' => $post_type_object->labels->uploaded_to_this_item,  
    188. 'warnDelete' => __( "You are about to permanently delete this item.\n 'Cancel' to stop, 'OK' to delete." ),  
    189. 'warnBulkDelete' => __( "You are about to permanently delete these items.\n 'Cancel' to stop, 'OK' to delete." ),  
    190. 'warnBulkTrash' => __( "You are about to trash these items.\n 'Cancel' to stop, 'OK' to delete." ),  
    191. 'bulkSelect' => __( 'Bulk Select' ),  
    192. 'cancelSelection' => __( 'Cancel Selection' ),  
    193. 'trashSelected' => __( 'Trash Selected' ),  
    194. 'untrashSelected' => __( 'Untrash Selected' ),  
    195. 'deleteSelected' => __( 'Delete Selected' ),  
    196. 'deletePermanently' => __( 'Delete Permanently' ),  
    197. 'apply' => __( 'Apply' ),  
    198. 'filterByDate' => __( 'Filter by date' ),  
    199. 'filterByType' => __( 'Filter by type' ),  
    200. 'searchMediaLabel' => __( 'Search Media' ),  
    201. 'searchMediaPlaceholder' => __( 'Search media items...' ), // placeholder (no ellipsis) 
    202. 'noMedia' => __( 'No media files found.' ),  
    203.  
    204. // Library Details 
    205. 'attachmentDetails' => __( 'Attachment Details' ),  
    206.  
    207. // From URL 
    208. 'insertFromUrlTitle' => __( 'Insert from URL' ),  
    209.  
    210. // Featured Images 
    211. 'setFeaturedImageTitle' => $post_type_object->labels->featured_image,  
    212. 'setFeaturedImage' => $post_type_object->labels->set_featured_image,  
    213.  
    214. // Gallery 
    215. 'createGalleryTitle' => __( 'Create Gallery' ),  
    216. 'editGalleryTitle' => __( 'Edit Gallery' ),  
    217. 'cancelGalleryTitle' => __( '← Cancel Gallery' ),  
    218. 'insertGallery' => __( 'Insert gallery' ),  
    219. 'updateGallery' => __( 'Update gallery' ),  
    220. 'addToGallery' => __( 'Add to gallery' ),  
    221. 'addToGalleryTitle' => __( 'Add to Gallery' ),  
    222. 'reverseOrder' => __( 'Reverse order' ),  
    223.  
    224. // Edit Image 
    225. 'imageDetailsTitle' => __( 'Image Details' ),  
    226. 'imageReplaceTitle' => __( 'Replace Image' ),  
    227. 'imageDetailsCancel' => __( 'Cancel Edit' ),  
    228. 'editImage' => __( 'Edit Image' ),  
    229.  
    230. // Crop Image 
    231. 'chooseImage' => __( 'Choose Image' ),  
    232. 'selectAndCrop' => __( 'Select and Crop' ),  
    233. 'skipCropping' => __( 'Skip Cropping' ),  
    234. 'cropImage' => __( 'Crop Image' ),  
    235. 'cropYourImage' => __( 'Crop your image' ),  
    236. 'cropping' => __( 'Cropping…' ),  
    237. 'suggestedDimensions' => __( 'Suggested image dimensions:' ),  
    238. 'cropError' => __( 'There has been anerrorcropping your image.' ),  
    239.  
    240. // Edit Audio 
    241. 'audioDetailsTitle' => __( 'Audio Details' ),  
    242. 'audioReplaceTitle' => __( 'Replace Audio' ),  
    243. 'audioAddSourceTitle' => __( 'Add Audio Source' ),  
    244. 'audioDetailsCancel' => __( 'Cancel Edit' ),  
    245.  
    246. // Edit Video 
    247. 'videoDetailsTitle' => __( 'Video Details' ),  
    248. 'videoReplaceTitle' => __( 'Replace Video' ),  
    249. 'videoAddSourceTitle' => __( 'Add Video Source' ),  
    250. 'videoDetailsCancel' => __( 'Cancel Edit' ),  
    251. 'videoSelectPosterImageTitle' => __( 'Select Poster Image' ),  
    252. 'videoAddTrackTitle' => __( 'Add Subtitles' ),  
    253.  
    254. // Playlist 
    255. 'playlistDragInfo' => __( 'Drag and drop to reorder tracks.' ),  
    256. 'createPlaylistTitle' => __( 'Create Audio Playlist' ),  
    257. 'editPlaylistTitle' => __( 'Edit Audio Playlist' ),  
    258. 'cancelPlaylistTitle' => __( '← Cancel Audio Playlist' ),  
    259. 'insertPlaylist' => __( 'Insert audio playlist' ),  
    260. 'updatePlaylist' => __( 'Update audio playlist' ),  
    261. 'addToPlaylist' => __( 'Add to audio playlist' ),  
    262. 'addToPlaylistTitle' => __( 'Add to Audio Playlist' ),  
    263.  
    264. // Video Playlist 
    265. 'videoPlaylistDragInfo' => __( 'Drag and drop to reorder videos.' ),  
    266. 'createVideoPlaylistTitle' => __( 'Create Video Playlist' ),  
    267. 'editVideoPlaylistTitle' => __( 'Edit Video Playlist' ),  
    268. 'cancelVideoPlaylistTitle' => __( '← Cancel Video Playlist' ),  
    269. 'insertVideoPlaylist' => __( 'Insert video playlist' ),  
    270. 'updateVideoPlaylist' => __( 'Update video playlist' ),  
    271. 'addToVideoPlaylist' => __( 'Add to video playlist' ),  
    272. 'addToVideoPlaylistTitle' => __( 'Add to Video Playlist' ),  
    273. ); 
    274.  
    275. /** 
    276. * Filters the media view settings. 
    277. * @since 3.5.0 
    278. * @param array $settings List of media view settings. 
    279. * @param WP_Post $post Post object. 
    280. */ 
    281. $settings = apply_filters( 'media_view_settings', $settings, $post ); 
    282.  
    283. /** 
    284. * Filters the media view strings. 
    285. * @since 3.5.0 
    286. * @param array $strings List of media view strings. 
    287. * @param WP_Post $post Post object. 
    288. */ 
    289. $strings = apply_filters( 'media_view_strings', $strings, $post ); 
    290.  
    291. $strings['settings'] = $settings; 
    292.  
    293. // Ensure we enqueue media-editor first, that way media-views is 
    294. // registered internally before we try to localize it. see #24724. 
    295. wp_enqueue_script( 'media-editor' ); 
    296. wp_localize_script( 'media-views', '_wpMediaViewsL10n', $strings ); 
    297.  
    298. wp_enqueue_script( 'media-audiovideo' ); 
    299. wp_enqueue_style( 'media-views' ); 
    300. if ( is_admin() ) { 
    301. wp_enqueue_script( 'mce-view' ); 
    302. wp_enqueue_script( 'image-edit' ); 
    303. wp_enqueue_style( 'imgareaselect' ); 
    304.  
    305. require_once ABSPATH . WPINC . '/media-template.php'; 
    306.  
    307. /** 
    308. * Fires at the conclusion of wp_enqueue_media(). 
    309. * @since 3.5.0 
    310. */