code Latest Entries

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

jQuery Script for Loading More Posts

AKA Twitter, Facebook and Tumblr style.

jQuery(document).ready(function($) {

	var $content = '#content';
	var $nav_wrap = '.navigation';
	var $anchor = '.navigation .alignleft a';
	var $text = 'Load More';

	var $next_href = $($anchor).attr('href'); // Get URL for the next set of posts

	$($nav_wrap).html('<a id="almc-load-more" href="' + $next_href + '">' + $text + '</a>');

	$('#almc-load-more').click(function(e) {
		e.preventDefault();

		$.get($(this).attr('href'), '', function(data) {
			var $timestamp = new Date().getTime();
			var $new_content = $($content, data).wrapInner('<div class="almc-loaded" id="almc-' + $timestamp + '" />').html(); // Grab just the content
			$next_href = $($anchor, data).attr('href'); // Get the new href

			$('html,body').animate({scrollTop: $($nav_wrap).position().top}, 'slow'); // Animate scroll

			$($nav_wrap).before($new_content); // Append the new content
			$('#almc-' + $timestamp).hide().fadeIn('slow'); // Animate load
			$('#almc-load-more').attr('href', $next_href);	// Change the next URL
			$('.almc-loaded ' + $nav_wrap).remove(); // Remove the original navigation
		});
	});

});

Automatic Updates for Plugins and Themes Hosted Outside WordPress Extend

Here are two sample scripts along with an API to provide automatic updates for plugins and themes you host on your own server.

Inside /api you’ll find index.php which processes all the update requests. You should place this in something like http://updates.example.com and update $api_url in /plugin/test-plugin-update/test-plugin-update.php and /theme/portfolio-racer/inc/updates.php accordingly. If you activate these sample plugins without changing API URL, updates will be checked against my test server. If you decide to update, both plugin and theme will be replaced with exactly the same version of each.

Standalone Script for Clearing APC Cache

After upgrading WordPress to a new version, you might need to clear the APC opcode cache. Here is a simple script that you can store in the root of your website, for example, clearapc.php:

if (function_exists('apc_clear_cache') && $_GET['pass'] == 'secret') {
        if (apc_clear_cache() && apc_clear_cache('user'))
                print 'All Clear!';
        else
                print 'Clearing Failed!';

        print '<pre>';
        print_r(apc_cache_info());
        print '</pre>';
} else {
        print 'Authenticate, please!';
}

Then call the script via http://example.com/clearapc.php?pass=secret

Instead of using ?pass=secret, you might well call the filename something that is hard to guess: clearapc93920.php

Automatically Escape HTML Entities of Code Fragments in Comments

Update: Ryan has made this into a plugin — Code Comments.

Add this to your theme’s functions.php to allow readers post fragments of code in their comments (wrapped in <code>...</code>) which are automatically encoded (think of < and &lt;)

add_filter('pre_comment_content', 'encode_code_in_comment');

function encode_code_in_comment($source) {
  $encoded = preg_replace_callback('/<code>(.*?)<\/code>/ims',
  create_function(
    '$matches',
    '$matches[1] = preg_replace(
        array("/^[\r|\n]+/i", "/[\r|\n]+$/i"), "",
        $matches[1]);
      return "<code>" . htmlentities($matches[1]) . "</code>";'
  ),
  $source);

  if ($encoded)
    return $encoded;
  else
    return $source;
}

Worth noting:

  • Everything wrapped in <code>...</code> is encoded.
  • Line breaks after opening <code> and before closing </code> are removed in order to avoid unnecessary <br /> tags.

How to Create Beautiful and Elegant HTML Lists Using CSS

HTML list have become one of the most used HTML elements for marking-up various semantic content structures — navigation, comments and even image galleries.

This article will explain and show you how to style lists inside blog posts, articles or other basic HTML documents.

Before we start, it is necessary to understand the importance of using specific HTML tags <ul> and <ol>, instead of simple numbering (like 1., 2. or •, ») for building lists. By applying content a semantic structure, we emphasize the relationships between different content elements. In case of lists we are able to imply that there is a certain relationship between all of the list members, which is possibly described by the paragraph introducing the list. It also helps screen reader users for whom the total number of items is announced before the rest of the list. Read more »