{"id":11360,"date":"2014-10-23T15:12:56","date_gmt":"2014-10-23T15:12:56","guid":{"rendered":"http:\/\/developer.wordpress.org\/?post_type=theme-handbook&#038;p=11360"},"modified":"2017-01-15T10:54:59","modified_gmt":"2017-01-15T10:54:59","slug":"media","status":"publish","type":"theme-handbook","link":"https:\/\/developer.wordpress.org\/themes\/classic-themes\/functionality\/media\/","title":{"rendered":"Media"},"content":{"rendered":"<p>WordPress enables theme developers to customize the look, feel, and functionality of the platform&#8217;s core media capabilities.<\/p>\n<h2>General<\/h2>\n<p>In WordPress you can upload, store, and display a variety of media such as image, video and audio files. Media can be uploaded via the <strong>Media &gt; Add New<\/strong> in the <a href=\"https:\/\/codex.wordpress.org\/Administration_Screens\">Administration Screen<\/a>, or Add Media button on the Post\/Page Editor.<\/p>\n<p>If a media file is uploaded within the edit screen, it will be automatically attached to the current post being created or edited. If it is uploaded via the Media&#8217;s Add New Screen or the Media Library Screen, it will be unattached, but may become attached to a post when it is inserted into a post later on.<\/p>\n<h3>Retrieving attachment ID or image ID<\/h3>\n<p>To retrieve the attachment ID, use <code><a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/get_posts\/\">get_posts()<\/a><\/code> or <code><a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/get_children\/\">get_children()<\/a><\/code> function. This example retrieves the all attachments of the current post and getting all metadata of attachment by specifying the ID.<\/p>\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php line-numbers\">\/\/ Insert into the Loop\n$args = array(\n\u00a0\u00a0 \u00a0'post_parent'\u00a0\u00a0\u00a0 =&gt; get_the_ID(),\n\u00a0\u00a0 \u00a0'post_type'\u00a0\u00a0\u00a0\u00a0\u00a0 =&gt; 'attachment',\n);\n$attachments = get_posts( $args );\nif ( $attachments ) {\n\u00a0\u00a0 \u00a0foreach ( $attachments as $attachment ) {\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0$meta_data = wp_get_attachment_metadata( $attachment-&gt;ID, false );\n\u00a0\u00a0 \u00a0}\n}<\/code><\/pre>\n<p>If you want to retrieve images from the post ID only, specify post_mime_type as image.<\/p>\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php line-numbers\">$args = array(\n\u00a0\u00a0 \u00a0'post_parent'\u00a0\u00a0\u00a0 =&gt; get_the_ID(),\n\u00a0\u00a0 \u00a0'post_type'\u00a0\u00a0\u00a0\u00a0\u00a0 =&gt; 'attachment',\n\u00a0\u00a0 \u00a0'post_mime_type' =&gt; 'image',\n);<\/code><\/pre>\n<h4>References<\/h4>\n<ul>\n<li><code><a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/get_posts\/\">get_posts()<\/a><\/code><\/li>\n<li><code><a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/get_children\/\">get_children()<\/a><\/code><\/li>\n<li><code><a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/wp_get_attachment_metadata\/\">wp_get_attachment_metadata()<\/a><\/code><\/li>\n<\/ul>\n<h2>Special considerations<\/h2>\n<h3>Compatible media formats<\/h3>\n<p>In the Media Library, you can upload any file (with the network administrator&#8217;s unfiltered_upload) and not just images or videos but text files, office documents or even binary files. Single site administrators do not have the unfiltered_upload capability by default and requires that definition to be set for the capability to kick in. Audio and Video files are processed by the internal library <code>MediaElement.js<\/code>. <\/p>\n<ul>\n<li><a href=\"https:\/\/developer.wordpress.org\/?post_type=theme-handbook&amp;p=25145#supported-audio-format\">Supported Audio format<\/a><\/li>\n<li><a href=\"https:\/\/developer.wordpress.org\/themes\/functionality\/media\/video\/#supported-video-format\">Supported Video format<\/a><\/li>\n<\/ul>\n<h3>Troubleshooting:<\/h3>\n<h4>Cannot retrieve attachment<\/h4>\n<p>When you cannot get your attached media by <code><a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/get_posts\/\">get_posts()<\/a><\/code> or <code><a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/get_children\/\">get_children()<\/a><\/code> function, confirm your media is really attached to the post.<br \/>\nFrom the <a href=\"https:\/\/codex.wordpress.org\/Administration_Screens\">Administration Screen<\/a>, Click <strong>Media &gt; Library<\/strong> to open the Media Library and confirm the value in &#8220;Uploaded to&#8221; column of the media.<\/p>\n","protected":false},"author":12560283,"featured_media":0,"parent":5819,"menu_order":0,"template":"","meta":{"_crdt_document":"","footnotes":""},"class_list":["post-11360","theme-handbook","type-theme-handbook","status-publish","hentry","type-handbook"],"revision_note":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/developer.wordpress.org\/wp-json\/wp\/v2\/theme-handbook\/11360","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/developer.wordpress.org\/wp-json\/wp\/v2\/theme-handbook"}],"about":[{"href":"https:\/\/developer.wordpress.org\/wp-json\/wp\/v2\/types\/theme-handbook"}],"author":[{"embeddable":true,"href":"https:\/\/developer.wordpress.org\/wp-json\/wp\/v2\/users\/12560283"}],"version-history":[{"count":46,"href":"https:\/\/developer.wordpress.org\/wp-json\/wp\/v2\/theme-handbook\/11360\/revisions"}],"predecessor-version":[{"id":25161,"href":"https:\/\/developer.wordpress.org\/wp-json\/wp\/v2\/theme-handbook\/11360\/revisions\/25161"}],"up":[{"embeddable":true,"href":"https:\/\/developer.wordpress.org\/wp-json\/wp\/v2\/theme-handbook\/5819"}],"wp:attachment":[{"href":"https:\/\/developer.wordpress.org\/wp-json\/wp\/v2\/media?parent=11360"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}