Sticky Posts

The main purpose of this site is as a testing bed for templates in construction. Because of this, the appearance of this site changes regularly.

This post is a great example of a Sticky Post.

Sticky posts are posts that you want to remain prominent and, when published, will remain at the top of your posts. They’re also very easy to create too.

Once you’ve written your post you can click on the Visibility options in the right hand Publish tab and tick the “Stick this post to the front page” checkbox. For previously published posts you can also select “Make this post sticky” using the quick edit option on your Posts menu

Posted in Technical | Leave a comment

Two more really useful shortcode hacks

We’re going to look at a simple way to add Facebook “like” buttons and embed YouTube videos into content, both of which use iframes and in both cases I’ve used the defaults but support for variations is included in the code.

The Facebook “like” button really only needs to know what page you’re on now which we obtain usingĀ  urlencode(get_permalink($post->ID)) and insert it into the standard Facebook code. You will see that I’ve also allowed variables for all the options within the Facebook Like code.

// facebook like

function facebook_like($atts) {
 extract(shortcode_atts(array(
 'layout' => 'standard',
 'width' => '450',
 'height' => '25',
 'faces' => 'false',
 'colorscheme' => 'light',
 'action' => 'like',
 ), $atts));

 return "<iframe src=\"http://www.facebook.com/plugins/like.php?href=" . urlencode(get_permalink($post->ID)) . "&amp;layout=" . $layout . "&amp;show_faces=" . $faces . "&amp;width=" . $width . "&amp;action=" . $action . "&amp;colorscheme=" . $colorscheme . "\" scrolling=\"no\" frameborder=\"0\" allowTransparency=\"true\" style=\"border:none; overflow:hidden; width:" . $width . "px; height:" . $height . "px\"></iframe>";
}
add_shortcode('facebook', 'facebook_like');

Using Facebook Like shortcode on your WordPress site

YouTube also uses an iframe to support embedding. I’m going to use the newer version of embedding because it’s simpler to implement but more powerful in the way it approaches video playback, favouring HTML5 video support but falling back to Flash if that’s unavailable.
For more details see the YouTube Blog

// youtube video

function youtube_embed($atts) {
 extract(shortcode_atts(array(
 'embed' => '',
 'width' => '480',
 'height' => '390',
 ), $atts));

 return "<iframe title=\"YouTube video player\" class=\"youtube-player\" type=\"text/html\" width=\"" . $width . "\" height=\"" . $height . "\" src=\"http://www.youtube.com/embed/" . $embed . "\" frameborder=\"0\" allowFullScreen></iframe>";
}
add_shortcode('youtube', 'youtube_embed');

Using YouTube embed shortcode on your WordPress site

Posted in Hints and Tips | Tagged , , | Comments Off on Two more really useful shortcode hacks

Shortcode in custom sidebar widget

I keep saying that WordPress makes a great CMS because of it’s ease of use and great flexibility, especially with all the great plugins that are available for it. However the disadvantage of using too many plugins is that, as you start to extend WordPress, you reduce the simplicity of it.

It is because of this that I’ve started to write custom functions that are embedded into the themes that I create. One of the plugins I replaced was a sidebar text widget that I used for creating custom sidebar text on a per post basis.
The only problem with it was that it created a link within the Custom Fields section which didn’t work as expected so you’d have to scroll down to the bottom of the editor and add it in. Confusing, it was!

Instead I created my own Custom Fields, disabling the WordPress defaults, based largely on the work of Steve Taylor.

Two of the fields are designed for custom sidebar text so the next process was to register widgets however because they need to get data from a post or page I needed to tell the widget what the post ID is.

After a lot of Googling and even more trial and error I realised that the only way I was going to get the ID into the function was to make it a global which worked beautifully…

function sidebar1() {
 echo '<div id="SidebarText1">';
 echo get_post_meta($GLOBALS['current_id'], '_sejaWP_sidebar_text_1', true);
 echo '</div>';
}
register_sidebar_widget(__('Side Text 1'), 'sidebar1');

Until I needed to add some shortcode to make a custom menu work

Googling shortcode in widgets returns a whole lot of posts that tell you to place the following code in the functions.php file

add_filter('widget_text', 'do_shortcode');

But this simply wasn’t working for me, probably because of the Globals variable call but thankfully one of the many articles led me to Stephanie Leary’s article Using Shortcodes Everywhere which introduced me to other possibilities.

The updated widget now uses do_shortcode which is designed for use directly within a theme (which I guess in a way it is!)

function sidebar1() {
 echo '<div id="SidebarText1">';
 echo do_shortcode(get_post_meta($GLOBALS['current_id'], '_sejaWP_sidebar_text_1', true));
 echo '</div>';
}
register_sidebar_widget(__('Side Text 1'), 'sidebar1');
Posted in Technical | Leave a comment

Flash Gallery

[flashgallery img=’2010/09/temp’]

I have used an freely available flash gallery that extracts images from a folder, this is a great solution as it doesn’t involve the editing of an XML file which, although is not complicated, does add an extra learning curve to clients wishing to utilise such a solution.

I have added a simple shortcode into the functions.php file which facilitates the scripting and calls on the image folder location and am using a plugin that allows for further categorising of image uploads (Custom Upload Dir).

The only drawback is that WordPress, by default, creates multiple images when you upload files so I will need to find a solution to that as ideally I don’t want to be using FTP.

Posted in Technical | Leave a comment

Extending Custom Menus

Custom Menus in WordPress 3 are great. They allow for easy drag and drop to create custom menu structures. Custom menus can be found by going to Appearance > Menus

The current default template, Twenty Ten, only uses one custom menu. Here we’re going to do two things – Firstly we’re going to extend the number of menu’s you can use and secondly we’re going to create some shortcode so we can drop our menu’s right into a post, page or sidebar widget.

Open up the functions.php file in your editor and go to the register_nav_menus array around line 99

register_nav_menus( array(
 'primary' => __( 'Primary Navigation', 'twentyten' ),
 ) );

Now add additional menus into the array

register_nav_menus( array(
 'primary' => __( 'Primary Navigation', 'twentyten' ),
 'menu-one' => __( 'Menu One', 'twentyten' ),
 'menu-two' => __( 'Menu Two', 'twentyten' ),
 'menu-three' => __( 'Menu Three', 'twentyten' ),
 ) );

That’s it – We now have our additional menu’s

The next stage is to call on these new menu’s. Traditionally that means embedding code within our template like so –

<?php wp_nav_menu(  array( 'theme_location' => 'menu-one' ));  ?>

Instead we’re going to further add to our functions.php file and convert this into shortcode that can be used anywhere in WordPress and I found a great example of how to do this here – http://www.cozmoslabs.com/2010/06/28/wp_nav_menu-shortcode/

// Function that will return our WordPress menu
function list_menu($atts, $content = null) {
	extract(shortcode_atts(array(
		'menu'            => '',
		'container'       => 'div',
		'container_class' => '',
		'container_id'    => '',
		'menu_class'      => 'menu',
		'menu_id'         => '',
		'echo'            => true,
		'fallback_cb'     => 'wp_page_menu',
		'before'          => '',
		'after'           => '',
		'link_before'     => '',
		'link_after'      => '',
		'depth'           => 0,
		'walker'          => '',
		'theme_location'  => ''),
		$atts));

	return wp_nav_menu( array(
		'menu'            => $menu,
		'container'       => $container,
		'container_class' => $container_class,
		'container_id'    => $container_id,
		'menu_class'      => $menu_class,
		'menu_id'         => $menu_id,
		'echo'            => false,
		'fallback_cb'     => $fallback_cb,
		'before'          => $before,
		'after'           => $after,
		'link_before'     => $link_before,
		'link_after'      => $link_after,
		'depth'           => $depth,
		'walker'          => $walker,
		'theme_location'  => $theme_location));
}
//Create the shortcode
add_shortcode("listmenu", "list_menu");

To use this shortcode within a post


This works particularly well in conjunction with the side content plugin, allowing custom indexes for child pages within the sidebar.

Posted in Technical | Tagged , | 2 Comments

Cool use for secondary sidebar widget

I’m really liking some of the features in WordPress 3. As someone who writes WordPress templates for clients to use as a CMS, this version has gone a long way to make it even easier.

Because most of my WordPress installs are for commercial sites I tend to set them up with focus on writing pages rather than posts. Posts become great as “Latest News” style pages.

One of the things that makes this even easier is secondary widgets in the sidebar. Essentially this allows us to use the primary widget area to place additional content across the whole site and focuses the secondary widget area to focus on posts.

What this means is you can have your search bar or custom text that appears on every page but target blog specific items, such as calendars, tag clouds and archive links to posts only.

This is done by slightly modifying the secondary widget code in the sidebar.php file

As I want the secondary widget area to only display in posts I’m going to use the is_page conditional tag to do this. You should find the following code around line 45 on the sidebar.php page

<?php
 // A second sidebar for widgets, just because.
 if ( is_active_sidebar( 'secondary-widget-area' ) ) : ?>

Modify the above code so it looks like this

<?php
 // A second sidebar for widgets, just because.
 if ( !is_page() && ( is_active_sidebar( 'secondary-widget-area' ) ) ) : ?>

Secondary widgets will now display ONLY on the posts page – Simple, huh!

Posted in Hints and Tips, Technical | Tagged | 1 Comment

Comment Spam in image posts

When you create a gallery page, WordPress automatically creates a separate page for each image. If you’re using the lightbox plugin you do not see this page but that doesn’t mean it’s not accessible.

A trick by spammers is to use this page to add comment spam because, if comments are enabled by default (which is usually the case with WordPress installations), then comments will be available on these gallery posts and there is no way to turn them off.

To avoid having comments in these media posts it is recommended to have comments disabled on all posts. You can then initialise comments on individual posts on an “as required” basis.

Unfortunately this doesn’t help you if you’re already being spammed in this manner. WordPress forums were not much help as they suggest that you disable comments then delete and rebuild the gallery which, quite frankly, is a poor solution.

If you’re technically minded and not afraid to tackle the database from the back-end then it’s actually very easy to solve this problem.

Firstly you need to know what the galley entry is called. If you’re recieving moderate notification emails the information is already provided

A new comment on the post “Chrysanthemum” is waiting for your approval
http://wordpress.seja-design.com.au/2009/11/lightbox/chrysanthemum/

Knowing now that the post is called Chrysanthemum we can now log into phpMyAdmin (available from your cPanel if we’re hosting your website), navigate to your WordPress database and open up theĀ  wp_posts table to locate the offending post

Click on the edit (pencil) icon and change the comment_status field from “open” to “closed”. Click Go and the change has been made. You can then repeat this process on other offending pages.

Posted in Hints and Tips | 1 Comment

another image test – separate images?

Penguins
Image entered and preconfigured to work with lightbox
Tulips
A medium size image
Tulips
Same as before – entered in the same way with the intention of editing afterwards.
Footnote: edit was successful so image interacts with Lightbox as per the first image

Posted in Technical | Comments Off on another image test – separate images?

lightbox

Testing the lightbox plugin, using some default images from Windows 7

Default WordPress Gallery

Posted in Technical | Tagged | Comments Off on lightbox