Updating a WordPress database with new domain details

Further to my original quick db update queries, I found the need for a more in-depth approach.

A WordPress site my father works on has weekly backups, but he wanted to see that they actually worked.
To do so, I set up a subdomain vhost on my bytemark server and set about getting the back to work with it.

One of the most annoying/lazy/strange things about WordPress is that it uses serialised arrays within database fields. This makes a simple search and replace fail if you need to do anything more than update the two fields here.

This script works for the database set-up for my father. It may require further tweaks if additional plugins store absolute paths or URLs.

It also could do with a heavy dose of refactoring, but it works for my needs at this point in time 🙂

PPTP tunnels and if-up.d

Current project requires a couple of VPN tunnels to be set up. An IPsec and a PPTP.
The IPsec will wait for another day, the PPTP is set up and ready to go.
One thing that wasn’t quite right with the tunnel though, is the static route created by PPTP missed out a large chunk of the server’s network.

Starting the tunnel sets up the routing rule of 192.168.100.111 to be sent to the tunnel.

This meant that if I wanted to get to 100.10, it wasn’t being sent.

The way around this was to add a new route: route add -host 192.168.100.10 dev ppp0

Works great from the CLI, nothing happens when added to /etc/network/interfaces or /etc/network/if-up.d/foobar.

Found out (trial and error) that it was trying to add the route as soon as the tunnel was created. This doesn’t work as the tunnel takes a few seconds to wake up.

Work around was to add a ‘sleep 5’ to /etc/network/if-up.d/foobar before the route command line.

Creating race conditions isn’t nice, but it works 🙁

Symfony 1.4 Doctrine 1.2 MS SQL Server

Web server: Linux (Ubuntu on my dev setup), Apache, PHP 5.3, Symfony 1.4, Doctrine 1.2.
Database server: Microsoft Windows 2008 Server, MS SQL Server

Trying to get Symfony to talk to the database server has been a painful experience for the last few days. But perseverance has paid off.

Lots of Googling with trial & error has resulted in actually achieving a development setup that will mirror the eventual production setup.

Short version:

  1. Follow the FreeTDS and ODBC setup instructions of http://jamesrossiter.wordpress.com/2011/03/08/connecting-to-microsoft-sql-server-using-odbc-from-ubuntu-server/
  2. Use the following in config/databases.yml

In the above snippit, replace ‘datasourcename’ with whatever you used in /etc/odbc.ini and replace ‘sqlserver’ with the name used in /etc/freetds/freetds.conf & /etc/odbc.ini

It’s late and I’ve been struggling to get this working for some time. I may expand this entry in the future if required.

References and insperation:
1) http://blog.acjacinto.com/2011/11/compiling-php-with-mssql-servers-native.html
2) http://www.microsoft.com/download/en/details.aspx?id=28160
3) http://jamesrossiter.wordpress.com/2011/03/08/connecting-to-microsoft-sql-server-using-odbc-from-ubuntu-server/
4) http://trac.symfony-project.org/wiki/HowToConnectToMSSQLServer

 

Google calendar in Mozilla Thunderbird on Ubuntu 11.10

Via the Ubuntu Software Centre:

  1. Search for thunderbird
  2. Click on the search result “Thunderbird Email” and then on the “More Info” button
  3. Enable at least the “Calendar Extension for Thunderbird – Google Calendar support (xul-ext-gdata-provider)”
  4. Install the add-ons
  5. Open your Google Calendar in your web browser
  6. In the left-hand column, under “My calendars”, hover over the calendar of choice and click the down arrow that appears after the calendar name
  7. Select “Calendar settings”
  8. Close to the bottom of the page will be the “Calendar ID” (in my case it is my full email address. Make a note of this ID.
  9. Start up Thunderbird upon completion.
  10. File -> New -> Calendar
  11. Select “On the Network” and click Next
  12. Select “CalDAV”
  13. In the Location field enter: https://www.google.com/calendar/dav/calendar.id.noted.in.point.8/events
  14. Click Next then enter a name for the calendar and set an email account against it
  15. When prompted, enter your username and password for accessing this calendar

Fixing odd characters

One of the biggest complaints that web developers have is their lack of control over end users.

When will we be able to tell them a) not to paste Micosoft Word or b) if you must, paste it into something that doesn’t do formatting 1st.

It has a nasty habit of converting quotes into “smart” quotes as well as messing up characters such as the euro symbol €.

This is the fix I came up with that allows end users to continue to paste from such programs and bring their weird charset issues with them:

Downloading SoundCloud Playlists

[Update 21st Aug 2013
Post code replaced with a Github GIST.

Please fork and help improve this script 🙂

 

[Updated 7th Feb 2012
Changes:
+ file names are now as they would be if you downloaded via web browser
+ Playlist is now in reverse date uploaded order]

I like to have music while I’m coding. It breaks the silence while working alone at home.

When I have music playing, it’s best if I don’t have to think about setting up playlists, which is why I used to listen to the radio. The radio has it’s limits though, not enough Ninja Tune I think.

Speaking of Ninja Tune, I <3 their SolidSteel mixes http://soundcloud.com/ninja-tune/sets/solid-steel-radio-shows/

What I don’t like is having to listen via my web browser and their music player in Flash. A recourse hog and not great if you want their music on a device disconnected from the ‘net.

Thankfully, with SoundCloud tracks, you can download any file you wish. The downside to it is that there’s a lot to download. What if you want a whole playlist? A playlist of (currently) 151 tracks for instance?

The script below will download all of the tracks in a set to a directory and create a .m3u playlist at the same time.

Please excuse the justified text and syntax-highlighting gone wrong. View plain, or copy to clipboard and it’ll be fine.

wp-block as a shortcode

In my last post I raved about the excellent plugin wp-blocks by Keir Whitaker and then went on to extend it a little.

Time to extend it a little further, this time by adding a shortcode.

Shortcodes are the handy square bracketed code snippets which conjure up more content and functionality direct from a page or post content.

The wp-blocks plugin is currently intended to work with the theme template files in PHP. Unfortunately (or fortunately?) PHP does not get parsed within the content areas. This does stop malicious code from being included, but it does limit things when you know what you’re doing.

The following code will give you a short code to work with which looks similar to the PHP code snippit you would include in your theme template file.

Add that to your theme’s functions.php file and then you will be able to add blocks to your content with, for example:

Getting a random wp-block by regex

If you’re used to developing with a CMS such as CMS Made Simple, you would be quite used to having blocks of content separate from the main content system. These blocks can be re-used through-out existing content and within the theme templates.

One might even consider this a core function for a CMS. Sadly it’s missing from WordPress. Keir Whitaker has been working on this missing option and gives us wp-blocks.

It’s a great little plugin, though it currently has a couple of niggles:

  • Apostrophes are escaped in the output: ‘
  • One can’t call a block from inside content, it has to be from the theme template.

Despite this, I’m glad that he’s made the plugin available for public use.

Now, time to extend it!

As with global content blocks in CMS Made Simple, wp-blocks does not offer a way to fetch a random content block. I fixed this in CMS Made Simple by writing the Random Global Content Block plugin.

Now I’ve done something similar for wp-blocks:

Copy that code into your theme’s functions.php and then call from within your template with, as an example:

This will then fetch a wp-block with a name starting with testimonial_.

Mimicking wordpress.com’s image resize URIs

To follow up from last night’s entry, I was determined to remove the intermediary step of my_resize_script.php.

I’ve acheived this goal in the mod_rewrite rule, file name and query string now get passed on to timthumb.php.

Updated rules:

This will pass both width and height variables into timthumb if they are declared.

Quick breakdown of the rules:

  1. Check that the URI exists as a file.
  2. Check that the URI in question is an image. Expand to other formats as needed.
  3. Check that one or both of width and height are being altered.
  4. Pass the file following to timthumb as variables:
    $1 = filename
    %1 = w or h
    %2 = value of w or h
    %3 = w or h
    %4 = value of w or h

If conditions 1 to 3 all return true, then the rewrite rule in 4 will be executed.

In summary, the URI
http://myrant.net/wp-content/uploads/2010/03/high_res_horsey.png?w=300&h=100

Will be passed on as
http://myrant.net/timtumb.php?src=wp-content/uploads/2010/03/high_res_horsey.png&w=300&h=100

Resize images on the fly without messing with image URLs

[EDIT: I’ve made this simpler with a follow up post: Mimicking wordpress.com’s image resize URIs]

Exporting a wordpress.com site for use on a standalone wordpress.org install is a joy to set up. The export and import system are simple to use and give little or no issues.

What is a problem is the automatic resizing of images that wordpress.com offers.

One can insert an image in the normal way and then append the image URI with some variables such as width (w) and height (h). This is also a d options, I assume this is for depth, but haven’t looked into it yet.

So an image URI might be http://myrant.net/wp-content/uploads/2010/03/high_res_horsey.png?w=300

This URI would display the image resized to 300px wide and retain the aspect ratio.

Currently, wordpress.org do not offer this facility (that I have seen). So I’ve been working on how it might be done:

1st off, we need to set up a mod_rewrite rule to cater for image URIs that have query string variables. This can be added to a vhost config or the site’s .htaccess file. I prefer to use .htaccess

Then we need a script (my_resize_script.php) to make use of the variables.

The output of this script as it is would be:

As you can see, I’m not actually making use of the variables yet. But all of the information that it does extract can be given to a proper resize script, such as TimThumb.

This is all a bit messy right now. I would much rather be able to pass variables directly into a resize script rather than have to use an intermediary one. If anybody wants to give some pointers, please do 🙂