Tag Archives: wordpress

Announcing Todayish In History

My other blog is a garden blog and so old posts have a high likelihood of becoming relevant again each and every year around the same time. I wanted a way to display a link to the blog post written nearest to the current date from each previous year. After not finding a plugin that did what I wanted, I wrote my own.

The plugin is named Todayish because if there isn’t a blog post from today’s
date in previous years it will use the blog post which is the least
number of days away from todays date.

Todayish in History provides a function for use in themes as well as a
widget for use in your sidebar.

Todayish In History’s Homepage

Todayish In History has a permanent home at http://stuporglue.org/todayish-in-history/.

Hopefully this proves to be a useful plugin for someone. Please send me any feedback. You can use the comments, the contact page, or email me at stuporglue@gmail.com.

Screenshots

 

Demo

You can see Todayish In History in action at the top of every page over at The Fridley Farmer.

Posted in Computers, Programming, Projects, Something Interesting, Todayish In History Plugin | Tagged , , , | Leave a comment

Moving a single WordPress Category to a New Site

I recently moved the Fridley Farmer content from this site over to http://fridleyfarmer.com, and wanted to move all of my content over there too. There are plenty of instructions on moving wordpress sites available online, I did a full database dump, copied all the files, then deleted the non Fridley Farmer content from the new site. You can also use the Import/Export functionality to export just a single category, but I didn’t know if that would include my pictures and comments as well, so I just copied everything.

What wasn’t obvious however, was how to redirect users to my new content! I didn’t want real users visiting stuporglue.org for the Fridley Farmer content anymore, but I didn’t want to send EVERYONE (like you!) over to the new site. I also didn’t want Google thinking that my new site just had duplicate content.

I ended up making three changes to my wordpress install in order to make the transition as seamless as possible.

 

Header Redirects on Single Posts and The Cateogry

In theme’s header.php I added a short bit of PHP that would detect users trying to visit the Fridley Farmer category archive, or any post in the Fridley Farmer category. It performs a 301 Moved Permanently HTTP header redirect. Easy peasy.

<?php
if( is_single() && in_category('Fridley Farmer') ){
 header ('HTTP/1.1 301 Moved Permanently');
 $newsite = "http://fridleyfarmer.com" . $_SERVER['REQUEST_URI'];
 header ('Location: '. $newsite);
}else if( is_category('Fridley Farmer') ) {
 header ('HTTP/1.1 301 Moved Permanently');
 $newsite = "http://fridleyfarmer.com" . $_SERVER['REQUEST_URI'];
 header ('Location: '. $newsite);
}
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html ...

Replace Category Content With a “We’ve Moved” Message

I edited the code in wp-includes/post-template.php to modify the post’s content as it’s returned.

I changed:

function the_content($more_link_text = null, $stripteaser = 0) {
   $content = get_the_content($more_link_text, $stripteaser);
   $content = apply_filters('the_content', $content);
   $content = str_replace(']]>', ']]&gt;', $content);
   echo $content;
}

Into:

function the_content($more_link_text = null, $stripteaser = 0) {
 if(in_category("Fridley Farmer")){
   $content = "<p>The Fridley Farmer has moved its wagon down the road to
   <a href='http://fridleyfarmer.com'>http://fridleyfarmer.com</a>. All of our
   belongings, posts and content have arrived safely, and we're just waiting for
   you!.</p>
   <p>Stuporglue.org will remain as a place for all my programming and other
   non-gardening related ramblings.</p>
   <p>So, update your links, tell all your friends and come check out the new site!</p>";
  } else {
   $content = get_the_content($more_link_text, $stripteaser);
   $content = apply_filters('the_content', $content);
   $content = str_replace(']]>', ']]&gt;', $content);
  }
  echo $content;
}

Replace Post Title Links With Link to New Site

In wp-includes/link-template.php I edited the very end of the function get_permalink. I made it so that links to Fridley Farmer content go to the new site.

return apply_filters('post_link', $permalink, $post, $leavename);
}

Into:

$finalurl = apply_filters('post_link', $permalink, $post, $leavename);

  require_once('category-template.php');
  if(in_category('Fridley Farmer',get_the_ID())){
    $finalurl = str_replace(home_url(),'http://fridleyfarmer.com',$finalurl);
  }

return $finalurl;
}

I hope that helps someone out. It wasn’t difficult, but I couldn’t find how to do something like this documented anywhere.

It will probably get overwritten the next time I upgrade WordPress, but hopefully by then everyone who needs to know to go to the new site will already have done so.

Posted in Something Interesting | Tagged , , | Leave a comment

Show the WP e-commerce shopping cart widget only on store pages

I’m using the WP E-Commerce plugin on a site I’m working on, and didn’t like the default cart options. The only options were to hide the cart when empty, or always display it.This might make sense on a primarily e-commerce site, but e-commerce is just a small sliver of the purpose of the site I am working on.

Instead, I wanted to display it a) Always when the user is on a store related page and b) on other pages only if the cart wasn’t empty.

This involved such a simple change I thought I would post it here for others to enjoy.

 

UPDATED to work with WP E-Commerce 3.8′s Shopping Cart Widget

The file to edit is now wp-content/plugins/wp-e-commerce/wpsc-widgets/shopping_cart_widget.php. You should now edit the widget() function at line 33.

Add this right at the top of the function:

if(preg_match('/^\/store\//',$_SERVER['REQUEST_URI']) === 0 && wpsc_cart_item_count() < 1){
      return;
 }

It looks like the hideonempty option is gone. The widget() function just does a bunch of echoes that print out the widget HTML/JavaScript. We now just do a quick check to see if we’re in the store, or if we have some items, and return immediately if we don’t.

In your file wp-content/plugins/wp-e-commerce/widgets/shopping_cart_widget.php on line 8 change

if(($options['hideonempty']== 1) && (wpsc_cart_item_count() < 1))

into

 if(preg_match('/^\/store\//',$_SERVER['REQUEST_URI']) === 0 && ($options['hideonempty']== 1) && (wpsc_cart_item_count() < 1))

Alternatively, if you only ever want to show it on store pages, change the line to

 if(preg_match('/^\/store\//',$_SERVER['REQUEST_URI']) === 0 || (($options['hideonempty']== 1) && (wpsc_cart_item_count() < 1)))

And that should do it!

PS. If you’re looking for a WordPress compatible cart, WP E-Commerce does, unfortunately, seem to be the easiest to use. I say unfortunately because it’s not as easy to set up as most WordPress plugins and the support is somewhat lacking. I have found it adequate but am interested in other, better, cart options if someone comes across one.

Posted in Computers | Tagged , , , , | 9 Comments