Set Default Number of Columns for WordPress Gallery

WordPress version 2.5 introduced a very useful feature for many bloggers: a built-in gallery system. All your photos uploaded to a specific Post or Page and using the Gallery Shortcode, [gallery], will display a set of thumbnails anywhere in your Post or Page. When used with most WordPress themes, the default 3-column thumbnail display works very well. But for some WordPress themes, such as this one, the default 3-column thumbnail display has too much space between each thumbnail. WordPress-Gallery-4-columnsFortunately, you can make the Gallery display 4-or-more-columns by adding columns=4 inside the Gallery Shortcode like [gallery columns=4]. That can be a pain because you have to remember to do each time you add a Gallery to a Post or Page and all your prior Posts or Pages will continue to be 3-columns. So how do you reset the default number of Gallery thumbnails? Fortunately a Google search led me to this post by Zeo who has a solution in which you enter the code below in your functions.php file:

[sourcecode language=”PHP”]
function gallery_columns($content){
$columns = 2;
$pattern = array(
‘/([gallery(.*?)columns="([0-9])"(.*?)])/ie’,
‘/([gallery])/ie’,
‘/([gallery(.*?)])/ie’
);
$replace = ‘stripslashes(strstr("1", "columns="$columns"") ? "1" : "[gallery 2 4 columns="$columns"]")’;

return preg_replace($pattern, $replace, $content);
}

add_filter(‘the_content’, ‘gallery_columns’);
[/sourcecode]

Make sure you change $columns = 2; to however many columns you like. Also the above code has to be wrapped in &#lt;?php and ?> or it will not work.

Very nice and elegant. This way if you change themes, you don’t have to manually edit every Post or Page to adjust how the Gallery column thumbnails display. Thanks Zeo.

iFrames in WordPress Post Editor Using Shortcodes and Custom Fields

I was looking for a way to embed a Google Map into one of my Reviews, but the FCKEditor, otherwise known as the Visual (or pretty) Editor, would remove any iFrame code. Posting it via the HTML tab works so long as you don’t click to the Visual tab, as it will strip the iFrame code. This can get pretty frustrating. I didn’t want to have to install another plugin to do iFrames either. Fortunately through a quick Google search I found Walt had a very elegant solution using WordPress Shortcodes and Custom Fields.

His technique worked out quite nicely and will allow me to post other iFrame content while still using the Visual Editor and not worry about the iFrame code getting stripped out. So I’m reposting his technique here for future reference.

You need to have a functions.php file in your theme for this to work. If it doesn’t exist, just create one and drop this line of code into it, making sure it is wrapped by <?php and ?> (thanks to Smick for reminding me):

[sourcecode language=”php”]
function field_func($atts) {
global $post;
$name = $atts[‘name’];
if (empty($name)) return;

return get_post_meta($post->ID, $name, true);
}

add_shortcode(‘field’, ‘field_func’);
[/sourcecode]

Upload that file into the theme folder that you are using or want to enable iFrames for.

[showads]

Now any anytime you want to embed a Google Map iFrame, all you have to do in your Post Editor is decide where you want the iFrame to appear and put:

[field name=iframe]

Then scroll down to the Custom Fields box and under Name write iframe and under Value paste in the exact code. Here’s an example:

WordPress-iframe-shortcode-functions-example

Make sure you click Add Custom Field to save the entry or it won’t show up in your post when you publish or update the changes. Now you can easily edit the existing post using the Visual Editor and not have to worry about losing any of your iFrame code.

Also note that unlike a plugin where you can switch themes and the code will continue to work, once you switch to a new theme, the iFrame code will no longer appear in any of your posts so make sure to include this line of code in your new theme. You could also save yourself the headache and make this function into a WordPress plugin.

WordPress iPhone Native App

The new WordPress iPhone Native App sadly does not work with WordPress MU if you use the domain mapping option. It only works for the main website. It won’t connect to any other domain mapped website. The problem has to do with the way we are doing domain mapping and how it affects XML-RPC.