Archive for the ‘PHP’ Category

Increasing the number of posts per page on category, tag, and archive pages in WordPress

In WordPress, you can set the number of pages to display per page in the Administration settings. This is set in Admin/Settings/Reading.

The issue with this is that this is a global setting. If you would like to set a different number of posts to display on the front page than on the category page, you can do it by adding this code to functions.php in your theme.

The following code displays 20 posts per page on all pages that are not the front page (index). This includes search results, categories, tag pages, archives, and author listings.

function change_number_of_posts($query) {
	if ( ! is_front_page()) // Make sure it is not the front page
	$query->query_vars['posts_per_page'] = 20; // Change 20 to the number of posts you would like to show
	return $query; // Return our modified query variables
}
add_filter('pre_get_posts', 'change_number_of_posts'); // Hook our custom function onto the request filter

You can change this only for category pages for example with this code.

function change_number_of_posts($query) {
	if ( $query->is_category ) // Make sure it is a category page
	$query->query_vars['posts_per_page'] = 20; // Change 20 to the number of posts you would like to show
	return $query; // Return our modified query variables
}
add_filter('pre_get_posts', 'change_number_of_posts'); // Hook our custom function onto the request filter

The result is that you can set the global setting in the WordPress admin page to 10 posts, which affects the front page (index). You can then insert this code into functions.php and all other pages will display 20 posts.

Note: You do not have to modify your theme other than functions.php for this to work.

Zero day vulnerability in many WordPress themes

Mark Maunder announced a zero day vulnerability in many WordPress themes yesterday here on his blog.

If you use any third party WordPress themes, you should check your server for the presence of timthumb.php or thumb.php. It is reported that several theme marketplace websites host common legitimate themes that utilize this library.

The fix is to remove any allowed sites from the thumb.php or timthumb.php files as recommended below.

BEFORE:

$allowedSites = array (
	'flickr.com',
	'picasa.com',
	'blogger.com',
	'wordpress.com',
	'img.youtube.com',
	'upload.wikimedia.org',
	'photobucket.com',
);

AFTER:

$allowedSites = array ();

Please read the full technical details at Mark’s blog in the link above.

How to display nicely formatted PHP code in a WordPress post

I’ve been struggling for a while about how to embed PHP, HTML, or CSS code into WordPress posts so that they look good and are easy to understand.

I found this list of 12 WordPress Plugins to Display and Highlight Code.

I researched a few of them and finally settled on SyntaxHighlighter Evolved, which was simple to install, easy to use with short codes, formats nicely, and seems to have a large existing user base.

In your posts, you can simply use [php] and [/php] shortcodes around your actual PHP code and it does it’s trick. You can review the plugin page for the many various types of languages/syntax that are available.

I will spare your eyes and use this for all embedded code from now on.

Updating Price PHP print statements in Ubercart

Recently, I updated an Ubercart shopping cart system on Drupal that was very out of date.

After the upgrade, the list price on the product pages had too many zeros. It looked like this.

MSRP $ 49.99000

After a bit of research, I learned that Ubercart has been updated to include more decimal places to accommodate more International currencies.

I took a look at the node-product.tpl.php template and found that the print statement for the list price was using the out of date print statement.

The old print statement looked like:

list_price ?>

The new print statement looks like this:

list_price); ?>

Now all is fine.

MSRP $ 49.99

How to dynamically set Facebook Opengraph Meta tags in WordPress headers

I recently developed a WordPress website where it was important that some pages could be liked and show up on a user’s Facebook status. The front page also had a Facebook fan page like box. This is getting to be pretty much the standard for most websites.

Setting up the placement of the buttons is pretty trivial. This guide shows you how to generate the widget code for the fan page like box, and this guide shows you how to generate the widget code for the like/recommend button.

Embedded the widget code into the WordPress or Drupal theme is pretty straightforward, and placing it so that it looks right is a matter of simple CSS.

Getting the like/recommend buttons to function properly so that the like action shows up on the user’s Facebook status, which then leads that user’s friends to your website is the difficult part. Additionally, what shows up in that link might not always be what you want.

Ideally, you want the proper page title, a short description under the page title, and a little image to the left of the title and description which is indicative of what the page is about.

You must add Facebook Opengraph meta tags to the pages header for this to work right. This is done by adding the meta tags to the header.php file in your WordPress theme.

That’s easy to do if you just want to set a static image, title, and description for all pages on your site. I’m going to show you how to do that dynamically.

First, let’s assume that we want to have something for the front page, something for pages that have featured images, and something for pages/posts that do not have a featured image. This is done with a PHP if statement and few WordPress short codes.

The first thing that happens is that we set two tags that are always going to be the same no matter what – the fb:admin tag, which is the profile ID of one of the admins of the Facebook Fan Page. The second tag is the URL of the link.

Next, if the front page is liked, then specify the title, default description, and logo.

The next part of the statement says that if the post is a page, and the page has a featured image attached, then use the post title, and featured image. The description is taken from the body field.

The else statement says that for all other pages, use the post title, but use the site’s default logo and description.

You may want to play with these a little bit, but for my project this is what I needed. I’m sure this will get you started and you can figure out the rest in getting the proper look you want on the user’s Facebook status.

See this example below:





 















Consulting

I'm currently available
for Lotus Notes / Domino consulting engagements.

LinkedIn

Connect with me:

LinkedIn

Advertisement
Advertisement
Categories