Posts tagged 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.

Self-hosted HTML5 Notepad App With Synchronization

#

Update: On February 12, 2012 I released an updated version of this app with new UI, support for Markdown syntax (incl. editor) and a few bug fixes. Please see the project page for more screenshots and downloads.

Ever since the days of Google Gears which enabled local storage and offline syncing for online applications such as Google Reader and Docs, I have always wished for a very simple and instantly accesible notepad application that I could host on my server and use also without an internet connection. A place where I could instantly write down an idea or paste a link or a quote from an article.

Google Docs and Evernote is great but they have way more features than I would normally use. WordPress is fine, but it takes several steps to get to the actual writing, and you must be connected to the internet to use it.

The Solution

Now that we have HTML5 with support for localStorage and offline apps in all of the modern browsers, it seems to be the perfect solution. All that is needed is a little PHP script that synchronizes the content of localStorage with the server. Here is how it looks:

HTML5 Notepad lists all notes

HTML5 Notepad version 2012

Version 0.1 of HTML5 Notepad with Sync

You have your files on the left and a very big plain text editor on the right.

How Does it Work

The app consists of the following:

  • One HTML file with enabled offline access (cache manifest).
  • jQuery for all the Javascript and AJAX magic.
  • One PHP file with HTTP authentication for syncing with the server.

The files are stored in browser’s localStorage as key/value pairs with unique timestamp IDs as key and JSON formated content as value. An index of all files and their version timestamps is kept and used for synchronization during which all the content of localStorage is sent to sync.php in a POST request. The server reads its own index file and determines which new or edited entries need to be stored and which returned to the browser. All entries are stored on the server as JSON strings in simple text files.

Demo, Download and Installation

Please visit the project page of the HTML5 Notepad App. There is also a sample Chrome app.

Enable WordPress Plugin, Theme Updates and Pretty Permalinks on Nginx

#

WordPress doesn’t know that your Nginx web server is capable of doing URL rewrites without mod_rewrite and Apache, so we explain that by adding:

add_filter('got_rewrite', 'nginx_has_rewrites');
function nginx_has_rewrites() {
	return true;
}

in your theme’s functions.php.

It is very likely that along Nginx you are also running PHP-FPM for all your PHP needs, and for some reason WordPress thinks its PHP process can’t write to disk. We tell WordPress that it can:

add_filter('filesystem_method', 'nginx_make_filesystem_direct');
function nginx_make_filesystem_direct() {
	return 'direct';
}