<?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>Konstantin Kovshenin &#187; snippets</title>
	<atom:link href="http://kovshenin.com/tag/snippets/feed/" rel="self" type="application/rss+xml" />
	<link>http://kovshenin.com</link>
	<description>WordPress, Automattic and Open Source</description>
	<lastBuildDate>Mon, 21 May 2012 15:59:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>How to: Disable HTTP Calls in WordPress</title>
		<link>http://kovshenin.com/2012/how-to-disable-http-calls-in-wordpress/</link>
		<comments>http://kovshenin.com/2012/how-to-disable-http-calls-in-wordpress/#comments</comments>
		<pubDate>Thu, 05 Apr 2012 09:41:18 +0000</pubDate>
		<dc:creator>Konstantin Kovshenin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[snippets]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://kovshenin.com/?p=5488</guid>
		<description><![CDATA[Sometimes you&#8217;re travelling on a plane or on a boat, in the metro or on the road. Sometimes you just have a crappy Internet connection, and you&#8217;ll notice that a local WordPress install (mostly admin pages) takes several seconds to load. That could create a big problem if you&#8217;re developing a theme or plugin, and [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes you&#8217;re travelling on a plane or on a boat, in the metro or on the road. Sometimes you just have a crappy Internet connection, and you&#8217;ll notice that a local WordPress install (mostly admin pages) takes several seconds to load. That could create a big problem if you&#8217;re developing a theme or plugin, and have to refresh eighty times per minute.</p>
<p>WordPress uses its HTTP API (the WP_Http class and wrapper functions) to look for plugin, themes and core updates, fetch RSS feeds or talk to third party API (such as a Twitter widget plugin) and with no Internet connection, calls to such functions will timeout in a few seconds, thus the long page load time. A simple solution is to simply disable all HTTP calls and luckily, WordPress allows us to do that in a single filter for our theme (functions.php) or plugin file:</p>
<pre>add_filter( 'pre_http_request', '__return_true', 100 );</pre>
<p>The smarter way would be to intercept the request and let it pass or block it based on the request URL, that way you&#8217;ll be able to work with local APIs. Yet another benefit of using the <a href="http://codex.wordpress.org/HTTP_API">WordPress HTTP API</a> and not <code>file_get_contents</code> or <code>curl_exec</code> :)</p>
<p><strong>Update</strong>: As Dion Hulse <a href="https://twitter.com/#!/dd32/status/187838815828852736">mentioned</a> on Twitter, you can also use <code>WP_HTTP_BLOCK_EXTERNAL</code> in your config file to achieve the same thing, but in a more &#8220;cron friendly&#8221; way. More information about this can be found somewhere around <a href="http://core.trac.wordpress.org/browser/tags/3.3.1/wp-includes/class-http.php#L473">this line</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://kovshenin.com/2012/how-to-disable-http-calls-in-wordpress/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Native Image Sizing &#8220;On the Fly&#8221; with WordPress</title>
		<link>http://kovshenin.com/2012/native-image-sizing-on-the-fly-with-wordpress/</link>
		<comments>http://kovshenin.com/2012/native-image-sizing-on-the-fly-with-wordpress/#comments</comments>
		<pubDate>Tue, 06 Mar 2012 07:06:49 +0000</pubDate>
		<dc:creator>Konstantin Kovshenin</dc:creator>
				<category><![CDATA[WordPress]]></category>
		<category><![CDATA[shortcodes]]></category>
		<category><![CDATA[snippets]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://kovshenin.com/?p=5181</guid>
		<description><![CDATA[I tweeted it out not too long ago, and it seems to have gotten people&#8217;s attention. So why do WordPress theme developers still use TimThumb? One of the valid reasons seems to be dynamic image sizing. Of course if you&#8217;re using TimThumb for post thumbnails in your theme, your life saver is the add_image_size function &#8212; [...]]]></description>
			<content:encoded><![CDATA[<p>I <a href="http://kovshenin.com/2012/timthumb-in-wordpress-themes/">tweeted</a> it out not too long ago, and it seems to have gotten people&#8217;s attention. So why do WordPress theme developers still use TimThumb? One of the valid reasons seems to be dynamic image sizing. Of course if you&#8217;re using TimThumb for post thumbnails in your theme, your life saver is the <a href="http://codex.wordpress.org/Function_Reference/add_image_size">add_image_size</a> function &#8212; you don&#8217;t need &#8220;dynamic resizing&#8221; since all your thumbnails are of equal sizes throughout your theme.</p>
<p>However, the valid reason I seem to have bumped in, is the fact that sometimes you would want to use a specific size for an image, but only once, for one post or page. The add_image_size function here will add a lot of overhead, crunching <span style="color: #333333;"><strong>all the uploaded images</strong> into the size you want to use only once. Like a shortcode that can resize and crop an image from your media library to whatever dimensions you tell it to:</span></p>
<pre>[image src="http://example.org/wp-content/uploads/2012/03/image.png" width="150" height="150"]</pre>
<p>Right, a shortcode (argh!) to dynamically resize an image. Fair enough, a lot of premium themes seem to have something similar, but mostly use TimThumb because it&#8217;s so simple. However, I&#8217;ve created <a href="https://gist.github.com/1984363">an image shortcode</a> function that resizes and crops images from the media library on the fly using the <a href="http://codex.wordpress.org/Function_Reference/image_make_intermediate_size">image_make_intermediate_size</a> function available as early as WordPress 2.5!</p>
<p>It saves the resized image on disk, and returns the absolute URL to that image. Of course you&#8217;d want to add things like captions, alt tags and so on, but the basic idea is there. Why is this better than TimThumb?</p>
<ul>
<li><span style="line-height: 21px;">It stores the image files on disk and uses absolute URLs to those images, meaning you won&#8217;t have problems hosting your thumbnails on a CDN.</span></li>
<li><span style="line-height: 21px;">It uses functions native to WordPress and works with the WordPress media library, so extra files are deleted when an attachment is deleted &#8212; no more junk in your cache/folder.</span></li>
<li><span style="line-height: 21px;">It doesn&#8217;t have a zero-day vulnerability :)</span></li>
</ul>
<p>There are drawbacks too, like the fact that it&#8217;s a shortcode! Although I&#8217;m pretty sure we can use the same technique and hook it directly to the content, bypassing the shortcodes madness. Oh, and it doesn&#8217;t support remote files too, only the ones in your media library, but that&#8217;s a good thing, as we have all figured out by now.</p>
<p>Feel free to fork <a href="https://gist.github.com/1984363">the Gist on Github</a> and experiment with the code, I&#8217;m sure you can think of how to make it better, easier to use and faster. Thoughts and comments welcome, and thank you for stopping by!</p>
]]></content:encoded>
			<wfw:commentRss>http://kovshenin.com/2012/native-image-sizing-on-the-fly-with-wordpress/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>How to Get the Current URL in WordPress</title>
		<link>http://kovshenin.com/2012/current-url-in-wordpress/</link>
		<comments>http://kovshenin.com/2012/current-url-in-wordpress/#comments</comments>
		<pubDate>Sun, 08 Jan 2012 21:23:59 +0000</pubDate>
		<dc:creator>Konstantin Kovshenin</dc:creator>
				<category><![CDATA[WordPress]]></category>
		<category><![CDATA[snippets]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://kovshenin.com/?p=3754</guid>
		<description><![CDATA[Here&#8217;s a quick tip! I was wandering around the web for the perfect solution to retrieve the current URL in a WordPress theme or plugin. I found a bunch of solutions for PHP, but not directly related to WordPress so I thought there has to be an easier way, and after a few hours of examining [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a <strong>quick tip</strong>! I was wandering around the web for the perfect solution to retrieve the current URL in a WordPress theme or plugin. I found a bunch of solutions for PHP, but not directly related to WordPress so I thought there has to be an easier way, and after a few hours of examining with global variables seems like I found it.</p>
<pre>global $wp;
$current_url = add_query_arg( $wp-&gt;query_string, '', home_url( $wp-&gt;request ) );</pre>
<p>Beats all the <code>$_SERVER</code> approaches and no need to identify whether the host is using SSL, etc. You can add a trailing slash to that with <a href="http://codex.wordpress.org/Function_Reference/trailingslashit">trailingslashit</a> if you need to, and it looks much cleaner too. Hope you enjoy that, and thanks for tweeting ;)</p>
]]></content:encoded>
			<wfw:commentRss>http://kovshenin.com/2012/current-url-in-wordpress/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Change the year of all posts in a particular category to 2012</title>
		<link>http://kovshenin.com/2011/change-year-of-posts-in-wordpress-category/</link>
		<comments>http://kovshenin.com/2011/change-year-of-posts-in-wordpress-category/#comments</comments>
		<pubDate>Thu, 29 Dec 2011 23:38:22 +0000</pubDate>
		<dc:creator>Konstantin Kovshenin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[snippets]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://kovshenin.com/?p=3729</guid>
		<description><![CDATA[Snippet! Change the year of all posts in a particular category to 2012 with a single SQL query (use with phpMyAdmin or the MySQL command line interface) UPDATE wp_posts AS p JOIN wp_term_relationships AS tr ON tr.object_id = p.id JOIN wp_term_taxonomy AS tt ON tt.term_taxonomy_id = tr.term_taxonomy_id JOIN wp_terms AS t ON tt.term_id = t.term_id SET [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Snippet!</strong> Change the year of all posts in a particular category to 2012 with a single SQL query (use with phpMyAdmin or the MySQL command line interface)</p>
<pre>UPDATE wp_posts AS p
  JOIN wp_term_relationships AS tr ON tr.object_id = p.id
  JOIN wp_term_taxonomy AS tt ON tt.term_taxonomy_id = tr.term_taxonomy_id
  JOIN wp_terms AS t ON tt.term_id = t.term_id
  SET p.post_date = REPLACE(p.post_date, YEAR(p.post_date), 2012)
  WHERE t.slug = 'my-category-slug' AND tt.taxonomy = 'category';</pre>
<p>Obviously replace <strong>my-category-slug</strong> with the category you&#8217;d like to target and perform the same magic on the <strong>p.post_date_gmt</strong> field. Not entirely sure why you would use such a technique, but it was asked by Jim Edwards <a href="https://twitter.com/#!/jamesedwards/status/152519852903497730">on Twitter</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://kovshenin.com/2011/change-year-of-posts-in-wordpress-category/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Varnish and Preview Posts in WordPress</title>
		<link>http://kovshenin.com/2011/varnish-and-preview-posts-in-wordpress/</link>
		<comments>http://kovshenin.com/2011/varnish-and-preview-posts-in-wordpress/#comments</comments>
		<pubDate>Thu, 24 Nov 2011 17:47:33 +0000</pubDate>
		<dc:creator>Konstantin Kovshenin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[snippets]]></category>
		<category><![CDATA[varnish]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://kovshenin.com/?p=3682</guid>
		<description><![CDATA[I wrote earlier that I started playing around with Varnish here on my site and that post has a little snippet that strips all incoming and outgoing cookies (except the admin of course.) Today I stumbled on a problem where I had no permission to preview a post I was drafting in WordPress and they [...]]]></description>
			<content:encoded><![CDATA[<p>I <a href="http://kovshenin.com/2011/my-website-is-now-super-fast-with-varnish/">wrote earlier</a> that I started playing around with Varnish here on my site and that post has a little snippet that strips all incoming and outgoing cookies (except the admin of course.) Today I stumbled on a problem where I had no permission to preview a post I was drafting in WordPress and they all turned 404&#8242;s when I tried to.</p>
<p>I first thought Varnish was stripping out the preview query string but I was wrong, the problem was that WordPress new I was logged in and editing the post when I were in the admin panel, but when I tried to preview it on the front end Varnish was stripping out my login cookies, hence it didn&#8217;t display my draft post.</p>
<p>Here&#8217;s a snippet for <strong>vcl_recv</strong> and <strong>vcl_fetch</strong> that should go <strong>before</strong> the unset cookies statement which passes the request to the web server if <code>preview=true</code> is found in the request URL.</p>
<pre>if (req.url ~ "preview=true") {
	return(pass);
}</pre>
<p>Restart the Varnish service and voila! Your cookies aren&#8217;t stripped out anymore and you can now preview your posts and pages. Do note though that if somebody manually types in the preview query string in the browser, they&#8217;ll by-pass Varnish as well.</p>
]]></content:encoded>
			<wfw:commentRss>http://kovshenin.com/2011/varnish-and-preview-posts-in-wordpress/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Attachments Filename and Directory in WordPress</title>
		<link>http://kovshenin.com/2011/attachments-filename-and-directory-in-wordpress/</link>
		<comments>http://kovshenin.com/2011/attachments-filename-and-directory-in-wordpress/#comments</comments>
		<pubDate>Mon, 21 Nov 2011 15:05:21 +0000</pubDate>
		<dc:creator>Konstantin Kovshenin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[attachments]]></category>
		<category><![CDATA[snippets]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://kovshenin.com/?p=3676</guid>
		<description><![CDATA[I was trying to figure out how to get the absolute directory of an attachment post in WordPress. Turns out there&#8217;s no easy function that can give you one, but there is one called wp_upload_dir which will give you an array of the upload directories and URLs. So here&#8217;s the secret sauce: $url = wp_get_attachment_url( $post_ID [...]]]></description>
			<content:encoded><![CDATA[<p>I was trying to figure out how to get the absolute directory of an attachment post in WordPress. Turns out there&#8217;s no easy function that can give you one, but there is one called <a href="http://codex.wordpress.org/Function_Reference/wp_upload_dir">wp_upload_dir</a> which will give you an array of the upload directories and URLs. So here&#8217;s the secret sauce:</p>
<pre>$url = wp_get_attachment_url( $post_ID );
$uploads = wp_upload_dir();
$file_path = str_replace( $uploads['baseurl'], $uploads['basedir'], $url );</pre>
<p>The <code>$file_path</code> variable will now contain an absolute path (filesystem path, not URL) to the location of your attachment file. You can then use it when sending attachments with <code>wp_mail</code> or perhaps when serving the file dynamically. Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://kovshenin.com/2011/attachments-filename-and-directory-in-wordpress/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Cleaner Titles in WordPress</title>
		<link>http://kovshenin.com/2011/cleaner-titles-in-wordpress/</link>
		<comments>http://kovshenin.com/2011/cleaner-titles-in-wordpress/#comments</comments>
		<pubDate>Thu, 03 Nov 2011 14:49:00 +0000</pubDate>
		<dc:creator>Konstantin Kovshenin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[snippets]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://kovshenin.com/?p=3643</guid>
		<description><![CDATA[Quick Tip! What happens when your post title does not fit on one line? It wraps to the second line. What if it&#8217;s only one word? And what if that word is &#8220;it&#8221; or some other shorty? Your title won&#8217;t look too nice, eh? Here&#8217;s a solution, somewhere inside the loop: $title = the_title( '&#60;h2&#62;', [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Quick Tip!</strong> What happens when your post title does not fit on one line? It wraps to the second line. What if it&#8217;s only one word? And what if that word is &#8220;it&#8221; or some other shorty? Your title won&#8217;t look too nice, eh? Here&#8217;s a solution, somewhere inside the loop:</p>
<pre>$title = the_title( '&lt;h2&gt;', '&lt;/h2&gt;', false );
if ( strlen( $title ) &gt; 0 )
    echo substr_replace( $title, '&amp;nbsp;', strrpos( $title, ' ' ), 1 );</pre>
<p>This will look at the last space character in the title and replace it with a non-breaking space character meaning that the last word will always stick to the one before it, hence if the title wraps it&#8217;ll be at least two words. Let&#8217;s hope the two words are not &#8220;is it&#8221; :) Anyone know of a CSS solution that can do this?</p>
]]></content:encoded>
			<wfw:commentRss>http://kovshenin.com/2011/cleaner-titles-in-wordpress/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Post Formats in WordPress: Breaking Down Those Quotes</title>
		<link>http://kovshenin.com/2011/post-formats-in-wordpress-breaking-down-those-quotes/</link>
		<comments>http://kovshenin.com/2011/post-formats-in-wordpress-breaking-down-those-quotes/#comments</comments>
		<pubDate>Thu, 03 Nov 2011 14:06:43 +0000</pubDate>
		<dc:creator>Konstantin Kovshenin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[post formats]]></category>
		<category><![CDATA[snippets]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://kovshenin.com/?p=3641</guid>
		<description><![CDATA[Here&#8217;s a function that would grab the contents HTML and parse out the first quote (blockquote element) together with it&#8217;s cite (usually the quote author) and the remaining text that comes after the quote. This is useful for when dealing with the quote post format in WordPress. function get_blockquote() { $dom = new DOMDocument; $dom-&#62;loadHTML( apply_filters( [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a function that would grab the contents HTML and parse out the first quote (blockquote element) together with it&#8217;s cite (usually the quote author) and the remaining text that comes after the quote. This is useful for when dealing with the <strong>quote</strong> post format in WordPress.</p>
<pre>function get_blockquote() {
    $dom = new DOMDocument;
    $dom-&gt;loadHTML( apply_filters( 'the_content', get_the_content( '' ) ) );
    $blockquotes = $dom-&gt;getElementsByTagname( 'blockquote' );

    if ( $blockquotes-&gt;length &gt; 0 ) {

        // First blockquote
        $blockquote = $blockquotes-&gt;item(0);

        $cite = $blockquote-&gt;getElementsByTagName( 'cite' )-&gt;item( 0 );
        $p = $blockquote-&gt;getElementsByTagName( 'p' );

        $cite_content = '';
        if ( $cite &amp;&amp; $p ) {

            // Remove the cite from the paragraph
            foreach ( $p as $paragraph )
                try { $paragraph-&gt;removeChild( $cite ); }
                catch( Exception $e ) {}

            $cite_content = $dom-&gt;saveXML( $cite );
        }

        $blockquote_content = '';
        foreach ( $p as $paragraph ) {
            if ( strlen( trim( $paragraph-&gt;nodeValue ) ) &gt; 0 )
                $blockquote_content .= $dom-&gt;saveXML( $paragraph );
            else
                $paragraph-&gt;parentNode-&gt;removeChild( $paragraph );

        $blockquote-&gt;parentNode-&gt;removeChild( $blockquote );
        $remaining_content = $dom-&gt;saveXML();
    }
    return $blockquote_content; // $cite_content or $remaining_content
}</pre>
<p>As you can see you have an option of returning the quote contents, the author contents or the stuff that&#8217;s not related to the first quote. You can also bundle all three of them in an array if needed. Anyways, as I said, this might come useful when dealing with the quote post format in WordPress.</p>
<p><a href="http://theme.fm/2011/11/peter-chester-on-scaling-wordpress-2860/"><img class="alignnone size-large wp-image-3642" src="http://kovshenin.com/files/2011/11/Screen-Shot-2011-11-03-at-5.59.20-PM-680x209.png" alt="Quote post format in WordPress" width="580" height="178" /></a></p>
<p>We&#8217;ve got a great <a href="http://theme.fm/2011/11/peter-chester-on-scaling-wordpress-2860/">example on Theme.fm</a> and the best part is that you don&#8217;t need to change the way you write quotes and since you&#8217;ve got the quote content, the author and the rest of the content as separate entities, you can style your first (main) quote however you like.</p>
<p>This continues my discussion on <a href="http://theme.fm/2011/10/tumblr-like-post-formats-by-alex-king-and-crowd-favorite-2784/">Tumblr-like Post Formats</a> by Alex King and Crowd Favorite. And obviously the code snippet can be improved, but hey, who needs regular expressions? ;)</p>
]]></content:encoded>
			<wfw:commentRss>http://kovshenin.com/2011/post-formats-in-wordpress-breaking-down-those-quotes/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Encode Entities Inside PRE Tags</title>
		<link>http://kovshenin.com/2011/encode-entities-inside-pre-tags/</link>
		<comments>http://kovshenin.com/2011/encode-entities-inside-pre-tags/#comments</comments>
		<pubDate>Wed, 02 Nov 2011 10:49:13 +0000</pubDate>
		<dc:creator>Konstantin Kovshenin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[snippets]]></category>

		<guid isPermaLink="false">http://kovshenin.com/?p=3636</guid>
		<description><![CDATA[Here&#8217;s a little Python script that searches through a given file for pre tags and encodes anything in between. This is useful for when escaping from syntax highlighting plugins and replacing every occurrence of the code shortcode with a pre tag. import re, sys # First argument is the filename, output is filename.encoded filename = [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a little Python script that searches through a given file for <code>pre</code> tags and encodes anything in between. This is useful for when <a href="http://theme.fm/2011/11/locking-yourself-out-of-the-syntax-highlighter-evolved-plugin-2837/">escaping from syntax highlighting plugins</a> and replacing every occurrence of the code shortcode with a pre tag.</p>
<pre>import re, sys

# First argument is the filename, output is filename.encoded
filename = sys.argv[1]
f = file(filename)
output = open('%s.encoded' % filename, 'w+');

# Read the whole file, fire the regular expressions
contents = f.read()
expr = re.compile(r'&lt;pre&gt;(.*?)&lt;/pre&gt;', re.MULTILINE|re.DOTALL)
matches = expr.findall(contents)

# Loop through each match and replace &lt; &gt; with &amp;lt; and &amp;gt;
for match in matches:
	contents = contents.replace(match, match.replace('&lt;', '&amp;lt;').replace('&gt;', '&amp;gt;'));

# Write output file and close both files
output.write(contents)
output.close()
f.close()</pre>
<p>Most syntax highlighting plugins will encode all entities on the fly for you so when you stop using them your code might break. Also, most highlighting plugins will render your TinyMCE visual editor useless when working with code, and I think it&#8217;s quite common to work with code using the visual editor in WordPress. At least Twenty Ten and Twenty Eleven understand that ;)</p>
<p>However, as seen from the replacement part, I don&#8217;t really encode all entities but rather replace the greater than and less than symbols. It&#8217;s enough for most cases but if you need a real entity encoding you should use the <a href="http://docs.python.org/library/cgi.html#cgi.escape">cgi.escape</a> function which is similar to <code>htmlspecialchars</code> in php.</p>
<p>Feed this script with your database dump and it&#8217;ll create a new file with an .encoded prefix which you can feed back to MySQL. Please note though that this script reads the entier input file which may lead to slow execution, high memory usage and swapping when working with large files. Worked fine on my 30 megabyte database though.</p>
]]></content:encoded>
			<wfw:commentRss>http://kovshenin.com/2011/encode-entities-inside-pre-tags/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How To: List Filters That You Are About to Apply</title>
		<link>http://kovshenin.com/2011/how-to-list-filters-that-you-are-about-to-apply/</link>
		<comments>http://kovshenin.com/2011/how-to-list-filters-that-you-are-about-to-apply/#comments</comments>
		<pubDate>Fri, 06 May 2011 12:56:06 +0000</pubDate>
		<dc:creator>Konstantin Kovshenin</dc:creator>
				<category><![CDATA[WordPress]]></category>
		<category><![CDATA[filters]]></category>
		<category><![CDATA[snippets]]></category>

		<guid isPermaLink="false">http://kovshenin.com/?p=3418</guid>
		<description><![CDATA[I was working on a project where I had to find out all the registered filters that I was about to apply, so here&#8217;s a WordPress quickie for you! I found this out by inspecting the apply_filters function inside the plugin.php core file, and apparently there&#8217;s a $wp_filter global that contains them all in an [...]]]></description>
			<content:encoded><![CDATA[<p>I was working on a project where I had to find out all the registered filters that I was about to apply, so here&#8217;s a WordPress quickie for you! I found this out by inspecting the apply_filters function inside the plugin.php core file, and apparently there&#8217;s a $wp_filter global that contains them all in an associative array where keys are tags.</p>
<p>So by doing a print_r at a certain time in your theme or function, you&#8217;re able to find out the whole list, but you might as well filter to the hook that you&#8217;re looking for. As an example let&#8217;s take &#8220;comment_text&#8221; as the filter tag and list all it&#8217;s callbacks &#8212; i.e. let&#8217;s find out what filters are applied to the comment text before we print it out. Note that this has to run at a point where the filters have already been added, probably before a call to the comment_text() function.</p>
<pre> global $wp_filter;
 print_r( $wp_filter['comment_text'] );</pre>
<p>This may seem pretty useless at first sight, but did you know that the comment_text is passed through wptexturize, convert_chars, make_clickable, convert_smilies and several other filters? I didn&#8217;t, and I used this trick to find out, which lead me to the make_clickable function which I was looking for in the first place ;)</p>
<p>If you&#8217;re wondering what the array keys are in the array that you&#8217;ve printed, those are the priorities which were passed (or defaulted) when calling add_filter.</p>
<p>I hope this turns out to be useful to somebody, so thanks for retweeting!</p>
]]></content:encoded>
			<wfw:commentRss>http://kovshenin.com/2011/how-to-list-filters-that-you-are-about-to-apply/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

