Category Archives: Snippets

Getting Recent Posts Outside Of WordPress

Many sites do use WordPress for blogging but they do not use it as back-end for whole site. (Your site or app may be one of them.) There may be a scenario in which you have to display recent posts from WordPress blog, on a page which is outside of WordPress. No problem. You might know that WordPress is quite flexible, hence you can handle such scenario with a small code. There are two ways to achieve this. You can either take advantage of WordPress loop itself or if you don’t want to deal with WordPress loop then you can use other way also.

If you don’t want WordPress loop

So you don’t want to take advantage of WordPress loop? Then you can use wp_get_recent_posts(). According to WordPress codex:
wp_get_recent_posts() will return a list of posts. Different from get_posts which returns an array of post objects.
How to use it? Just copy paste following code in page where you want to list the Recent Posts
include( 'wp-load.php' );

// Get the last 10 posts
$recent_posts = wp_get_recent_posts( array(
    'numberposts' => 10,
    'post_type'   => 'post',
    'post_status' => 'publish',
) );

// Display them as list
echo '<ul>';
foreach ( $recent_posts as $post ) {
    echo '<li><a href="', get_permalink( $post['ID'] ), '">', $post['post_title'], '</a></li>';
}
echo '</ul>';
You can simply change numberposts parameters value to change the number of posts you want to retrieve. Continue reading Getting Recent Posts Outside Of WordPress

HTML5 Prefetching Api

Note: This post was written in 2012. Link prefetching is still valid, but modern browsers now support more targeted hints like rel="preload", rel="preconnect", and rel="prefetch" — each serving a different purpose. The MDN docs are still the best reference.

Making websites faster is something both developers and browsers are constantly working on. There are plenty of well-known techniques for this — CDNs, minified JavaScript and CSS, image sprites, smart caching headers in .htaccess, and so on. But one lesser-known technique that most developers overlook entirely is link prefetching.

What is link prefetching?

According to MDN:

Link prefetching is a browser mechanism which utilizes browser idle time to download or prefetch documents that the user might visit in the near future. A web page provides a set of prefetching hints to the browser, and after the browser is finished loading the page, it begins silently prefetching specified documents and stores them in its cache. When the user visits one of the prefetched documents, it can be served up quickly out of the browser’s cache.

In simple terms: you hint to the browser which page the user is likely to visit next, and the browser quietly downloads it in the background during idle time. When the user clicks through, the next page loads nearly instantly because it’s already cached.

How to implement it

It’s just a <link> tag with rel="prefetch" or rel="next":

<!-- Prefetch a full page -->
<link rel="prefetch" href="https://ankurm.com/lab/blog/api/using-localhost-for-facebook-app-development/1091/" />

<!-- Prefetch an image -->
<link rel="prefetch" href="https://ankurm.com/wp-content/uploads/2012/03/logo.png" />

Continue reading HTML5 Prefetching Api

Redirecting www To non-www And Vice Versa

Note: This post was written in 2012. The .htaccess rules below still work on Apache servers, but modern setups (NGINX, Caddy, Apache 2.4 with virtual hosts) handle redirects differently. The rules also use http:// — update them to https:// for any SSL-enabled site.

Every domain can be accessed through two different URLs. Take example.com — it’s reachable as both example.com and www.example.com. The same applies to subdomains. From a user’s point of view this doesn’t matter much, but from an SEO standpoint it’s a problem. Having the same content available on two different URLs can be flagged as duplicate content and hurt your search rankings.

Continue reading Redirecting www To non-www And Vice Versa

AddHandler vs AddType : Apache Directive

Note: This post was written in 2012. The core concepts of AddHandler vs AddType are still valid, but modern server setups (NGINX, Apache 2.4+, PHP-FPM) may handle MIME types differently than described here.

Just the other day, while stumbling around the web looking into website branding techniques, I came across something called using custom extensions to brand your website. The topic was interesting enough that I decided to try it myself. My first attempt threw an error — Apache was sending my file straight to the browser as a download instead of executing it server-side. That sent me down a rabbit hole and I landed on something called Apache Directives.

A directive controls how a file is treated by either the client or the server. Once I understood which directive I was supposed to be using, everything started working fine.

AddHandler and AddType are the two directives you’ll deal with most when it comes to file handling. Using the wrong one can break your server or create a security risk.

Continue reading AddHandler vs AddType : Apache Directive

Programmatically Set Proxy Settings In Visual Basic

Note: This guide was written in 2012 for VB .NET and targets the WinINet API (wininet.dll). It still functions on modern Windows, but with important caveats:

  • Scope is application-only — This only affects your own running application’s HTTP requests. It does not change settings for IE, Chrome, Edge, or any other browser running simultaneously.
  • Not a registry edit — Unlike registry-based approaches, this leaves the rest of the system completely untouched, which is exactly what you want for most use cases.
  • For modern .NET apps, consider using HttpClient with an explicit WebProxy instead, which is cleaner and cross-platform.
  • Best use case today: Legacy WinForms apps or anything using the WebBrowser control that relies on WinINet as its HTTP layer.

Working with proxies is a common requirement when you’re developing applications that need to talk to the internet, especially in corporate environments. Often, you want your application to use a specific proxy without affecting the rest of the user’s computer.

If you search the web for how to do this in Visual Basic, you’ll find plenty of tutorials that tell you to modify the Windows Registry. The problem with that approach is that it changes the settings for the entire PC, which usually isn’t what you want. Finding a clean way to set an “application-only” proxy can be surprisingly difficult.

Continue reading Programmatically Set Proxy Settings In Visual Basic

HTML 5 DnD Download: A Quick Implementation

Note: I originally wrote this tutorial in 2012 during the early days of HTML5. Browser APIs have evolved, and the Drag-and-Drop specification has seen significant updates since then!

Whether you’re a web developer or just an everyday user, you’ve probably encountered Drag-and-Drop file uploading. You use it when tossing photos onto Facebook or attaching files in Gmail. Drag-and-Drop isn’t exactly a new concept—it’s how we’ve been moving files around our desktops for decades. But while there are thousands of tutorials covering how to build Drag-and-Drop uploads for the web, very few talk about Drag-and-Drop downloads. Let’s take a quick look at how to implement this cool HTML5 feature.

What exactly is Drag-and-Drop Download?

If you’ve ever used a cloud service like Box, you might have noticed that you can drag a file directly from your web browser and drop it onto your desktop to download it. You can even drag a file straight into your printer’s queue or drop it into a messaging app to send it to a friend. It completely eliminates the tedious “Right-Click -> Save As” workflow and saves you from having to dig through your file system.

Continue reading HTML 5 DnD Download: A Quick Implementation

Speed Up Your Site with Chrome Prerendering

Note: I wrote this guide in 2012. While the concept of resource hints is still very important, the specific prerender standard and browser implementations have evolved significantly since then. Modern best practices often rely on prefetch or preload instead.

While exploring the latest web technologies, I came across a fantastic tool for web designers. You might already know that Google Chrome has an option to enable Network Prediction. Google built this feature to dramatically decrease page load times by fetching resources in the background, so when a user clicks a link, the page loads almost instantly. This feature is often referred to as “Instant Pages.”

Here’s a quick video from Google explaining how prerendering works:

What exactly is prerendering?

According to the Google Web Developer Guide:

Prerendering is an experimental feature in Chrome (versions 13 and up) that can take hints from a site’s author to speed up the browsing experience of users. A site author includes an element in HTML that instructs Chrome to fetch and render an additional page in advance of the user actually clicking on it.

Continue reading Speed Up Your Site with Chrome Prerendering