Archive
# I’ve created a very simple online tool for converting .po and .mo files (both directions) which many WordPress developers will find useful especially when translators forget to email the original .po file. You can learn more about translating WordPress core, plugins and themes at WordPress Codex.

# Here is a simple filter to automatically add a class attribute like widget-order-1 to all widgets within sidebars:
add_action('init', 'add_widget_order_class');
function add_widget_order_class() {
global $wp_registered_sidebars, $wp_registered_widgets;
$sidebars = wp_get_sidebars_widgets();
if (empty($sidebars))
return;
foreach ($sidebars as $sidebar_id => $widgets) {
if (empty($widgets))
continue;
foreach ($widgets as $i => $widget_id) {
$order = $i + 1;
$wp_registered_widgets[$widget_id]['classname'] .= ' widget-order-' . $order;
}
}
}
# All of the web browsers today have input autocomplete or auto-fill feature built-in and chances are that you have your WordPress username and password stored in the browser and the input fields are completed automatically upon visiting /wp-login.php. Wouldn’t it be nice if WordPress would log you in automatically if the fields have been pre-filled by the browser?
Here is a simple snippet of PHP and Javascript that checks if the input fields have been filled and submits the login form one second after the page has been loaded. All you have to do is append #quicklogin to your login bookmark URL so that it looks like http://example.com/wp-admin/#quicklogin and Javascript will do its magic. Add this to your functions.php.
add_action('login_footer', 'enable_admin_quick_login');
function enable_admin_quick_login() {
?>
<script type="text/javascript">
$url_hash = window.location.hash;
if ($url_hash.indexOf('quicklogin') != -1) {
setTimeout(function() {
if (document.loginform.user_login.value && document.loginform.user_pass.value) {
document.loginform.submit();
}
}, 1000);
}
</script>
<?php
}
# The concept of page stacks is something brilliant for building leveled navigation and giving users immediate glimpse of the big picture.
(via onkulis.com)
# 
A “widow” in typography is defined as:
A paragraph-ending line that falls at the beginning of the following page/column, thus separated from the rest of the text.
If you are using the standard inline “Read more” links at the end of post excerpts on index and archive pages, here is a simple filter to ensure that a non-breaking space is added before the “Read more” link:
add_filter('the_content_more_link', 'prepend_non_breaking');
function prepend_non_breaking($more_link) {
return str_replace(' <a ', ' <a ', $more_link);
}
#
Etsy Code as Craft on LiveStream
# Ever wanted to use something like example.com/backend or example.com/dash to access your WordPress dashboard or login area? Here is a simple snippet of PHP for your functionality plugin or functions.php to do just that — it uses standard WordPress URL rewrite API.
Update: turns out that in WordPress 3.4 there are new default redirects (/admin, /dashboard and /login) implemented in the core (see this ticket). So here is a better way to add your own redirects which will work only if you don’t have a page or post with the same name (slug) already:
Updated Version:
add_action('template_redirect', 'add_my_custom_redirects');
function add_my_custom_redirects() {
if (!is_404())
return;
$current_uri = untrailingslashit($_SERVER['REQUEST_URI']);
$my_admin_uris = array(
home_url('dash', 'relative'),
home_url('your-custom-uri', 'relative')
);
if (in_array($current_uri, $my_admin_uris)) {
wp_redirect(admin_url());
exit;
}
}
The Obsolete Version:
add_filter('generate_rewrite_rules', 'add_my_custom_rewrites');
function add_my_custom_rewrites($wp_rewrite) {
$my_rewrites = array(
'dash' => 'wp-admin'
);
$wp_rewrite->rules = $my_rewrites + $wp_rewrite->rules;
}
# If you are generating <meta> description tags automatically (e.g. by including all headings of the document), chances are that you’re extracting it from various sources of content that contain different HTML elements and line breaks in them. Here is a simple regular expression to remove all line breaks, carriage returns and tabs, and replace them with an empty space.
$text = preg_replace("/\r\n+|\r+|\n+|\t+/i", " ", $text);
# Headlines and headings are usually very relevant and descriptive pieces of information for any HTML page. You might want to include them into the description <meta> tag on that page. Here is a simple regular expression to extract all those headings:
preg_match_all('|<h[^>]+>(.*)</h[^>]+>|iU', $html, $headings);
# Simple Twitter Connect is a really useful, simple and well coded plugin by Otto (@Otto42) that allows you to post tweets right from your WordPress dashboard or automatically after publishing new posts.
A standard tweet containing a prefix (such as “New blog post:”), post title and a link to that post is generated automatically by the plugin. Wouldn’t it be nice to include also post categories and tags as hashtags in order to add additional metadata along with the post title? Like so:

Post categories and tags replaced or added to tweet as hashtags
Thanks to the stc_publish_text filter supplied with the plugin there is an easy way to do it. Add this snippet to your theme’s functions.php:
add_filter('stc_publish_text', 'add_taxonomies_to_tweets', 10, 2);
function add_taxonomies_to_tweets($output, $id) {
if ($cats = get_the_category($id))
foreach ($cats as $c => $cat)
$output = add_taxonomy_hashtag($output, $cat->cat_name);
if ($tags = get_the_tags($id))
foreach ($tags as $t => $tag)
$output = add_taxonomy_hashtag($output, $tag->name);
return $output;
}
function add_taxonomy_hashtag($tweet, $tax) {
if (stripos($tax, ' ')) // Remove whitespace
$tax = str_replace(' ', '', $tax);
if (strlen($tweet) + 1 > 140) { // Check if the new tweet is not too long
return $tweet;
} elseif (stripos($tweet, $tax)) { // Replace an existing word with a tag
return str_replace($tax, '#' . $tax, $tweet);
} elseif (strlen($tweet) + strlen($tax) + 1 < 140) { // or simply append it
return $tweet . ' #' . $tax;
}
return $tweet;
}