{"id":90,"date":"2013-05-12T23:06:35","date_gmt":"2013-05-13T07:06:35","guid":{"rendered":"http:\/\/nramkumar.org\/tech\/?p=90"},"modified":"2013-05-12T23:06:35","modified_gmt":"2013-05-13T07:06:35","slug":"wordpress-hacking-finding-all-images-in-a-post","status":"publish","type":"post","link":"https:\/\/nramkumar.org\/tech\/blog\/2013\/05\/12\/wordpress-hacking-finding-all-images-in-a-post\/","title":{"rendered":"WordPress Hacking &#8211; Finding all images in a post"},"content":{"rendered":"<p>This is a continuation of the series where I attempt to build a solution to display WordPress posts with attached images such that the post content itself doesn&#8217;t show any of the images and the sidebar instead displays the images. In my previous post I described creating a simple WordPress plugin with an empty widget.<\/p>\n<p>Now it is time to make the widget do something interesting. In my case, I wanted the widget to display something only if it was being shown in the context of a single post. That is, if you&#8217;re viewing a page of posts in the WordPress site, this sidebar widget should not display anything. A bit of searching in the WordPress <a title=\"WordPress Codex\" href=\"http:\/\/codex.wordpress.org\/Developer_Documentation\" target=\"_blank\">Codex<\/a>\u00a0lead me to the <a title=\"is_single\" href=\"http:\/\/codex.wordpress.org\/Function_Reference\/is_single\" target=\"_blank\"><code>is_single<\/code><\/a> function which allows us to distinguish between a single post or page and a page of posts.<\/p>\n<p>The next task was to figure out how to extract the images associated with a post. WordPress function <code><a title=\"get_children\" href=\"http:\/\/codex.wordpress.org\/Function_Reference\/get_children\" target=\"_blank\">get_children<\/a>\u00a0<\/code>allows you to do exactly this. Once we get this list, the widget can then output the appropriate HTML to display the images in the sidebar. Here&#8217;s the widget function that extracts all the images from a single post and displays them in the sidebar widget only for single posts:<\/p>\n<pre>function widget($args) {\r\n    global $post;\r\n\r\n    extract($args);\r\n\r\n    if ( is_single() ) {\r\n        echo $before_widget;\r\n        echo $before_title . 'Snapshots' . $after_title;\r\n        $imgfilter = array(\r\n            'order' =&gt; 'ASC',\r\n            'post_mime_type' =&gt; 'image',\r\n            'post_parent' =&gt; $post-&gt;ID,\r\n            'post_status' =&gt; 'inherit',\r\n            'post_type' =&gt; 'attachment',);\r\n\r\n        $attachments = get_children( $imgfilter );\r\n\r\n        if ( $attachments ) {\r\n            $images = array();\r\n            foreach ( $attachments as $attachment ) {\r\n                $imgUrl = wp_get_attachment_url( $attachment-&gt;ID );\r\n                $isMatch = strpos($imgUrl, '_thumb.jpg');\r\n                if ( $isMatch === false ) {\r\n                    $isMatch = strpos($imgUrl, 'Emoticon');\r\n                }\r\n                if ( $isMatch === false ) {\r\n                    $images[] = $attachment;\r\n                }\r\n            }\r\n\r\n            foreach ( $images as $image ) {\r\n                $imageSrc = wp_get_attachment_image_src( $image-&gt;ID, 'medium' );\r\n                echo '&lt;a href=\"' . wp_get_attachment_url( $image-&gt;ID ) . '\" class=\"cboxElement\"&gt;' . \r\n                     '&lt;img src=\"' . $imageSrc[0] . '\" class=\"colorbox-' . $post-&gt;ID . '\"&gt;&lt;\/a&gt;';\r\n            }\r\n        }\r\n        else {\r\n            echo __('No Images', 'text_domain');\r\n        }\r\n        echo $after_widget;\r\n    }\r\n}<\/pre>\n<p>We first specify a filter for <code>get_children<\/code> that finds the images attached to the current post (note that the current post is available to us in the global <code>$post<\/code>). Once we have this set of attachments we loop through it, do a bit of pre-processing on it and obtain an array of <code>URLs<\/code> that we can use in the <code>&lt;img&gt;<\/code> tags.<\/p>\n<p>In this case, I wanted to make sure that posts that simply showed a <a href=\"http:\/\/www.wordpress.org\/extend\/plugins\/nextgen-gallery\/\" title=\"NextGen Gallery\" target=\"_blank\">NextGen Gallery<\/a> are treated specially and the images appear in the body of the post and not in the sidebar. I accomplished this by filtering out any image <code>URL<\/code> that matches <code>_thumb.jpg<\/code>. Additionally, <a href=\"http:\/\/www.microsoft.com\/en-us\/download\/details.aspx?id=8621\" title=\"Windows Live Writer\" target=\"_blank\">Windows Live Writer<\/a> adds images for any emoticons used in a post. Clearly we shouldn&#8217;t be moving these out of the post and into the sidebar. This is accomplished by filtering out any image that has <code>Emoticon<\/code> in the <code>URL<\/code>. Note that both these filters are excellent &#8220;hacks&#8221; but not fool-proof. They work well enough for the problem I was trying to solve and I used them.<\/p>\n<p>Once we have the filtered set of images, we just loop through them and emit the <code>HTML<\/code> required to get them to show up in the sidebar. Note that anything that you <code>echo<\/code> in the widget method basically gets displayed in the sidebar that has your widget. The code adds the images (at medium size) as clickable links with the same <code>colorbox CSS<\/code> class thus essentially converting the pictures in the post to a clickable slideshow with medium size thumbnails shown in the sidebar. We only have one more step left &#8211; hide the images in the original post content.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is a continuation of the series where I attempt to build a solution to display WordPress posts with attached images such that the post content itself doesn&#8217;t show any of the images and the sidebar instead displays the images. In my previous post I described creating a simple WordPress plugin with an empty widget&#8230;.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,34,33,27],"tags":[36,75,74,72],"class_list":["post-90","post","type-post","status-publish","format-standard","hentry","category-images","category-sidebar","category-widget","category-wordpress","tag-extract-images-in-a-post","tag-sidebar","tag-widget","tag-wordpress"],"_links":{"self":[{"href":"https:\/\/nramkumar.org\/tech\/wp-json\/wp\/v2\/posts\/90","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nramkumar.org\/tech\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nramkumar.org\/tech\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nramkumar.org\/tech\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nramkumar.org\/tech\/wp-json\/wp\/v2\/comments?post=90"}],"version-history":[{"count":5,"href":"https:\/\/nramkumar.org\/tech\/wp-json\/wp\/v2\/posts\/90\/revisions"}],"predecessor-version":[{"id":95,"href":"https:\/\/nramkumar.org\/tech\/wp-json\/wp\/v2\/posts\/90\/revisions\/95"}],"wp:attachment":[{"href":"https:\/\/nramkumar.org\/tech\/wp-json\/wp\/v2\/media?parent=90"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nramkumar.org\/tech\/wp-json\/wp\/v2\/categories?post=90"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nramkumar.org\/tech\/wp-json\/wp\/v2\/tags?post=90"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}