Why is my WordPress AJAX form returning -1?

Because you are doing it wrong. But then I guess you already know that. An exit status or return code is a numerical representation of whether a process worked and how it worked. Although it does vary, you won’t go far wrong with this:

  • -1 (or less) – the process failed. An error happened.
  • 0 – there were no errors. But nothing happened, you failed to trigger any change.
  • 1 (or more) – success. The process did something.

How does WordPress handle bad AJAX?

WordPress returns 0 if you do something silly (like forgetting to add the ajax hook). If you write something that actually breaks, then it will just fail silently (you don’t see the error, unless you use something like FireBug). So if you are seeing -1 then the ajax hook itself is explicitly telling you that you are doing something wrong. And whatever that is, it depends on what you are trying to do. Sadly debugging techniques like this fall outside of the scope of my blog. I suggest visiting the WordPress support forums.

Embed private member only content in your posts and pages

You may be running a site where you want to tease people into registering for your content. Perhaps to gain access to a download, or a sure-thing bet for next week’s game. Using a simple shortcode filter we can do this with elegance.

Private content shortcode

function tcb_private_shortcode($atts, $content=null) {
  $default = array( 'alt' => '<em>Private content, login to view it.</em>' );
  extract( shortcode_atts($default, $atts) );
  if( is_user_logged_in() && !empty($content) && !is_feed() )
    return $content;
  return $alt;
}
add_shortcode('private', 'tcb_private_shortcode');

It can easily be extended to include the WordPress login form.

WordPress search – redirect on single result

The days of ‘everything should be at most 3 clicks away‘ have gone. Even so, if you want to save your visitors a click, you can automatically redirect them to their result when the WordPress search returns a single hit.

function tcb_redirect_to_single_search_result() {
  if( is_search() ){
    global $wp_query;
    if( $wp_query->post_count == 1 ){
      wp_redirect( get_permalink($wp_query->posts['0']->ID), 302 );
      exit;
    }
  }
}
add_action('template_redirect', 'tcb_redirect_to_single_search_result');

The wp_redirect() 302 argument is the default value, but I like to explicitly set it. As popular as 301 redirects are, they really do not suit this scenario.

How I built this site (meta blogging)

The purpose of this site has always been more about me learning how to write about technical topics in an engaging fashion. I don’t want to be a journalist or copy writer. However, I do like to be able to express technical things to non-technical people. I would like for everyone to understand their tools in way that is meaningful to them. When someone understands how their tools work, they are better able to use them. To me, there is a level of understanding between knowing how things work, and knowing how to make things.

Making a blogging site

My two main problems are:

  1. I am a slow writer, worrying about all the little things
  2. I love context, and I don’t know when to stop expanding into detail
To overcome these quirks, there are some pretty basic rules or techniques I can apply. These are probably terribly simplistic techniques, and any professional writer of any kind will chuckle at my amateur approach. However, failing is an option!

How to speed up my writing

I chose to try the following things:

  • Jot every idea down, with links and warts in a draft post. Do a brain dump of it.
  • Install a spell checker to stop me being distracted by spelling mistakes and grammar.

How to define my context

This will help stop be babbling, and let me post short snippets of information.
  • Make it all about WordPress. Initially assume I am writing for people who have some prior knowledge about WordPress. Most likely theme development or tinkering.
  • Keep to five categories, and decide what they are, before writing any posts.

Other blog writing techniques

  • Schedule posts. Never post straight away. Initially I thought I would do one a week, but for now I have settled on every four days.

What are the benefits of all this?

Scheduling posts takes the pressure off. If I can log in and see that I have entries in for the next month, then I won’t feel bad about just browsing to see how many hits I’ve had. Dumping my ideas means I can be inspired many times in just a few minutes and get five post ideas note quickly. Once saved as a draft I can re-visit the post and either add detail, or maybe drop it as a bad one.

My five categories are: Rambling, jQuery, Linux, Nginx and WordPress. The last category might be better as PHP, as most of my WordPress work is PHP. However, I didn’t want to exclude HTML and CSS. Having just those categories has helped focus my ideas, and let me not worry about expanding the content to cover other areas. I think singularly that this has been the most useful preparation I made for the site.

I’ve installed jetpack and activated After the Deadline. I don’t actually use it that much, but it has allowed me write what I’m thinking without interrupting myself to fix some spelling. I tend to get fixated on one sentence, making sure it conveys exactly the right message in an unambiguous way that fits into the context of the paragraph and the post as a whole. I know a plugin won’t do that for me, but now I have enough confidence to let a crappy sentence slide until I’ve completed the post.

Additionally I gives me the opportunity to re visit my posts. A month can go by and I can read my post with a fresh pair of eyes.

Am I succeeding?

This is the most enjoyable writing experience I have had all my life. We all have a budding author inside us (apparently), and I seem to be finding mine. And I am learning more about WordPress.