Posts tagged code

Enable WordPress Dashboard Quick Login

#

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
}

How to Avoid Widow “Read More” Links in Post Excerpts

#

Read more link widow in WordPress excerpts

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 ', '&nbsp;<a ', $more_link);
}

Add Custom URL Redirects to Your WordPress Dashboard Areas or Login Page

#

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;
}

How to Remove All Line Breaks from Text Using Regex

#

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);

How to Add Post Categories and Tags as #Hashtags to Tweets Automatically with the Simple Twitter Connect Plugin

#

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 added as hashtags in Twitter Publisher using Simple Twitter Connect plugin

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;
}

HTML5 Notepad App Update

#

I have released an updated version of my HTML5 Notepad app which features a new user interface, adds support for Markdown syntax (along with a simple editor and preview) and fixes a synchronization bug which in some cases converted line breaks into text.

HTML5 Notepad with Markdown syntax and sync support

Please note that there could still be some bugs and the code hasn’t been polished for a production use, but it serves as a very useful example of some of the most interesting HTML5 features such as localStorage and offline cache.

You can fork it on GitHub or download from the project page.

How to Automatically Add Image Credit or Source URL to Photo Captions in WordPress

#
Add image author or source URL to photos in WordPress

Photo credit added automatically to every photo with a caption (via putukrejums.lv)

Crediting and linking the source of any republished photo or illustration on the web is one of the most important best practices of web publishing. Unfortunately, there isn’t a standard way of doing it in WordPress and authors are left with their own decision on how and where to credit the original author or website. Read more »

Remove Inline CSS and Line Breaks in WordPress Galleries

#

Inline CSS and line breaks <br style="clear" /> that are automatically added to the image galleries in WordPress is an example of how the needs of Automattic and WordPress.com influence the way new features are added to the WordPress core. So instead of editing every single theme on WordPress.com and adding the necessary CSS for galleries to look good in all of them, they decided to put it WordPress core.

Here is how to replace those double line breaks with a single <br /> and remove the inline CSS:

add_filter('the_content', 'remove_br_gallery', 11);
function remove_br_gallery($output) {
	return preg_replace('/(<br[^>]*>\s*){2,}/', '<br />', $output);
}
add_filter('use_default_gallery_style', '__return_false');