Jan 212013
 

For some reason wp_redirect/wp_safe_redirect in WordPress can be a total pain, whether it be when writing custom front end code, plugins or widgets I seem to come across an issue with redirects periodically, with the utterly frustrating “Cannot modify header information – headers already sent” error.  As a heads up, this is caused as the name suggests by  content in a header being pushed down before the redirect is called, this is a generic issue not one that is specific to WordPress.  More often than not it is simple to identify, if any of the involved PHP files have leading or trailing white space, and or there’s some hidden debug output you have forgotten.  Occasionally the issue is WordPress however, in my case the issue of wp_redirect not working on a custom admin page, was caused by a function in

/wp-admin/includes/template.php:1636 or so, whereby headers/html, quite a lot of them are indeed sent as part of the page load process.

Having ensured all of my files were trimmed and there was nothing wrong with my plugin code, I discovered here that there’s a little workaround for forms being called in custom admin pages, that is simply to append:

&noheader=true

To your action, e.g.

<form method=”POST” action=”?page=your-plugin-page&noheader=true”>

This resolved my problem instantly – understandably given it suppressed the content being produced in template.php!

Hope it helps anyone having problems with wp_redirect and wp_safe_redirect on custom admin pages in WordPress.

 

 

Nov 022011
 

WordPress is fantastic, what else can you say.  The new custom post types and taxonomies make it a super powerful system that can handle the vast majority of average web content management requirements simply and quickly.  It does have its quirks to be sure, and one of them is the tendency for it to screw up it’s permalink cache at times producing random 404 errors on pages that were working to that point.  This often happens when you change slug values in your functions.php but it can certainly happen at other times.  If this happens the quickest way to sort it (normally) is to reset your permalink structure to the default then reapply any custom paths you have defined.  Volia, old pages back on track, no more random 404s on WordPress.

Jan 202010
 

.htaccess problems and can be a real pain, especially if like me you only do things with the .htaccess file every now and then so aren’t completely comfortable with the syntax.  Last night when helping a friend and author of the excellent ExchangeWire (about tracking the nascent ad exchange and ad trading networks) switch a domain, I configured the .htaccess file using a standard format:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^oldomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www. oldomain.com$
RewriteRule ^(.*)$ “http\:\/\/www\.newdomain\.com\/$1” [R=301,L]

I say “a” standard format because as anyone who has used .htaccess before knows, there are a few variations on this syntax that do the same thing.  Not thinking about it much I put this in the root .htaccess of the WordPress install, I tested the top domain, worked fine, but the pages themselves just kept redirecting to the the root, not the actual page.  My complete .htaccess file that was not redirecting properly:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

RewriteEngine On
RewriteCond %{HTTP_HOST} ^oldomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www. oldomain.com$
RewriteRule ^(.*)$ “http\:\/\/www\.newdomain\.com\/$1” [R=301,L]

Ok so the clued up amongst you will realise the problem was caused because of the ordering of the commands I had put in the file, this is important. In terms of importance, the domain redirection had to happen first, so I needed to put it above the WordPress command otherwise the WordPress command hijacked the process and confused the primary redirect.  So, the lesson is be careful of the order you put things in and the potential conflicts that can occur as a result.  Keep the major redirects/rules at the top of the page (e.g. when you want to change domains) and the more detailed one(s) below for more consistency (such as WordPress redirects).

Oct 242009
 

As you may have noticed the information on this blog is mainly about technical stuff and contains code references.  To make these clearer I use a plugin that highlights and formats the code and makes it available for cutting and pasting.  I looked at a few but without doubt the best code/syntax highlighter plugin for wordpress blogs that I could find is one called the “Syntax Highlighter and Code Colorizer for WordPress” by a guy called Vijesh Mehta.  All I can say Vijesh is that you rock!

This plugin integrates Alex Gorbatchev’s Syntax Highlighter Tool and will highlight the syntax of:

  • Bash/shell
  • C#
  • C++
  • CSS
  • Delphi
  • Diff
  • Groovy
  • JavaScript
  • Java
  • Perl
  • PHP
  • Plain Text
  • Python
  • Ruby
  • Scala
  • SQL
  • Visual Basic
  • XML

Which for the grand total of $0 is a pretty sweet deal!  Here’s a PHP example so you know what it looks like.

<?php
echo 'Hello world';
?>

I gave the plugin full marks on the WordPress plugin site, I’m sure everyone that uses it will.