WordPress Page 2

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

Add rel Attribute to Image Links in WordPress Galleries

#

If you want to use either Fancybox or Lightbox scripts for image galleries, you need to add the rel attribute to all full size image links. Here is how to do it:

add_filter('wp_get_attachment_link', 'add_gallery_id_rel');
function add_gallery_id_rel($link) {
	global $post;
	return str_replace('<a href', '<a rel="gallery-'. $post->ID .'" href', $link);
}

We simply replace <a href with <a rel="gallery-n" href where n is a unique post ID. This filter is applied in /wp-includes/post-template.php.

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

How to Add Simple Google Site Search to Any Website

#

Built-in search engines of WordPress, Drupal and other content management systems are good, but not as good as Google. Here is a simple way to create a Google search form that will return results only from your site:

<form name="google-search" method="get" action="http://www.google.com/search">
	<input type="hidden" name="sitesearch" value="http://yourdomain.com/" />
	<input name="q" type="text" />
	<input type="submit" name="sa" value="Google" />
</form>

which will look like this:

This form does redirect your visitors to Google, but the results returned are only from your site, so there is nothing to worry about. And with Googling indexing blogs almost instantly, there are actually very little drawbacks from using this approach for site search.