How to make WordPress print the date every time

If you are using the template function the_date or get_the_date while listing posts you might notice that the date doesn’t get printed every time.

This is because the previous day is stored so that it doesn’t print the date stamp more than once.

A simple work around is to simply use the function the_time instead, or by resetting the $previousday variable with a filter.

function mytheme_the_date($date){
global $previousday;

$previousday = null;

return $date;
}
add_filter( 'the_date', 'mytheme_the_date' );

Even though I would recommend the first alternative it is good to know why the date doesn’t appear!

Tagged with: , ,
Posted in Beginner

Google Charts?

Google Charts in WordPress? Hmm, maybe!

Tagged with:
Posted in Misc

Style WordPress images and captions

When inserting an image into the rich text editor you get the following:

The WordPress logo

When viewing the code in the HTML tab you see that the following Shortcode is created:

[ caption id="attachment_75" align="alignnone" width="150" caption="The WordPress logo" ]<a href="https://wpquicktips.wordpress.com/wp-content/uploads/2010/03/blue-l.png"><img class="size-full wp-image-75" title="WordPress" src="https://wpquicktips.wordpress.com/wp-content/uploads/2010/03/blue-l.png" alt="" width="150" height="150" /></a>[ /caption ]

And the Shortcode creates the following HTML:

<div style="width: 160px;" class="wp-caption alignnone" id="attachment_75"><a href="https://wpquicktips.wordpress.com/wp-content/uploads/2010/03/blue-l.png"><img height="150" width="150" alt="" src="https://wpquicktips.wordpress.com/wp-content/uploads/2010/03/blue-l.png?w=150&amp;h=150" title="WordPress" class="size-full wp-image-75"></a><p class="wp-caption-text">The WordPress logo</p></div>

When developing new themes it really helps to use the existing markup. You could redo the HTML if you wanted by using filters, but why invent the wheel twice?

Tagged with: , ,
Posted in Beginner

How to change the allowed HTML tags for WordPress

WordPress by default filters out potentially harmful and/or invalid HTML. But in some cases you might want some of that invalid HTML!

One of those cases occurred today when we needed to embed a Google Form in a post. To do so, Google provides you with HTML code that contains a iframe, but uh-oh! The iframe element is not allowed by default.

If you look in wp-includes/kses.php you see a variable called $allowedposttags. The array contains all the allowed tags, and their allowed attributes. So you could also change the allowed attributes for the already allowed elements if you so wish.

To include iframe in the list of allowed element I added the following code to my theme’s functions.php:

$allowedposttags["iframe"] = array(
 "src" => array(),
 "height" => array(),
 "width" => array()
);

And something that is always requested is the ability to simply just paste the embed-code provided by YouTube or Vimeo without having it filtered out. To do so simply add this to your functions.php or plugin:

$allowedposttags["object"] = array(
 "height" => array(),
 "width" => array()
);

$allowedposttags["param"] = array(
 "name" => array(),
 "value" => array()
);

$allowedposttags["embed"] = array(
 "src" => array(),
 "type" => array(),
 "allowfullscreen" => array(),
 "allowscriptaccess" => array(),
 "height" => array(),
 "width" => array()
);

But if you want to embed Flash files in a more standard compliant and modern way, check out our Easy Flash Embed plugin instead!

Tagged with: , , ,
Posted in Intermediate

Never use PHP short tags for WordPress development

Today while working on a project where we had collaborated with freelance developers I noticed that some pages weren’t looking as they should. A quick glance at the code revealed that the developer was using “short tags” to include the header.

<? get_header ?>

To use short tags, the short_open_tag option must be turned on in php.ini. On most servers short_open_tag is turned on, but alot of servers (like my local server) has it turned off.

If you are developing a theme for a specific server environment, this may be acceptable. But if you are developing a theme or a plugin to be used by a lot of people on a lot of different server environments then short tags can not be used!

Tagged with: , ,
Posted in Best Practices, Misc

Embed Flash files in WordPress

While developing a site for a client a need emerged for us to be able to embed Flash files in the content.

To do so you could paste some HTML in the content editor and hope for it not to be parsed away. And to keep it the HTML or XHTML  valid while doing so, and to support alternative content might be a bit tricky.

I thought that I would fix this problem using the Shortcode API, and also to make a plugin out of it while I was at it.

The result is Easy Flash Embed!

Embed Flash easily and standard compliant with SWFObject using only a [swf] shortcode!

In the text editor simply write something like:

[swf src="http://www.example.com/my-flash-file.swf" width=300 height=100]

You can even add some alternative content by writing something between the brackets:

[swf src="http://www.example.com/my-flash-file.swf" width=300 height=100]You must have Flash to view this file[/swf]

Read more about it or download it right now!

Tagged with: , , , ,
Posted in Beginner

Use WordPress functions to include your CSS

When I’m not developing for WordPress, I try to use a specific folder structure for my project. I have all my .css-files in the /css/-folder, all JavaScript in /js/-folder and so forth.

I tried to keep this up for WordPress, and placing my CSS in the root instead of the theme folder, but this quickly changed. Mainly because it’s easier to manage when you have all your files in one folder (your theme folder). And also because it’s not the way it’s meant to be done.

You can use functions as bloginfo(“stylesheet_url”) to include style.css. The function generates a complete URL, which is great since you should always have a complete URL due to performance issues.

Tagged with: ,
Posted in Beginner, Best Practices

Use an empty action attribute in forms

When developing a simple form to use in your WordPress theme or plugin you often want to post to the same page.

To do so, simply leave the action attribute empty in your form.

<form action="" method="post">
<div>
<label>Name</label><br />
<input type="text" name="your_name" />
<input type="submit" value="Send" />
</div>
</form>

Notice that your_name is used, using simply name as the attribute value may cause some trouble. At least it did for me.

Mark Jaquith has written a bit more detailed about the action attribute.

Tagged with: ,
Posted in Best Practices, Intermediate

Make use of the body_class function

The function body_class was introduced in WordPress 2.8 and should be included in every theme.

Include it in your HTML like so:

<body <?php body_class(); ?>

And you will end up with something like this

<body class="page page-id-4 page-parent page-template page-template-default">

It can come quite handy when you want pages to have different color themes for example. Just like the classes in your navigation, the classes can be altered with the body_class filter.

For example, filter the classes so that page-id-4 becomes page-YOUR-SLUG

function my_theme_body_class($classes, $class){
	global $post;
	foreach($classes as &$str){
		if(strpos($str, "page-id-") > -1){
			$str = "page-" . $post->post_name;
		}
	}
	return $classes;
}
add_filter("body_class", "my_theme_body_class", 10, 2);

Hope you find it useful!

Tagged with: ,
Posted in Beginner, Best Practices
About

WordPress Quick Tips is a blog supplying great tips about WordPress.

We hope to create a great knowledge resource for WordPress developers as well as serving a reminder for all the forgetful ones.

The blog is created and run by Vincent of Oakwood Creative

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 13 other subscribers
Design a site like this with WordPress.com
Get started