WordPress – 301 location to sub page

WordPress is great for blogging but still has a way to go to be a CMS. One thing I find odd is that you can create Categories for Posts but not for Pages. Usually a client will want pages categorized for navigation. So this becomes a problem. Sure you can nest Pages (parents and children) but that means you must have content in every menu item.

After a quick google search I came across some code that allows WP’s Pages to have empty menu items for categorization. Or at least that’s the visual effect. The fix is to create a Template in your theme with some PHP code. It redirects the browser to the first Child Page:

<?php
/*
Template Name: Redirect To First Child
*/
if (have_posts()) {
  while (have_posts()) {
  the_post();
  $pagekids = get_pages("child_of=".$post->ID."&sort_column=menu_order");
  $firstchild = $pagekids[0];
  wp_redirect(get_permalink($firstchild->ID));
 }
}
?>

This works but I’m not too fond of it. It uses a refresh. I tweaked it a bit. Changed the wp_redirect to a Location header and added a 301 status code:

<?php
/*
Template Name: 301 Redirect To First Child
*/
if (have_posts()) {
 while (have_posts()) {
  the_post();
  $pagekids = get_pages("child_of=".$post->ID."&sort_column=menu_order");
  $firstchild = $pagekids[0];
  header("HTTP/1.1 301 Moved Permanently");
  header("Location: ".get_permalink($firstchild->ID));
  exit();
 }
}
?>

This is much faster on the front end. It also tells search engines not to index those blank categorization pages.

<?php/*Template Name: 301 Redirect To First Child*/if (have_posts()) {  while (have_posts()) {    the_post();    $pagekids = get_pages(“child_of=”.$post->ID.”&sort_column=menu_order”);    $firstchild = $pagekids[0];   header(“HTTP/1.1 301 Moved Permanently”);   header(“Location: “.get_permalink($firstchild->ID));   exit();  }}?>

Updated compressed scripty 1.8.3 for LightView, PHP

I ran across a problem with LightView which is stated as requiring Prototype 1.6.1 and Scriptaculous 1.8.2. Yet it was failing even when I had scriptaculous 1.8.3 loaded.

Apparently LightView looks for the loader scriptaculous.js rather than the actual components (effect, builder, etc).

My previous compressed Prototype 1.6.1 and Scriptaculous 1.8.3 did not have the loader/stub scriptaculous.js file. Ideally, that’s never needed as long as you load the other scriptaculous js files.

So here is an updated package. Also in this archive is the PHP version of the ColdFusion file to server the jgz.

The combined file is now 273KB. The load order is as follows:

  • prototype
  • scriptaculous
  • builder
  • effects
  • dragdrop
  • controls
  • slider
  • sound

It includes all comments and credits. I gzipped it down to 62KB.

You’d load it like so:

<script type="text/javascript" charset="ISO-8859-1" src="/includes/js/scriptaculous/scriptaculous.cfm"></script>

Download it: wv-scripty-183.zip

WordPress Plugin, Visual Code Editor, posts and edits code snippets in wordpress’s visual editor

I will be moving my blog from blogCFC to WordPress. So I have it installed and played with skins and syntax highlighting… which is quite lacking.

The problem comes in when you edit your post twice. When you load that post again, WP converts ampersands into “&amp;”. Which is nice. But if you edit that post again, WP re encodes ampersands again. Giving you “&amp;&amp;”. Want to guess what happens if you save and load it again? 🙂

Now lets compound that with the visual editor, which automatically encodes for you as you type or paste. That only accelerates the process. And the fault here is with WP when it loads the post. Not with TinyMCE, at all.
Lets make matters even worse. WP’s init for TinyMCE is really screwy. Looking at the code you’d think they were rocket scientists. But… dang it… the defaults are horrible for programmers who want to blog!

  • Users have to switch to HTML or TEXT views to insert <pre> or <code> tags.
  • TinyMCE does not allow unknown attributes for tags like <pre line=”1″>.

That may sound really picky. But most syntax highlight plugins require either <pre> or <code> and to use special attributes in them to display properly. Some require the syntax to have all entities escaped, some require the opposite.  What a freaking nightmare.  So I set out to end this nonsense.

Enter Visual Code Editor. It’s a plugin I wrote for WordPress. Visual Code Editor makes it possible (and trouble free) to post and edit code snippets in wordpress’s visual editor.

  • Adds <pre> & <code> to block format menu
  • Allows extra attributes for compatibility in some syntax highlighters (ie, <pre lang=”php” line=’5′>)
  • Unescape WP’s double escaping of &
  • v1.1: Removes extra <pre> tags around SyntaxHighlighter Plus’s sourcecode blocks

And the nicest thing is… it’s a plugin! You don’t have to go around editing WP’s base installation. Download, extract, upload, activate, done.Coupled with the SyntaxHighlighter Plus plugin, you get results like this:

syntax highlight 

Syntax highlighter Plus runs through javascript on the client and expects the code to have entities encoded. Thanks to my plugin and TinyMCE, we’re golden. I chose Syntax highlighter Plus for a few reasons. 

  • it’s the easiest to use and is smart.
  • It runs in javascript, search engines do not see tons of inline css muddying the page.
  • The page also downloads faster because of that.
  • The ‘copy to clipboard’ does not require flash.

All you have to do is create a block, select the sourcecode block, select Preformatted from Tiny’s format menu, then paste in your code.

It should look like this:

syntax highlight 

The Preformatted area tells TinyMCE to leave the formatting the heck alone. makes Syntax highlighter Plus do it’s magic on the viewer side. And you can still edit all of your code in the visual editor. Awesome! 

I also left the door open for other syntax highlighters, which instead of using , use <pre lang=”xxx” [line=”5″]> or even <code>, etc.

Feel free to download the plugin, which is now in the WordPress PlugIn Directory! Yippee I learnt how ta use SVN today! http://wordpress.org/extend/plugins/visual-code-editor/

Plugins that failed to help

I did my homework before writing this plugin. I tried all of the following. All of which failed in one way or another.

  • Chili Code Highlighter
  • CodeHighlighter
  • Developer Formatter
  • Disable wpautop
  • Editor Extender
  • EmbedIt
  • Google Syntax Highlighter for WordPress
  • Preserve Code Formatting
  • Raw HTML capability
  • Text Control
  • TinyMCE Entities Patch
  • Code Snippet
  • Dean’s Code Highlighter
  • Highlight Source Pro
  • iG:Syntax Hiliter
  • Sniplets
  • Snippet Highlight
  • WP-CodeBox
  • WP-Syntax

Here are the plugins that I am currently running:

WHEW! That’s enough blogging for today. I gotta get some work done!

Random SQL Server results – a one word command

I learned a great way to randomize SQL results.

In the past, I would have followed this routine:

Run a query pulling all of the results (tens? hundreds? thousands?).
Create an array.
Loop until 5 good random results are made:
Get 1 random query result.
If it’s not in the array, put it in.
If it is in the array, ignore it and try again.
Then I’d output the data from the array, rather than the query.

I suppose I could simply fill an array then randomize it. But that’s not really the point.

As you see, with this version we have to pull back so many more results than we’re actually going to use. That’s wasteful – especially if you want to pull back lots of data in each row.

So what’s this great super duper new way I’ve discovered to simplify code, only pull back the 5 results from SQL Server, and run fast? It’s really simple: tell SQL Server to do the work for you, with one word. No post parsing needed!

Just order the results by NewID(). Yeah, that’s it.

SELECT TOP 5
	id, name, description
FROM
	companies
WHERE
	active=1
ORDER BY
	NewId()

This query will only return 5 random results – even if there are 1000 companies that are active – and in random order.  So you can simply output your query like normal with your server language. Sweet.