WordPress + Varnish Cache, A match made in heaven


Recently my other blog, Script and Scroll, started to get a decent amount of traffic. One blog in particular linked back to a post someone published and it gained a lot of attention. Within a few hours of the link being created I started to get alerts from my monitoring system…Wordpress was slowing down, crapping out and running out of memory when serving pages. The load on my VPS hit the roof and Apache more or less came to a halt. After a quick round of investigating, scanning logs etc, I realized what was happening after seeing the referrer and looking at the page that had linked to us. Right, it was time to do something now that I knew what the problem was. Mind you, I had a caching plugin installed but it wasn’t doing much good. After thinking of what my options were I decided to go with Varnish Cache.

Read more of this post

Migrating from self-hosted to hosted wordpress


Been a while since I posted an entry, I thought I’d do one today after my little migration.

I’ve always used a self-hosted WordPress until two nights ago. I decided the resources used by my blog just wasn’t worth having it on my VPS with my other sites. I didn’t want to use a wordpress.com sub-domain either but then I read about a little and found that you can map your existing domain name, I thought “sweet”. For pennies I can get my domain and blog hosted and not worry about server and all that stuff. So here’s a quick break down of migrating from a self-hosted wordpress installation, over to the “hosted” wordpress. Read more of this post

Create custom widgets for the wordpress dashboard


Following on from my previous post on how to remove the default widgets/modules on the wordpress dashboard, today I’m going to show you how quick and simple it is to add your own custom widget/module that displays or does what ever you want it to.

The code below creates to widgets on our dashboard with the title of posts from two different categories,
specified by the cat=x parameter.

 // add 10 most recent post to dashboard from category with id 10
 function blogPost_widget() {
 $recentPosts = new WP_Query();
 $recentPosts->query('showposts=10&cat=10');
 ?>
 <ul>
 <?php
 while ($recentPosts->have_posts()) : $recentPosts->the_post();
 ?>
 <li><a href="<?php  the_permalink(); ?>"><?php the_title(); ?></a></li>
 <?php endwhile; ?>
 </ul>
 <?php
 }
 //adds news widget to dashboard
 function blogNews_widget() {
 $recentPosts = new WP_Query();
 $recentPosts->query('showposts=10&cat=1');
 ?>
 <ul>
 <?php
 while ($recentPosts->have_posts()) : $recentPosts->the_post();
 ?>
 <li><a href="<?php echo the_permalink() ?>" ><?php the_title(); ?></a></li>
 <?php endwhile; ?>
 </ul>
 <?php
 }
 // Create the function use in the action hook

 function add_dashboard_widgets() {
 //this will display titles and links to the blog posts
 wp_add_dashboard_widget('blogPost_widget', 'Recent Blog Posts', 'blogPost_widget');//widget one
 wp_add_dashboard_widget('blogNews_widget', 'Company news', 'blogNews_widget');//widget two
 }

 // Hook into the 'wp_dashboard_setup' action to register our other functions

 add_action('wp_dashboard_setup', 'add_dashboard_widgets');
 

The above code goes into your functions.php file for a template or anywhere in your plugin file. Taken directly from the wordpress documentation, the wp_add_dashboard_widget is the magic function here,
it gives our widget an ID and a name and tells wordpress which function to invoke when creating the widget.
The parameters expected from the wordpress docs are:

<?php wp_add_dashboard_widget($widget_id, $widget_name, $callback, $control_callback ); ?>

Parameters

$widget_id

(integer) (required) an identifying slug for your widget. This will be used as its css class and its key

in the array of widgets.

Default: None

$widget_name

(string) (required) this is the name your widget will display in its heading.

Default: None

$callback

(string) (required) The name of a function you create that will display the actual contents of your widget.

Default: None

$control_callback

(string) (optional) The name of a function you create that will handle submission

of widget options forms, and will also display the form elements.

Further reading: http://codex.wordpress.org/Function_Reference/wp_add_dashboard_widget

Remove the default dashboard widgets from WordPress programatically


It’s been over a week since I made a decent post (been extremely busy) so I thought I’d give it a go today.

One of the project’s I’ve had going on requires building a website for a local charity. I discussed it with the charity and agreed on using wordpress. It turns out however, the API for wordpress admin is very minimal and has very little documentation.

I found myself having to develop a large portion of things for the admin section so I’ve had to make do. One of the things I was asked for was to show custom widgets on the dashboard with various things and to remove the default ones…. whaaaaa? Usually wordpress users just manually remove and/or configure these widgets, however the potential users of this are going to be people who have little or no technical expertise… Below is a quick and simple way to remove all the default widgets/modules that are loaded on the dashboard.

function remove_dashboard_widgets() {
    // Globalize the metaboxes array, this holds all the widgets for wp-admin

    global $wp_meta_boxes;
    //remove all default dashboard apps
    unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_right_now']);
    unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_recent_comments']);
    unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_incoming_links']);
    unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_plugins']);
    unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_quick_press']);
    unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_recent_drafts']);
    unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']);
    unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary']);
}

// Hoook into the 'wp_dashboard_setup' action to register our function

add_action('wp_dashboard_setup', 'remove_dashboard_widgets');

Put that block of code in your functions.php file, if it is a template you’re working on or include it somewhere if it is a plugin.
That’s all there is to it… In my next post I’ll show you how to add your own modules.
Further reading:

http://codex.wordpress.org/Dashboard_Widgets_API

Limit wordpress post content out put automatically


This is a quick and dirty hack to limit the amount of words wordpress outputs on your homepage.

Its sometimes not ideal to have the entire post showing on the homepage when you have really long posts. 10 or 15 long posts will make the page take forever to load, especially if they all have images.

First thing to do is backup your theme’s index.php file. So go to wp-content/themes/your-theme-name/ and make a copy of index.php
just in case you mess it up and forget what the original content was…

Open the file, (the original, not the copy ) and find where it has the_content();
Replace it with:

<?php
$content = get_the_content();//apply_filters('the_content', $content);
$content = str_replace(']]>', ']]&gt;', $content);
$con=explode(" ",$content);
$i=0;
while($i<50 && $i<count($con)){
echo $con[$i]." ";
$i++;<
}
?>
<a href="<?php the_permalink(); ?>" rel="bookmark" title="
<?php the_title_attribute(); ?>">... <br />Continue reading, <?php the_title(); ?>.</a>

Change 50 in the loop to the amount of words you want to allow. When done just save and re-upload the file.

Further reading:
codex.wordpress.org/Template_Tags/the_content

Fix wordpress blank page


Now, I like many others use wordpress. Not just for blogging but for a range of things.

Sometimes it doesn’t work out too well however…like after migrating host.
I set up a new centos distro a few days ago with a simple LAMP stack. After moving over,

This blog didn’t work and I got a blank page with no error. It’s bad when your sites are giving errors
but when you’ve got nothing, that’s when you’re really in trouble.

Going through the logs I noticed this line:

“[Fri Jan 14 07:40:11 2011] [error] [client xxx.xxx.xx.xxx] PHP Parse error:  syntax error, unexpected T_STRING in /var/www/vhosts/crlog.info/httpdocs/wp-config.php on line 14”

Checking the file revealed that somehow the settings on that line got
split into two lines and right in the middle of a php function.
I am pretty sure that is due to editing the file using nano when
I changed the DB settings.

There seems to be a tonne of php notices in the log as well, for websites
that only just got transferred this will be a problem.
The log file is already megabytes in size. I’d advise anyone seeing
similar issues to go through the logs and take a note of the
Path to the file generating the notice,warning or error.
Once done, try finding an alternative method to replace the one causing
the issue. Most notices for deprecated functions
will have the new function to replace your existing one with.

Enough of the ranting though, simple steps to fixing the issue:
1)  Check file permissions, and execute chmod –R 755 httpdocs (or whatever
your directory is called so recursively set permissions to 755)
2)  Enable wp_debug options in wp-config.php by adding a line
with “define(‘WP_DEBUG’,true);”
3)  If you moved from an old host and created a tar file using
tar cf archive.tar * then this does not include your .htaccess file.
You need to create .htaccess on the new host and copy the contents
from the old host or modify your tar command.
4)  Check your log files, as above there may be an [error] somewhere.
5)  If all else fails, it may be your database as discussed on
the wordpress forums.
Check for any suspicious looking enabled plugins.
In wordpress 3.0 and on wards this will be in the wp_options table and
the option_name column will be ‘active_plugins’. You can safely remove
the contents of the option_value for this row. Plugins can always be re-enable
later. For older versions of wordpress, consider upgrading. If that’s no an
option then you will have wp_settings table instead of wp_options.
6)  Move all plugins and themes out of the plugin and theme directories to
somewhere else. Leave the wordpress default theme in place “twentyten” for wordpress 3.0.

There are a few more tip/tricks to debugging and getting around this but
I can’t remember them at the moment. I’ll update this post as I do.

Follow

Get every new post delivered to your Inbox.

Join 350 other followers

%d bloggers like this: