<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>WP Engineer &#187; WordPress Hacks</title>
	<atom:link href="http://wpengineer.com/tag/wordpress-hacks/feed/" rel="self" type="application/rss+xml" />
	<link>http://wpengineer.com</link>
	<description>WordPress News, Hacks, Tips, Tutorials, Plugins and Themes</description>
	<lastBuildDate>Mon, 21 May 2012 22:48:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>First or Last Page in Page-Structures of WordPress</title>
		<link>http://wpengineer.com/2440/first-or-last-page-in-page-structures-of-wordpress/</link>
		<comments>http://wpengineer.com/2440/first-or-last-page-in-page-structures-of-wordpress/#comments</comments>
		<pubDate>Tue, 08 May 2012 10:16:26 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
				<category><![CDATA[WordPress Hacks]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[WP]]></category>

		<guid isPermaLink="false">http://wpengineer.com/?p=2440</guid>
		<description><![CDATA[Now and then little snippets are pretty useful. For a fix in a Premium-Theme, I needed a kind of evaluation, where I am in the site structure and with little effort I was able to expand the classes and react with CSS. The following code shows the basic for it and get_pages() is the key [...]]]></description>
			<content:encoded><![CDATA[<p>Now and then little snippets are pretty useful. For a fix in a Premium-Theme, I needed a kind of evaluation, where I am in the site structure and with little effort I was able to expand the classes and react with CSS.</p>
<p>The following code shows the basic for it and <code>get_pages()</code> is the key from the core of WordPress to get to these results. This function provides the necessary result of using the parameter and the output via the parameter <code>sort_order</code> provides the sequence and identification of the first page, which is then either the first or last page in this structure.<br />
<span id="more-2440"></span></p>
<pre>
// First post in structure
// simple, but usefull <img src='http://wpengineer.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />
global $post;

$page_childs = get_pages( &#039;child_of=&#039; . $post-&gt;post_parent . &#039;&amp;sort_order=ASC&#039; );
$first_child = $page_childs&#091;0&#093;;

if ( get_the_ID() === (int) $first_child-&gt;ID )
 echo &#039;First Child&#039;;
</pre>
<pre>
// Last post in structure
global $post;

$page_childs = get_pages( &#039;child_of=&#039; . $post-&gt;post_parent . &#039;&amp;sort_order=DESC&#039; );
$last_child  = $page_childs&#091;0&#093;;

if ( get_the_ID() === (int) $last_child-&gt;ID )
 echo &#039;Last Child&#039;;
</pre>
<hr /><a href="http://wpplugins.com/plugin/281/snippets" title="More informations about this plugin for WordPress"><img src="http://wpengineer.com/wp-content/themes/wpe-3/images/snippets-125-125.png" height="90" alt="WordPress Snippet Plugin" /></a> <a href="http://xtreme-theme.com"><img src="http://wpengineer.com/wp-content/uploads/feed-banner-2.jpg" alt="Xtreme One WordPress Framework"/></a><br />
&copy; <a href="http://wpengineer.com/">WP Engineer Team</a>, All rights reserved <small>(Digital Fingerprint: WPEngineer-be0254ce2b4972feb4b9cb72034a092d)</small></p>
]]></content:encoded>
			<wfw:commentRss>http://wpengineer.com/2440/first-or-last-page-in-page-structures-of-wordpress/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Create a Separate Page for Blog Posts in WordPress</title>
		<link>http://wpengineer.com/2030/create-a-separate-page-for-blog-posts-in-wordpress/</link>
		<comments>http://wpengineer.com/2030/create-a-separate-page-for-blog-posts-in-wordpress/#comments</comments>
		<pubDate>Tue, 22 Jun 2010 17:35:34 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[WordPress Hacks]]></category>
		<category><![CDATA[template]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://wpengineer.com/?p=2030</guid>
		<description><![CDATA[WPBeginner wrote a nice post today, how to create a separate page for blog posts. And we appreciate WPBeginners great posts, but before you start to use their solution, here is a 3 second solution to make this work faster, I hope WPBeginner doesn't mind: Create a new file and call it blogpage.php or whatever [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.wpbeginner.com/">WPBeginner</a> wrote <a href="http://www.wpbeginner.com/wp-tutorials/how-to-create-a-separate-page-for-blog-posts-in-wordpress/">a nice post</a> today, how to create a separate page for blog posts. And we appreciate WPBeginners great posts, but before you start to use their solution, here is a 3 second solution to make this work faster, I hope WPBeginner doesn't mind:<br />
<span id="more-2030"></span><br />
Create a new file and call it blogpage.php or whatever you like. Add this code:</p>
<pre lang="php">
&lt;?php
/*
 * Template Name: Blogpage
 */
load_template(TEMPLATEPATH . &#039;/index.php&#039; );
?&gt;
</pre>
<p>Then write a new page, for example Blog, leave the content empty, select the template Blogpage and publish the page. After that, select "static page" in "Reading Options". That's all!</p>
<p>If you work with <strong>WordPress 3.0</strong> already, you can use this code:</p>
<pre lang="php">
&lt;?php
/*
 * Template Name: Blogpage
 */
locate_template( array( &#039;index.php&#039; ), true );
?&gt;
</pre>
<hr /><a href="http://wpplugins.com/plugin/281/snippets" title="More informations about this plugin for WordPress"><img src="http://wpengineer.com/wp-content/themes/wpe-3/images/snippets-125-125.png" height="90" alt="WordPress Snippet Plugin" /></a> <a href="http://xtreme-theme.com"><img src="http://wpengineer.com/wp-content/uploads/feed-banner-2.jpg" alt="Xtreme One WordPress Framework"/></a><br />
&copy; <a href="http://wpengineer.com/">WP Engineer Team</a>, All rights reserved <small>(Digital Fingerprint: WPEngineer-be0254ce2b4972feb4b9cb72034a092d)</small></p>
]]></content:encoded>
			<wfw:commentRss>http://wpengineer.com/2030/create-a-separate-page-for-blog-posts-in-wordpress/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Use WordPress Cron</title>
		<link>http://wpengineer.com/1908/use-wordpress-cron/</link>
		<comments>http://wpengineer.com/1908/use-wordpress-cron/#comments</comments>
		<pubDate>Mon, 14 Dec 2009 07:35:47 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
				<category><![CDATA[WordPress Tutorials]]></category>
		<category><![CDATA[Advent Calendar]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[WordPress Hacks]]></category>
		<category><![CDATA[WP]]></category>

		<guid isPermaLink="false">http://wpengineer.com/?p=1908</guid>
		<description><![CDATA[WordPress has its own cron to automatically and scheduled run certain themes. Therefore WordPress provides several functions to use the cron. In our first example we send every hour a mail with the help of the WordPress function wp_mail(). FYI, this is just a possibility, please don't do it on your system! As default, WordPress [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://wpengineer.com/wp-content/uploads/WordPress-Christmas-14.jpg" alt="WordPress-Christmas-14" title="WordPress-Christmas-14" width="600" height="400" class="aligncenter size-full wp-image-1892" />WordPress has its own cron to automatically and scheduled run certain themes. Therefore WordPress provides <a title="read in codex" href="http://codex.wordpress.org/Function_Reference/wp_schedule_event">several functions</a> to use the cron.</p>
<p>In our first example we send every hour a mail with the help of the WordPress function <code>wp_mail()</code>. FYI, this is just a possibility, please don't do it on your system!</p>
<p>As default, WordPress can handle 3 time keys, which you can call with the function <code>wp_schedule_event</code>.</p>
<pre lang="php">
// send automatic scheduled email
if ( ! wp_next_scheduled(&#039;my_task_hook&#039;) ) {
	wp_schedule_event( time(), &#039;hourly&#039;, &#039;my_task_hook&#039; ); // hourly, daily and twicedaily
}

add_action( &#039;my_task_hook&#039;, &#039;my_task_function&#039; );
function my_task_function() {

	wp_mail(
		&#039;example@yoursite.com&#039;,
		&#039;Automatic mail&#039;,
		&#039;Hello, this is an automatically scheduled email from WordPress.&#039;
	);
}
</pre>
<p>If you use the cron in a Plugin or theme, then don't forget to deactivate the cron if you don't need it anymore.</p>
<pre lang="php">
delete_action( &#039;my_task_hook&#039;, &#039;my_task_deactivate&#039; );
// clean the scheduler
function my_task_deactivate() {

	wp_clear_scheduled_hook( &#039;my_task_hook&#039; );
}
</pre>
<p>But not always are 3 time values enough. Luckily you can expand the control via a filter.</p>
<pre lang="php">
add_filter( &#039;cron_schedules&#039;, &#039;filter_cron_schedules&#039; );
// add custom time to cron
function filter_cron_schedules( $param ) {
	return array( &#039;once_half_hour&#039; =&gt; array(
		&#039;interval&#039; =&gt; 1800, // seconds
		&#039;display&#039;  =&gt; __( &#039;Once Half an Hour&#039; )
	) );
}
</pre>
<hr /><a href="http://wpplugins.com/plugin/281/snippets" title="More informations about this plugin for WordPress"><img src="http://wpengineer.com/wp-content/themes/wpe-3/images/snippets-125-125.png" height="90" alt="WordPress Snippet Plugin" /></a> <a href="http://xtreme-theme.com"><img src="http://wpengineer.com/wp-content/uploads/feed-banner-2.jpg" alt="Xtreme One WordPress Framework"/></a><br />
&copy; <a href="http://wpengineer.com/">WP Engineer Team</a>, All rights reserved <small>(Digital Fingerprint: WPEngineer-be0254ce2b4972feb4b9cb72034a092d)</small></p>
]]></content:encoded>
			<wfw:commentRss>http://wpengineer.com/1908/use-wordpress-cron/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Feed Cache in WordPress</title>
		<link>http://wpengineer.com/1820/feed-cache-in-wordpress/</link>
		<comments>http://wpengineer.com/1820/feed-cache-in-wordpress/#comments</comments>
		<pubDate>Wed, 18 Nov 2009 10:11:53 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
				<category><![CDATA[WordPress Tutorials]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[Constants]]></category>
		<category><![CDATA[Feed]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[RSS]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[WordPress Hacks]]></category>
		<category><![CDATA[WP]]></category>

		<guid isPermaLink="false">http://wpengineer.com/?p=1820</guid>
		<description><![CDATA[WordPress already offers the possibility of own functions to read feeds and use it in your blog. Since version 2.8 of WordPress, a new feature is in use for that. So there are two ways to work with feeds in WordPress, and in both functions, the data is cached. But this is not always wanted, [...]]]></description>
			<content:encoded><![CDATA[<p>WordPress already offers the possibility of own functions to read feeds and use it in your blog. Since version 2.8 of WordPress, a new feature is in use for that. So there are two ways to work with feeds in WordPress, and in both functions, the data is cached. But this is not always wanted, so I show how to take influence on caching of the two functions.<br />
<span id="more-1820"></span></p>
<h3>fetch_feed() <em>Since WordPress 2.8</em></h3>
<p>With version 2.8 of WordPress, they add the class <strong>SimplePie</strong> into the core. SimplePie has always distinguished by a very rapid development and a stable user-friendly class. Before <strong>MagpieRSS</strong> has been used, which in many cases was not available and the development is rather slow. For some time SimplePie is no longer be maintained by the developer, which worried the developer community. But developers from the WordPress team took care of it and keep the class alive. More information can be find on the blog <a href="http://wpdevel.wordpress.com/2009/09/24/suggest-agenda-items-for-oct-1st-dev-cha/">WP Dev Updates</a></p>
<p>Now the class is in the core of WordPress and is capable of generating RSS and ATOM feeds. It uses the cache of WordPress and can be controlled via a hook.</p>
<p>But first a small sample to read a feed with the class in WordPress.</p>
<pre lang="php">
&lt;?php
include_once(ABSPATH . WPINC . &#039;/feed.php&#039;);
$rss = fetch_feed(&#039;http://bueltge.de/feed/&#039;);
$rss_items = $rss-&gt;get_items( 0, $rss-&gt;get_item_quantity(5) );
if ( !$rss_items ) {
    echo &#039;no items&#039;;
} else {
    foreach ( $rss_items as $item ) {
        echo &#039;&lt;p&gt;&lt;a href=&quot;&#039; . $item-&gt;get_permalink() . &#039;&quot;&gt;&#039; . $item-&gt;get_title() . &#039;&lt;/a&gt;&lt;/p&gt;&#039;;
    }
}
?&gt;
</pre>
<p>The small sample reads my feed and returns the last 5 entries.</p>
<p>Essential is the function <code>fetch_feed()</code>. This function has a hook which can make influence on caching - <code>wp_feed_cache_transient_lifetime</code>.<br />
The standard of the cache is set to 12 hours (<code> 43,200 </code>), which is not always the favorite setting for users. The value is operated on the method <code>WP_Feed_Cache_Transient()</code>, parameter <code>$lifetime</code>. Therefore, it is advisable to use the following little query, be it in the Plugin or in the <code>functions.php</code> of the theme.</p>
<pre lang="php">
add_filter( &#039;wp_feed_cache_transient_lifetime&#039;, create_function( &#039;$a&#039;, &#039;return 1800;&#039; ) );
</pre>
<p>In this example, I set the cache to 30 minutes. With the help of <a href="http://codex.wordpress.org/Conditional_Tags">Conditional Tags</a> you can control the whole of it so that the cache-hook is only activated when you click on a page (<code>is_page()</code>) or for example you are in the front end (<code>!is_admin()</code>) of the blog. Here you must simply use the opportunities and adapt to your own needs.</p>
<p>A small possibility I would like to point out more, <strong>available as of WordPress 2.9</strong> (<a href="http://core.trac.wordpress.org/ticket/11117">ticket 11117</a>), to simplify debugging for developers. Thus, the cache is disabled in the environment, if the constant <code>WP_DEBUG</code> is set, which can be important now and then. Setting the constant is usually done in the <code>wp-config.php</code> of the installation, or as alternative in the Plugin.</p>
<pre lang="php">
function do_not_cache_feeds(&amp;$feed) {
	$feed-&gt;enable_cache(false);
}

if ( defined(&#039;WP_DEBUG&#039;) &amp;&amp; WP_DEBUG )
	add_action( &#039;wp_feed_options&#039;, &#039;do_not_cache_feeds&#039; );
</pre>
<h3>fetch_rss()</h3>
<p>Another function is <code>fetch_rss()</code>, which was set with WordPress 2.8 on the suspense list. As an alternative, the above function is active. Current is <code>fetch_rss()</code> still available and is also supported. Therefore, for completeness, also the possibility to influence the cache.</p>
<p>Here an example to read feed and output the last 5 entries of the feed</p>
<pre lang="php">
&lt;?php
include_once(ABSPATH . WPINC . &#039;/rss.php&#039;);
$rss = fetch_rss(&#039;http://bueltge.de/feed/&#039;);
$rss_items = array_slice($rss-&gt;items, 0, 5);
if ( empty($rss_items) ) {
    echo &#039;no items&#039;;
} else {
    foreach ( $rss_items as $item ) {
        echo &#039;&lt;p&gt;&lt;a href=&quot;&#039; . $item&#091;&#039;link&#039;&#093; . &#039;&quot;&gt;&#039; . $item&#091;&#039;title&#039;&#093; . &#039;&lt;/a&gt;&lt;/p&gt;&#039;;
    }
}
?&gt;
</pre>
<p>Here, the function <code>fetch_rss()</code> is used for reading and the cache can be changed via the constants. This must defined either directly in <code>wp-config.php</code> where they will apply globally to all applications, or you need to integrate them into its own functions.</p>
<pre lang="php">
define(&#039;MAGPIE_CACHE_ON&#039;, 0); // deactivate cache
define(&#039;MAGPIE_CACHE_AGE&#039;, 60*60) // Cache 1 hour in seconds
</pre>
<h3>Conclusion</h3>
<p>Since WordPress decided not to support MagpieRSS any longer in the future, you should only deal with SimplePie and not with the second possibility.<br />
<hr /><a href="http://wpplugins.com/plugin/281/snippets" title="More informations about this plugin for WordPress"><img src="http://wpengineer.com/wp-content/themes/wpe-3/images/snippets-125-125.png" height="90" alt="WordPress Snippet Plugin" /></a> <a href="http://xtreme-theme.com"><img src="http://wpengineer.com/wp-content/uploads/feed-banner-2.jpg" alt="Xtreme One WordPress Framework"/></a><br />
&copy; <a href="http://wpengineer.com/">WP Engineer Team</a>, All rights reserved <small>(Digital Fingerprint: WPEngineer-be0254ce2b4972feb4b9cb72034a092d)</small></p>
]]></content:encoded>
			<wfw:commentRss>http://wpengineer.com/1820/feed-cache-in-wordpress/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Easier And Better Solutions To Get Pictures On Your Posts</title>
		<link>http://wpengineer.com/1735/easier-better-solutions-to-get-pictures-on-your-posts/</link>
		<comments>http://wpengineer.com/1735/easier-better-solutions-to-get-pictures-on-your-posts/#comments</comments>
		<pubDate>Tue, 20 Oct 2009 11:04:24 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
				<category><![CDATA[WordPress Tutorials]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Theme]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[WordPress Hacks]]></category>
		<category><![CDATA[WordPress Themes]]></category>
		<category><![CDATA[WP]]></category>

		<guid isPermaLink="false">http://wpengineer.com/?p=1735</guid>
		<description><![CDATA[WordPress makes it pretty easy to upload picture on posts and pages. Thereby are various data stored, which can be used. Again and again, you can read tutorials on how to bring photos to posts, normally this is a solution via the custom fields. But there is a different and simpler solution, I think. The [...]]]></description>
			<content:encoded><![CDATA[<p>WordPress makes it pretty easy to upload picture on posts and pages. Thereby are various data stored, which can be used.<br />
Again and again, you can read tutorials on how to bring photos to posts, normally this is a solution via the custom fields. But there is a different and simpler solution, I think. The maintenance of custom fields is not always necessary.<br />
Therefore, I would like to show some solutions that are, in my opinion, much better and can be expanded in many ways.</p>
<p><span id="more-1735"></span></p>
<h3>With custom fields</h3>
<p>In the first case, the solution using a custom field to be displayed, while the data must be maintained in this field to a key.</p>
<p>In version 1 you store the value of the authors to the key <code>author link</code>, so it's easy to understand the use of the fields. More information about this can be found in the <a href="http://codex.wordpress.org/Using_Custom_Fields">Codex</a>.</p>
<h4>Version 1</h4>
<p><img src="http://wpengineer.com/wp-content/uploads/wp-cf.png" alt="wp-cf" title="wp-cf" width="450" height="209" class="aligncenter size-full wp-image-1736" /><br />
The output of the key is handed over by using the template tag <code>get_post_meta()</code>. The function expects the Id of the post and the key value as a mandatory, or alternatively you can set if a value or arrays are allowed. Put on <code>true</code> and it returns only a string</p>
<pre lang="php">
/**
 * @param int $post_id Post ID.
 * @param string $key The meta key to retrieve.
 * @param bool $single Whether to return a single value.
 * @return mixed Will be an array if $single is false. Will be value of meta data field if $single
 *  is true.
 */
function get_post_meta($post_id, $key, $single = false)
</pre>
<pre lang="php">
&lt;?php $key = &#039;authorlink&#039;; ?&gt;
&lt;p&gt;Photo of &lt;a href=&quot;&lt;?php echo get_post_meta($post-&gt;ID, $key, true);?&gt;&quot;&gt;&lt;/a&gt;.&lt;/p&gt;
</pre>
<p>The next step is basically no different, except that we now put the address in a value to another key, where the picture is. This requires the author to know address and save the URL.<br />
<img src="http://wpengineer.com/wp-content/uploads/wp-cf2.png" alt="wp-cf2" title="wp-cf2" width="450" height="207" class="aligncenter size-full wp-image-1737" /></p>
<p>In version 2, however, we go one step further and put a function into the <code>functions.php</code>, which takes care of it and then we can feed it with data. Again, this is just an example and needs in this version the key of the custom fields, the width and height of the images. These three sample values, we then write in the tag to output the image.</p>
<h4>Version 2</h4>
<pre lang="php">
/*Custom Field Images*/
function image_attachment($key, $width, $height) {
	global $post;

	$custom_field = get_post_meta($post-&gt;ID, $key, true);
	if ($custom_field) {
		echo &#039;&lt;img src=&quot;&#039; . $custom_field . &#039;&quot; alt=&quot;Post Image&quot; width=&quot;&#039; . $width . &#039;&quot; height=&quot;&#039; . $height . &#039;&quot; /&gt;&#039;;
	} else {
		return;
	}
}
</pre>
<p>The output of the template is determined by the above function as follows.</p>
<pre lang="php">
&lt;?php
$image_key = &#039;image&#039;;
$myimage = get_post_meta($post-&gt;ID, $image_key, true);
if ($myimage) { ?&gt;
	&lt;div class=&quot;post-image&quot;&gt;
		&lt;?php image_attachment($image_key, 512, 200); ?&gt;
	&lt;/div&gt;
&lt;?php } ?&gt;
</pre>
<p>This really can make some beautiful things. Many theme authors would like to just have a picture to a post, which is directly taken at various points. Often this is achieved through the above solution. However, this requires that the authors need to know the custom fields and maintain them, so the key of the custom field and URL to the image.</p>
<p>Not very comfortable, so I want to show a solution, how you can get the image that was uploaded to the post, which can be found in the Gallery of the post.</p>
<p><img src="http://wpengineer.com/wp-content/uploads/wp-images.png" alt="wp-images" title="wp-images" width="450" height="346" class="aligncenter size-full" /></p>
<h3>WordPress own functions for images</h3>
<p>One of the many options is <code>wp_get_attachment_image()</code>, which represents in my view, the simplest solution. Here is an example of what explains it in more detail.</p>
<p>At first we just get all pictures, not all the attachments of the posts that has been uploaded directly to the post. These images, we can directly display. The syntax must always be in the loop.</p>
<pre lang="php">
&lt;?php
$attachments = get_children( array(&#039;post_parent&#039; =&gt; get_the_ID(), &#039;post_type&#039; =&gt; &#039;attachment&#039;, &#039;post_mime_type&#039; =&gt; &#039;image&#039;) );
foreach ( $attachments as $attachment_id =&gt; $attachment ) {
	echo wp_get_attachment_image($attachment_id);
} ?&gt;
</pre>
<p>The above example loads via the loop all images and display them. We don't take care about the actual size of the image, in default it will display the thumbnail. By setting <code>post_mime_type</code> = <code>image</code>, it only fetches images and not other attachments of the post.<br />
The function of WordPress, which outputs the image with HTML, requieres the ID of the attachment, so you have to retrieve the ID to the post beforehand, we'll do with <code>get_children()</code>.</p>
<p>A few words now to function <code>wp_get_attachment_image()</code> of WP to display the images. As already mentioned, there are several functions that all have a similar structure, and either give back other markup or allow other parameters. For a simple output, this function is best. It is also described in the <a href="http://codex.wordpress.org/Function_Reference/wp_get_attachment_image"> Codex</a>.</p>
<p>The function allows for 3 parameters:</p>
<pre lang="php">
/**
 * Get an HTML img element representing an image attachment
 *
 * @param int $attachment_id Image attachment ID.
 * @param string $size Optional, default is &#039;thumbnail&#039;.
 * @param bool $icon Optional, default is false. Whether it is an icon.
 * @return string HTML img element or empty string on failure.
 */
function wp_get_attachment_image($attachment_id, $size = &#039;thumbnail&#039;, $icon = false)
</pre>
<p>You get the markup back for the img-Tag, like the following example.</p>
<pre lang="php">
&lt;mg width=&quot;150&quot; height=&quot;150&quot; src=&quot;http://example.com/wp-content/uploads/2009/08/DSC00261-150x150.jpg&quot;; class=&quot;attachment-thumbnail2 alt=&quot;Image Example&quot; title=&quot;Example Image&quot; /&gt;
</pre>
<p>The last of the three parameters is not really interesting in association with pictures, since it displays an icon. But the second parameter is there even more exciting, because this allows to set the size of the image. Here you can give either one of the preconfigured sizes, the sizes which you can configure in the settings of WordPress and are created when you upload using the media library. The following options are available.</p>
<ul>
<li>Default-Values: <code>thumbnail</code>, <code>medium</code>, <code>large</code> oder <code>full</code></li>
<li>Custom sizes via Array: array( width, height ) Example: <code>array(100, 100)</code></li>
</ul>
<p>If you pass your own values, then WordPress fetches the appropriate image in dependence of these values. So if you set, for example, 100x100 and the thumbnails are 150px150px, then these images are drawn. If you have your own array of 200x200, it will output the medium image because the thumbnail is too small.</p>
<pre lang="php">
&lt;?php
$attachments = get_children( array(&#039;post_parent&#039; =&gt; get_the_ID(), &#039;post_type&#039; =&gt; &#039;attachment&#039;, &#039;post_mime_type&#039; =&gt;&#039;image&#039;) );
foreach ( $attachments as $attachment_id =&gt; $attachment ) {
	echo wp_get_attachment_image( $attachment_id, array(200, 250) );
} ?&gt;
</pre>
<h4>Images with links</h4>
<p>Now I will not always just the pictures, but also have the image with a link to the image in original size. In that case there is the function <code>wp_get_attachment_link()</code>. Here an example:</p>
<pre lang="php">
&lt;?php
$attachments = get_children( array(&#039;post_parent&#039; =&gt; get_the_ID(), &#039;post_type&#039; =&gt; &#039;attachment&#039;, &#039;post_mime_type&#039; =&gt;&#039;image&#039;) );
foreach ( $attachments as $attachment_id =&gt; $attachment ) {
	echo wp_get_attachment_link( $attachment_id, &#039;medium&#039; );
} ?&gt;
</pre>
<p>The HTML looks like this.</p>
<pre lang="php">
&lt;a href=&quot;http://example.com/wp-content/uploads/2009/08/DSC00261.JPG&quot; title=&quot;Example Image&quot;&gt;&lt;img src=&quot;http://example.com/wp-content/uploads/2009/08/DSC00261-150x150.jpg&quot; class=&quot;attachment-thumbnail&quot; alt=&quot;Image Example&quot; title=&quot;Example Image&quot; height=&quot;150&quot; width=&quot;150&quot;/&gt;&lt;/a&gt;
</pre>
<p>Thus the image in the requested size is generated, and the link was set to the image in original size.</p>
<h3>Determine size of the images</h3>
<p>Sometimes you want to know in advance how big is the image. There is a function that returns the values as an array. This is an example of the output:</p>
<pre lang="php">
&lt;?php
$attachments = get_children( array(&#039;post_parent&#039; =&gt; get_the_ID(), &#039;post_type&#039; =&gt; &#039;attachment&#039;, &#039;post_mime_type&#039; =&gt; &#039;image&#039;) );
foreach ( $attachments as $attachment_id =&gt; $attachment ) {
	$src = wp_get_attachment_image_src( $attachment_id, &#039;full&#039; );
	var_dump($src);
} ?&gt;
</pre>
<p>This simple loop to output the values returns an array.</p>
<pre lang="php">
array
  0 =&gt; string &#039;http://example.com/wp-content/uploads/2009/08/DSC00261.JPG&#039; (length=63)
  1 =&gt; int 1632
  2 =&gt; int 1224
  3 =&gt; boolean false
</pre>
<p>The order of the array is allocated as follows.</p>
<ul>
<li>$src[0] => url</li>
<li>$src[1] => width</li>
<li>$src[2] => height</li>
<li>$src[3] => icon</li>
</ul>
<p>Thus, it can react to the size, depending on requirements. Alternatively, you can work with the function <code>image_get_intermediate_size($post_id, $size='thumbnail')</code>, which returns some more values. A sample of the output for 'medium':</p>
<pre lang="php">
array
  &#039;file&#039; =&gt; string &#039;DSC00261-300x225.jpg&#039; (length=20)
  &#039;width&#039; =&gt; string &#039;300&#039; (length=3)
  &#039;height&#039; =&gt; string &#039;225&#039; (length=3)
  &#039;path&#039; =&gt; string &#039;2009/08/example-300x225.jpg&#039; (length=28)
  &#039;url&#039; =&gt; string &#039;http://example.com/wp-content/uploads/2009/08/DSC00261-300x225.jpg&#039; (length=71)
</pre>
<h3>Further functions and possibilities</h3>
<p>In this context, there are a few other functions that enable the work with attachments or pictures. I do not want to explain each of them, therefore I constribute a small loop for testing purposes. This covers many possibilities and are self explanatory, I think.</p>
<pre lang="php">
&lt;?php
$attachments = get_children( array(&#039;post_parent&#039; =&gt; get_the_ID(), &#039;post_type&#039; =&gt; &#039;attachment&#039;, &#039;post_mime_type&#039; =&gt; &#039;image&#039;) );
foreach ( $attachments as $attachment_id =&gt; $attachment ) {
	echo &#039;&lt;p&gt;&#039;;
	echo &#039;wp_get_attachment_image: &#039; . wp_get_attachment_image( $attachment_id, array(200,250) ) . &#039;&lt;br /&gt;&#039;;
	echo &#039;wp_get_attachment_link: &#039; . wp_get_attachment_link( $attachment_id ) . &#039;&lt;br /&gt;&#039;;
	echo &#039;wp_get_attachment_url: &#039; . wp_get_attachment_url( $attachment_id ) . &#039;&lt;br /&gt;&#039;;
	echo &#039;wp_get_attachment_thumb_url: &#039; . wp_get_attachment_thumb_url( $attachment_id ) . &#039;&lt;br /&gt;&#039;;
	echo &#039;get_attachment_link: &#039; . get_attachment_link( $attachment_id ) . &#039;&lt;br /&gt;&#039;;
	$src = image_get_intermediate_size( $attachment_id, &#039;medium&#039;	 );
	echo &#039;image_get_intermediate_size. &#039;; var_dump($src); echo &#039;&lt;br /&gt;&#039;;
	$src = wp_get_attachment_image_src( $attachment_id, &#039;full&#039;, true );
	echo &#039;wp_get_attachment_image_src. &#039;; var_dump($src); echo &#039;&lt;br /&gt;&#039;;
	echo &#039;Title of attachment: &#039; . apply_filters( &#039;the_title&#039;, $attachment-&gt;post_title ) . &#039;&lt;br /&gt;&#039;;
	echo &#039;Link to post: &#039; . get_permalink($image-&gt;post_parent) . &#039;&lt;br /&gt;&#039;;
	echo &#039;&lt;hr style=&quot;clear:both;&quot; /&gt;&lt;/p&gt;&#039;;
} ?&gt;
</pre>
<p>Remember, the syntax must be in the loop of WordPress.</p>
<h4>Only one picture to post</h4>
<p>Now I have explained how to get the pictures but in general you will probably not need to post all the pictures, but you want just one picture. This occurs, for example, in magazine themes, where a small picture is displayed to make the post more interesting. Through these functions I can give the author of the respective post the control of that picture. Normally I do this in such a way that it explicitly output the first picture from the gallery to the post. Thus, the author can move the image in the gallery by dragging and dropping to the right place.</p>
<p><img src="http://wpengineer.com/wp-content/uploads/wp-gallery-reihenfolge.png" alt="wp-gallery-reihenfolge" title="wp-gallery-reihenfolge" width="450" height="375" class="aligncenter size-full wp-image-1739" /></p>
<p>This is not an own function, you just have to feed <code>get_children()</code> accordingly. The output of the image, and other values to the image is again as described above.</p>
<p>In our first example we pick <strong>only the first image</strong> from the gallery.</p>
<pre lang="php">
&lt;?php
$attachments = get_children( array(
				&#039;post_parent&#039;    =&gt; get_the_ID(),
				&#039;post_type&#039;      =&gt; &#039;attachment&#039;,
				&#039;numberposts&#039;    =&gt; 1, // show all -1
				&#039;post_status&#039;    =&gt; &#039;inherit&#039;,
				&#039;post_mime_type&#039; =&gt; &#039;image&#039;,
				&#039;order&#039;          =&gt; &#039;ASC&#039;,
				&#039;orderby&#039;        =&gt; &#039;menu_order ASC&#039;
				) );
foreach ( $attachments as $attachment_id =&gt; $attachment ) {
	echo wp_get_attachment_image( $attachment_id );
} ?&amp;&gt;
</pre>
<p>Now the author of the contribution can always choose the first image of what he wants to use.</p>
<p>Alternatively we can turn the tables and get the <strong> last image from the Gallery </strong>.</p>
<pre lang="php">
&lt;?php
$attachments = get_children( array(
				&#039;post_parent&#039;    =&gt; get_the_ID(),
				&#039;post_type&#039;      =&gt; &#039;attachment&#039;,
				&#039;numberposts&#039;    =&gt; 1, // show all -1
				&#039;post_status&#039;    =&gt; &#039;inherit&#039;,
				&#039;post_mime_type&#039; =&gt; &#039;image&#039;,
				&#039;order&#039;          =&gt; &#039;DESC&#039;,
				&#039;orderby&#039;        =&gt;&#039;menu_order ASC&#039;
				) );
foreach ( $attachments as $attachment_id =&gt; $attachment ) {
	echo wp_get_attachment_image( $attachment_id );
} ?&gt;
</pre>
<p>With the help of numberposts, we specify how many images are to be fetched. So we fetch in the following example the <strong>first 2 pics</strong> and after the <strong>last 2 pictures</strong>.</p>
<pre lang="php">
&lt;?php
$attachments = get_children( array(
				&#039;post_parent&#039;    =&gt; get_the_ID(),
				&#039;post_type&#039;      =&gt; &#039;attachment&#039;,
				&#039;numberposts&#039;    =&gt; 1, // show all -1
				&#039;post_status&#039;    =&gt; &#039;inherit&#039;,
				&#039;post_mime_type&#039; =&gt; &#039;image&#039;,
				&#039;order&#039;          =&gt; &#039;ASC&#039;,
				&#039;orderby&#039;        =&gt; &#039;menu_order ASC&#039;
				) );
foreach ( $attachments as $attachment_id =&gt; $attachment ) {
	echo wp_get_attachment_image( $attachment_id );
} ?&gt;
</pre>
<p>Well, the <strong>last two pictures</strong>, starting with the last picture.</p>
<pre lang="php">
&lt;?php
$attachments = get_children( array(
				&#039;post_parent&#039;    =&gt; get_the_ID(),
				&#039;post_type&#039;      =&gt; &#039;attachment&#039;,
				&#039;numberposts&#039;    =&gt; 2, // show all -1
				&#039;post_status&#039;    =&gt; &#039;inherit&#039;,
				&#039;post_mime_type&#039; =&gt; &#039;image&#039;,
				&#039;order&#039;          =&gt; &#039;DESC&#039;,
				&#039;orderby&#039;        =&gt; &#039;menu_order ASC&#039;
				) );
foreach ( $attachments as $attachment_id =&gt; $attachment ) {
	echo wp_get_attachment_image( $attachment_id );
} ?&gt;
</pre>
<h3>Meta-Data of images</h3>
<p>WordPress saves various meta data to the images. Now and then these are very useful, and why not simply use them.</p>
<p>First an example to get to the data. This we do with the function <code>wp_get_attachment_metadata()</code>. This function returns quite a lot of data and we can access the size of images, their path and also access the meta data.</p>
<pre lang="php">
&lt;?php
$attachments = get_children( array(
				&#039;post_parent&#039;    =&gt; get_the_ID(),
				&#039;post_type&#039;      =&gt; &#039;attachment&#039;,
				&#039;numberposts&#039;    =&gt; 1, // show all -1
				&#039;post_status&#039;    =&gt; &#039;inherit&#039;,
				&#039;post_mime_type&#039; =&gt; &#039;image&#039;,
				&#039;order&#039;          =&gt; &#039;ASC&#039;,
				&#039;orderby&#039;        =&gt; &#039;menu_order ASC&#039;
				) );
foreach ( $attachments as $attachment_id =&gt;; $attachment ) {
	echo wp_get_attachment_image( $attachment_id );
	$imagemeta = wp_get_attachment_metadata( $attachment_id );
	var_dump($imagemeta); // list values in array

	$aperture          = $imagemeta&#091;&#039;image_meta&#039;&#093;&#091;&#039;aperture&#039;&#093;;
	$credit            = $imagemeta&#091;&#039;image_meta&#039;&#093;&#091;&#039;credit&#039;&#093;;
	$camera            = $imagemeta&#091;&#039;image_meta&#039;&#093;&#091;&#039;camera&#039;&#093;;
	$caption           = $imagemeta&#091;&#039;image_meta&#039;&#093;&#091;&#039;caption&#039;&#093;;
	$created_timestamp = $imagemeta&#091;&#039;image_meta&#039;&#093;&#091;&#039;created_timestamp&#039;&#093;;
	$copyright         = $imagemeta&#091;&#039;image_meta&#039;&#093;&#091;&#039;copyright&#039;&#093;;
	$focal_length      = $imagemeta&#091;&#039;image_meta&#039;&#093;&#091;&#039;focal_length&#039;&#093;;
	$iso               = $imagemeta&#091;&#039;image_meta&#039;&#093;&#091;&#039;iso&#039;&#093;;
	$shutter_speed     = $imagemeta&#091;&#039;image_meta&#039;&#093;&#091;&#039;shutter_speed&#039;&#093;;
	$title             = $imagemeta&#091;&#039;image_meta&#039;&#093;&#091;&#039;title&#039;&#093;;
} ?&gt;
</pre>
<p>In the above code is the output value of a <code>var_dump()</code> in it. Thus we see very quickly which content in the array exists and where they can be accessed.<br />
All values of the meta data, I have written in each variable, so that even less experienced users should understand it.</p>
<p>For a simple picture, of what would be created via cell phone camera, for example, the array looks like this.</p>
<pre lang="php">
array
  &#039;width&#039; =&gt; string &#039;1632&#039; (length=4)
  &#039;height&#039; =&gt; string &#039;1224&#039; (length=4)
  &#039;hwstring_small&#039; =&gt; string &#039;height=&#039;96&#039; width=&#039;128&#039;&#039; (length=23)
  &#039;file&#039; =&gt; string &#039;2009/08/DSC00261.JPG&#039; (length=20)
  &#039;sizes&#039; =&gt;
    array
      &#039;thumbnail&#039; =&gt;
        array
          &#039;file&#039; =&gt; string &#039;DSC00261-150x150.jpg&#039; (length=20)
          &#039;width&#039; =&gt; string &#039;150&#039; (length=3)
          &#039;height&#039; =&gt; string &#039;150&#039; (length=3)
      &#039;medium&#039; =&gt;
        array
          &#039;file&#039; =&gt; string &#039;DSC00261-300x225.jpg&#039; (length=20)
          &#039;width&#039; =&gt; string &#039;300&#039; (length=3)
          &#039;height&#039; =&gt; string &#039;225&#039; (length=3)
      &#039;large&#039; =&gt;
        array
          &#039;file&#039; =&gt; string &#039;DSC00261-1024x768.jpg&#039; (length=21)
          &#039;width&#039; =&gt; string &#039;1024&#039; (length=4)
          &#039;height&#039; =&gt; string &#039;768&#039; (length=3)
  &#039;image_meta&#039; =&gt;
    array
      &#039;aperture&#039; =&gt; string &#039;2.8&#039; (length=3)
      &#039;credit&#039; =&gt; string &#039;&#039; (length=0)
      &#039;camera&#039; =&gt; string &#039;W800i&#039; (length=5)
      &#039;caption&#039; =&gt; string &#039;&#039; (length=0)
      &#039;created_timestamp&#039; =&gt; string &#039;1184253323&#039; (length=10)
      &#039;copyright&#039; =&gt; string &#039;&#039; (length=0)
      &#039;focal_length&#039; =&gt; string &#039;0&#039; (length=1)
      &#039;iso&#039; =&gt; string &#039;100&#039; (length=3)
      &#039;shutter_speed&#039; =&gt; string &#039;0.0166666666667&#039; (length=15)
      &#039;title&#039; =&gt; string &#039;&#039; (length=0)
</pre>
<h3>Alternative in WordPress 2.9</h3>
<p>Using WordPress version 2.9, there is a template tag just for this request, so it is easier and more understandable. You just use the <code>the_post_image()</code> and you can set the size. Default is <code>thumbnail</code>, it is possible <code>thumbnail</code>, <code>medium</code>, <code>large</code> or <code>full</code>.</p>
<pre lang="php">
the_post_image(); // without parameter -&gt; Thumbnail
the_post_image(&#039;thumbnail&#039;); // Thumbnail
the_post_image(&#039;medium&#039;); // Medium resolution - use full,
</pre>
<p>As a small alternative is still <code>get_the_post_image()</code> available, whereas the function doesn't have echo, and can optionally have the ID of the post.<br />
<code>get_the_post_image( $size = 'thumbnail', $post_id = NULL )</code></p>
<h3>Conclusion</h3>
<p>WordPress offers a lot of different functions to access attachments, whether pictures or other files. Also a part of meta-data is stored, which can be used. Many approaches can be found in the code, primarily in the <code>wp-includes/media.php</code>. Perhaps I could point out some solutions which in my view a better alternative than the use of custom fields, or even scanning the content.<br />
<hr /><a href="http://wpplugins.com/plugin/281/snippets" title="More informations about this plugin for WordPress"><img src="http://wpengineer.com/wp-content/themes/wpe-3/images/snippets-125-125.png" height="90" alt="WordPress Snippet Plugin" /></a> <a href="http://xtreme-theme.com"><img src="http://wpengineer.com/wp-content/uploads/feed-banner-2.jpg" alt="Xtreme One WordPress Framework"/></a><br />
&copy; <a href="http://wpengineer.com/">WP Engineer Team</a>, All rights reserved <small>(Digital Fingerprint: WPEngineer-be0254ce2b4972feb4b9cb72034a092d)</small></p>
]]></content:encoded>
			<wfw:commentRss>http://wpengineer.com/1735/easier-better-solutions-to-get-pictures-on-your-posts/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Deactivate WordPress Default Widgets</title>
		<link>http://wpengineer.com/1650/deactivate-wordpress-default-widgets/</link>
		<comments>http://wpengineer.com/1650/deactivate-wordpress-default-widgets/#comments</comments>
		<pubDate>Wed, 02 Sep 2009 20:54:30 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
				<category><![CDATA[WordPress Themes]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Widget]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[WordPress Hacks]]></category>

		<guid isPermaLink="false">http://wpengineer.com/?p=1650</guid>
		<description><![CDATA[You do not always want to have all the widgets active, which comes within the WordPress core. You can disable the unneccessary Widgets in your functions.php of your theme with a small function. The following syntax will switch off all the standard widgets. It should therefore be adjusted depending on your requirements: // unregister all [...]]]></description>
			<content:encoded><![CDATA[<p>You do not always want to have all the widgets active, which comes within the WordPress core.</p>
<p>You can disable the unneccessary Widgets in your <code>functions.php</code> of your theme with a small function. The following syntax will switch off all the standard widgets. It should therefore be adjusted depending on your requirements:</p>
<pre lang="php" >
// unregister all default WP Widgets
function unregister_default_wp_widgets() {

	unregister_widget(&#039;WP_Widget_Pages&#039;);

	unregister_widget(&#039;WP_Widget_Calendar&#039;);

	unregister_widget(&#039;WP_Widget_Archives&#039;);

	unregister_widget(&#039;WP_Widget_Links&#039;);

	unregister_widget(&#039;WP_Widget_Meta&#039;);

	unregister_widget(&#039;WP_Widget_Search&#039;);

	unregister_widget(&#039;WP_Widget_Text&#039;);

	unregister_widget(&#039;WP_Widget_Categories&#039;);

	unregister_widget(&#039;WP_Widget_Recent_Posts&#039;);

	unregister_widget(&#039;WP_Widget_Recent_Comments&#039;);

	unregister_widget(&#039;WP_Widget_RSS&#039;);

	unregister_widget(&#039;WP_Widget_Tag_Cloud&#039;);
}

add_action(&#039;widgets_init&#039;, &#039;unregister_default_wp_widgets&#039;, 1);
</pre>
<p>The function <code>unregister_widget()</code> needs as parameters the class, therefore the code is working since version 2.8 only. In prior versions, Widgets were supported differently.</p>
<p>How to create your own Widgets since WordPress Version 2.8 is explained in <a href="http://wpengineer.com/wordpress-built-a-widget/">Build A WordPress 2.8 Widget With The New Widget API</a>.<br />
<hr /><a href="http://wpplugins.com/plugin/281/snippets" title="More informations about this plugin for WordPress"><img src="http://wpengineer.com/wp-content/themes/wpe-3/images/snippets-125-125.png" height="90" alt="WordPress Snippet Plugin" /></a> <a href="http://xtreme-theme.com"><img src="http://wpengineer.com/wp-content/uploads/feed-banner-2.jpg" alt="Xtreme One WordPress Framework"/></a><br />
&copy; <a href="http://wpengineer.com/">WP Engineer Team</a>, All rights reserved <small>(Digital Fingerprint: WPEngineer-be0254ce2b4972feb4b9cb72034a092d)</small></p>
]]></content:encoded>
			<wfw:commentRss>http://wpengineer.com/1650/deactivate-wordpress-default-widgets/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Use WordPress Cache</title>
		<link>http://wpengineer.com/1607/use-wordpress-cache/</link>
		<comments>http://wpengineer.com/1607/use-wordpress-cache/#comments</comments>
		<pubDate>Fri, 21 Aug 2009 10:19:24 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
				<category><![CDATA[WordPress Tutorials]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Plugin]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[WordPress Hacks]]></category>
		<category><![CDATA[WP]]></category>

		<guid isPermaLink="false">http://wpengineer.com/?p=1607</guid>
		<description><![CDATA[WordPress has an internal cache, also for extensions can be used. There are various functions available and you don't have to create something new, you can easily use the cache functionality of WordPress. To get to know and understand the features a little bit, I use a small example, therefore I cache in the following [...]]]></description>
			<content:encoded><![CDATA[<p>WordPress has an internal cache, also for extensions can be used. There are various functions available and you don't have to create something new, you can easily use the cache functionality of WordPress.<br />
To get to know and understand the features a little bit, I use a small example, therefore I cache in the following tutorial a feed, which should be displayed in the frontend.</p>
<p>All functions of <a href="http://codex.wordpress.org/Function_Reference/WP_Cache">cache are in the Codex</a> by WordPress listed, so a look at the Codex is worthwhile if you deal with the syntax.</p>
<p>The first cache solution came with WordPress 2.3 and was file based. The cache was optional and had some parameters to configure.<br />
You were able to activate via following constant: <code>define ( 'ENABLE_CACHE', true); </code><br />
<span id="more-1607"></span><br />
The biggest improvement happened in version 2.6, in which the cache has changed to an object-oriented solution. Therefore the opportunities for cache usage are lying rather on the server and not explicitly on WordPress. This was mainly realized in order to maximize the resources of the server and not to be handed over to WordPress. With this introduction, the cache of WordPress has no longer explicitly be activated, it is always active. Therefore, it is important that the server has a certain minimum amount of RAM available, WordPress requires 32 MByte - but that is not always the case, for example, when updating the core, it contains a call which defines the RAM to 128MByte, which in many cases is not available and therefore the update does not work.<br />
But this is not the topic of this post today, because I want to explain how to use the cache in your own extensions. So back to the syntax and I just start with the key functions to realize a small example.</p>
<p>All functions can be found in <code>wp-includes/cache.php</code>, or alternatively in <a href="http://codex.wordpress.org/Function_Reference/WP_Cache">Codex</a>.</p>
<p>To reset the cache, insofar there is no data for this key, you can use the following function.</p>
<pre lang="php">
/**
 * @param int|string $key The cache ID to use for retrieval later
 * @param mixed $data The data to add to the cache store
 * @param string $flag The group to add the cache to
 * @param int $expire When the cache data should be expired
 */
wp_cache_add($key, $data, $flag = &#039;&#039;, $expire = 0)
</pre>
<p>To delete cache data for a key, here is the opposite.</p>
<pre lang="php">
/**
 * @param int|string $id What the contents in the cache are called
 * @param string $flag Where the cache contents are grouped
 * @return bool True on successful removal, false on failure
 */
wp_cache_delete($id, $flag = &#039;&#039;)
</pre>
<p>Fetching data for a key is done by using:</p>
<pre lang="php">
/**
 * @param int|string $id What the contents in the cache are called
 * @param string $flag Where the cache contents are grouped
 * @return bool|mixed False on failure to retrieve contents or the cache
 */
wp_cache_get($id, $flag = &#039;&#039;)
</pre>
<p>Should within the cache to a key the content to be replaced, then the following function will work.</p>
<pre lang="php">
/**
 * @param int|string $id What to call the contents in the cache
 * @param mixed $data The contents to store in the cache
 * @param string $flag Where to group the cache contents
 * @param int $expire When to expire the cache contents
 * @return bool False if cache ID and group already exists, true on success
 */
wp_cache_replace($key, $data, $flag = &#039;&#039;, $expire = 0)
</pre>
<p>But now a small example, which caches the feed. The feed gets loaded by <code>fetch_rss()</code>, a function of WordPress which is available since version 1.5.</p>
<pre lang="php">
$mycache = wp_cache_get( &#039;mycache&#039; ); // fetch data from cache to the key &quot;mycache&quot;
if ($mycache == false) { // if no data, then
	$mycache = fetch_rss(&quot;http://mycache.com/feed/&quot;); // parse feed
	wp_cache_set( &#039;mycache&#039;, $mycache ); // save feed content to key &quot;mycache&quot;
}
var_dump( $mycache ); // display content
</pre>
<p>FYI: You get an insight into the cache of WordPress easily via the variable <code>$wp_object_cache</code> or using the Plugin <a href="http://bueltge.de/debug-objects-wordpress-plugin/966/">Debug Objects </a> or <a href="http://bueltge.de/wordpress-cache-steuern-plugin/819/">WP Cache Inspect</a>; whereas Debug Objects explicitly has been made for this and should be used in development environments only.<br />
<hr /><a href="http://wpplugins.com/plugin/281/snippets" title="More informations about this plugin for WordPress"><img src="http://wpengineer.com/wp-content/themes/wpe-3/images/snippets-125-125.png" height="90" alt="WordPress Snippet Plugin" /></a> <a href="http://xtreme-theme.com"><img src="http://wpengineer.com/wp-content/uploads/feed-banner-2.jpg" alt="Xtreme One WordPress Framework"/></a><br />
&copy; <a href="http://wpengineer.com/">WP Engineer Team</a>, All rights reserved <small>(Digital Fingerprint: WPEngineer-be0254ce2b4972feb4b9cb72034a092d)</small></p>
]]></content:encoded>
			<wfw:commentRss>http://wpengineer.com/1607/use-wordpress-cache/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Change WordPress Mail Sender</title>
		<link>http://wpengineer.com/1604/change-wordpress-mail-sender/</link>
		<comments>http://wpengineer.com/1604/change-wordpress-mail-sender/#comments</comments>
		<pubDate>Wed, 19 Aug 2009 10:55:04 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
				<category><![CDATA[WordPress Plugins]]></category>
		<category><![CDATA[hack]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Plugin]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[WordPress Hacks]]></category>
		<category><![CDATA[WordPress Tutorials]]></category>

		<guid isPermaLink="false">http://wpengineer.com/?p=1604</guid>
		<description><![CDATA[WordPress makes it easy and fast to add new users in the backend. Since version 2.8 of WordPress, it can send the access information via email. A nice feature, with no additional settings to change the sender of this email. For example the email should be send from the administrator instead from WordPress. Nevertheless, there [...]]]></description>
			<content:encoded><![CDATA[<p>WordPress makes it easy and fast to add new users in the backend. Since version 2.8 of WordPress, it can send the access information via email. A nice feature, with no additional settings to change the sender of this email. For example the email should be send from the administrator instead from WordPress.</p>
<p>Nevertheless, there is a possibility and with the help of two hooks, the sender can be changed. I created a small Plugin, where you can easy and simple adjust the sender name and sender email.<br />
<span id="more-1604"></span>-<br />
The Plugin is available in the following source code and has no options for the backend and doesn't leave any data in the database. Anyone who wants can simply extend the Plugin.</p>
<pre lang="php">
&lt;?php
/**
 * @package WP Mail From
 * @author Frank B&uuml;ltge
 * @version 0.1
 */

/*
Plugin Name: WP Mail From
Plugin URI: http://bueltge.de/
Description: Change the default address that WordPress sends it&rsquo;s email from.
Version: 0.1
Author: Frank B&uuml;ltge
Author URI: http://bueltge.de/
Last Change: 11.08.2009 08:41:06
*/

if ( !function_exists(&#039;add_action&#039;) ) {
	header(&#039;Status: 403 Forbidden&#039;);
	header(&#039;HTTP/1.1 403 Forbidden&#039;);
	exit();
}

if ( !class_exists(&#039;wp_mail_from&#039;) ) {
	class wp_mail_from {

		function wp_mail_from() {
			add_filter( &#039;wp_mail_from&#039;, array(&amp;$this, &#039;fb_mail_from&#039;) );
			add_filter( &#039;wp_mail_from_name&#039;, array(&amp;$this, &#039;fb_mail_from_name&#039;) );
		}

		// new name
		function fb_mail_from_name() {
			$name = &#039;My Blog is my Blog&#039;;
			// alternative the name of the blog
			// $name = get_option(&#039;blogname&#039;);
			$name = esc_attr($name);
			return $name;
		}

		// new email-adress
		function fb_mail_from() {
			$email = &#039;info@example.com&#039;;
			$email = is_email($email);
			return $email;
		}

	}

	$wp_mail_from = new wp_mail_from();
}
?&gt;</pre>
<p>The values for name and email have to be maintain in each of the associated function. After that, the two values are getting examined, but is not necessarily needed.</p>
<p>As a note: the function <code>esc_attr()</code> is only since version 2.8 available and replaces the function <code>attribute_escape()</code>. Should the solution be used in an earlier version, then you have to change the function.</p>
<p>For suggestions and improvements, I am grateful, as always. You can use this Plugin to improve WordPress a bit and the user is not surprised about the sender "WordPress".<br />
<hr /><a href="http://wpplugins.com/plugin/281/snippets" title="More informations about this plugin for WordPress"><img src="http://wpengineer.com/wp-content/themes/wpe-3/images/snippets-125-125.png" height="90" alt="WordPress Snippet Plugin" /></a> <a href="http://xtreme-theme.com"><img src="http://wpengineer.com/wp-content/uploads/feed-banner-2.jpg" alt="Xtreme One WordPress Framework"/></a><br />
&copy; <a href="http://wpengineer.com/">WP Engineer Team</a>, All rights reserved <small>(Digital Fingerprint: WPEngineer-be0254ce2b4972feb4b9cb72034a092d)</small></p>
]]></content:encoded>
			<wfw:commentRss>http://wpengineer.com/1604/change-wordpress-mail-sender/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Warning Message For Self Customized Plugins</title>
		<link>http://wpengineer.com/1486/warning-message-for-self-customized-plugins/</link>
		<comments>http://wpengineer.com/1486/warning-message-for-self-customized-plugins/#comments</comments>
		<pubDate>Mon, 20 Jul 2009 14:25:36 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
				<category><![CDATA[WordPress Plugins]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Plugin]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[WordPress Hacks]]></category>
		<category><![CDATA[WordPress Tutorials]]></category>

		<guid isPermaLink="false">http://wpengineer.com/?p=1486</guid>
		<description><![CDATA[Now and then you have to modify an existing Plugin for your own special needs. But you still get a message if an update exists for the original Plugin. If you modified the Plugin several month ago and you forgot about it, so you update the Plugin and all your modifications are gone. Most of [...]]]></description>
			<content:encoded><![CDATA[<p>Now and then you have to modify an existing Plugin for your own special needs. But you still get a message if an update exists for the original Plugin. If you modified the Plugin several month ago and you forgot about it, so you update the Plugin and all your modifications are gone. Most of the time you have a backup, but even though it's annoying to upload the customized Plugin again after you realized the original Plugin is not exactly what you want. Therefore you can paste a little function in the specified Plugin to display a warning right after the update message. So you won't forget, that you modified this Plugin a while ago.</p>
<p><a href="http://wpengineer.com/wp-content/uploads/plugin-modified-warning.jpg"><img src="http://wpengineer.com/wp-content/uploads/plugin-modified-warning-small.jpg" alt="plugin-modified-warning-small" title="plugin-modified-warning-small" width="500" height="180" class="aligncenter size-full wp-image-1498" /></a></p>
<pre lang="php">
function my_update_notice() {
	$info = __( &#039;ATTENTION! Plugin was modified.&#039;, MY_TEXTDOMAIN );
	echo &#039;&lt;span class=&quot;spam&quot;&gt;&#039; . strip_tags( $info, &#039;&lt;br&gt;&lt;a&gt;&lt;b&gt;&lt;i&gt;&lt;span&gt;&#039; ) . &#039;&lt;/span&gt;&#039;;
}

if ( is_admin() )
	add_action( &#039;in_plugin_update_message-&#039; . plugin_basename(__FILE__), &#039;my_update_notice&#039; );
</pre>
<hr /><a href="http://wpplugins.com/plugin/281/snippets" title="More informations about this plugin for WordPress"><img src="http://wpengineer.com/wp-content/themes/wpe-3/images/snippets-125-125.png" height="90" alt="WordPress Snippet Plugin" /></a> <a href="http://xtreme-theme.com"><img src="http://wpengineer.com/wp-content/uploads/feed-banner-2.jpg" alt="Xtreme One WordPress Framework"/></a><br />
&copy; <a href="http://wpengineer.com/">WP Engineer Team</a>, All rights reserved <small>(Digital Fingerprint: WPEngineer-be0254ce2b4972feb4b9cb72034a092d)</small></p>
]]></content:encoded>
			<wfw:commentRss>http://wpengineer.com/1486/warning-message-for-self-customized-plugins/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Recents Drafts All Authors</title>
		<link>http://wpengineer.com/1343/recents-drafts-all-authors/</link>
		<comments>http://wpengineer.com/1343/recents-drafts-all-authors/#comments</comments>
		<pubDate>Fri, 26 Jun 2009 11:36:20 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
				<category><![CDATA[WordPress Plugins]]></category>
		<category><![CDATA[Feed]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Plugin]]></category>
		<category><![CDATA[Widget]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[WordPress Hacks]]></category>
		<category><![CDATA[WP]]></category>

		<guid isPermaLink="false">http://wpengineer.com/?p=1343</guid>
		<description><![CDATA[It's not uncommon that a blog runs by different authors, so it may be useful if you can have a quick look at the drafts of all authors. In our joint blog WP Engineer we created a feed, which keeps us up to date if a new draft of all authors were created. The work [...]]]></description>
			<content:encoded><![CDATA[<p>It's not uncommon that a blog runs by different authors, so it may be useful if you can have a quick look at the drafts of all authors. In our joint blog WP Engineer we created a feed, which keeps us up to date if a new draft of all authors were created.</p>
<p>The work of every author is different and the dashboard is the center of information. Therefore we have decided to supplement a widget in the Dashboard, which shows the last five drafts of all authors. I enhanced the existing Plugin <a href="http://wpengineer.com/feed-for-drafts-plugin/">Draft Feed</a>.</p>
<ul>
<li>It provides a feed of all drafts</li>
<li>And the last 5 drafts of all authors in the Dashboard.</li>
</ul>
<p><span id="more-1343"></span><br />
<img src="http://wpengineer.com/wp-content/uploads/recents_draft.png" alt="recents_draft" title="recents_draft" width="362" height="225" class="aligncenter size-full wp-image-1344" /></p>
<p>Everybody who needs something like this, can download this Plugin from my <a href="http://bueltge.de/wordpress-feed-fuer-entwuerfe/829/">German Blog</a>. It doesn't have any options and doesn't exist in SVN of WordPress - if you want you can use and expand it or leave a comment if you have a question. Enjoy!<br />
<hr /><a href="http://wpplugins.com/plugin/281/snippets" title="More informations about this plugin for WordPress"><img src="http://wpengineer.com/wp-content/themes/wpe-3/images/snippets-125-125.png" height="90" alt="WordPress Snippet Plugin" /></a> <a href="http://xtreme-theme.com"><img src="http://wpengineer.com/wp-content/uploads/feed-banner-2.jpg" alt="Xtreme One WordPress Framework"/></a><br />
&copy; <a href="http://wpengineer.com/">WP Engineer Team</a>, All rights reserved <small>(Digital Fingerprint: WPEngineer-be0254ce2b4972feb4b9cb72034a092d)</small></p>
]]></content:encoded>
			<wfw:commentRss>http://wpengineer.com/1343/recents-drafts-all-authors/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

