A few Wordpress Tricks.

Just a few little tips for Wordpress:

If you don’t want the titles of posts to have the » character before them for SEO purposes just use:
<?php echo trim(wp_title('&raquo;',false), '&raquo; ');?>

I was sick of my index.php being used to display pages, so I added the following to the top of my index.php to force Wordpress to use single.php for all pages:

  1. <?php
  2. if(is_page()){
  3. include('single.php');
  4. exit;
  5. }
  6. ?>

I wanted to be able to use javascript in some of my pages so I added the following to my header.php:

  1. <?php
  2. if(is_page()){
  3. $j = get_post_meta($post->ID, 'header', true);
  4. if($j != ''){
  5. echo $j;
  6. }
  7. }
  8. ?>
  9. Use the following to copy and paste the code.

Now anytime I need special style or javascript code added for a particular page I just add a custom field with the name header.

Have you ever tried to validate your Wordpress site to use with Google Webmaster Tools? It can be a pain depending on the way your permalinks are, and even if those don’t cause a problem when Google tries to go to a random URL if the header isn’t 404 then it fails.

Continue reading. »

Redirecting a large number of posts to their new Wordpress permalinks.

Add this to the VERY top of your theme’s 404 page.
Change http://localhost/wordpress to your default wordpress folder.
This just does posts and possibly pages that end in the id (with or without the trailing url) Categories etc will have to be done elsewhere or by hand.

  1. <?php
  2. //Kinda obvious
  3. $rf = $_SERVER['REQUEST_URI'];
  4. $rf = explode('/',$rf);
  5. $last = count($rf);
  6. //Filter out regular 404 that arn't old urls
  7. if(is_numeric($rf[$last-1])){
  8. $post_id = $rf[$last-1];
  9. }elseif(is_numeric($rf[$last-2])){
  10. $post_id = $rf[$last-2];
  11. }
  12. // No sense loading WP if there isn't an id
  13. if ( $post_id ) {
  14. // Oh fine we will load wordpress
  15. define('WP_USE_THEMES', false);
  16. require('http://localhost/wordpress/wp-blog-header.php');
  17. $post = get_post($post_id);
  18. //Well hope it is the right post. If not, ohwell. We tried. if($post){
  19. //Lets get the new URL
  20. $new_link = get_permalink($post_id);
  21. //We don't want google to get angry with us. header("HTTP/1.1 301 Moved Permanently");
  22. header("Location: $new_link");
  23. }
  24. }
  25. ?>
  26. Use the following to copy and paste the code.