Wishful Coding

Didn't you ever wish your computer understood you?

Hidden Pages in Wordpress

More often that not, I need to hide pages from the menu and search pages, when setting up a Wordpress blog.

Usually for download and thank-you pages, or other stuff that is only accessible via a direct link.

By default, Wordpress includes a visibility settings, which has 3 values.

  • Public – anyone can see it.
  • Protected – Anyone with the password can see it.
  • Private – Only you can see it

None of them is useful for my goals, however, some digging revealed that a small change would allow you to link directly to password protected pages.

Copy wp-pass.php to unlock.php, and make the changes below.

--- wp-pass.php	2011-09-19 05:17:26.000000000 +0200
+++ unlock.php	2012-01-02 17:38:47.000000000 +0100
@@ -10,8 +10,8 @@
 require( dirname(__FILE__) . '/wp-load.php');
 
 // 10 days
-setcookie('wp-postpass_' . COOKIEHASH, stripslashes( $_POST['post_password'] ), time() + 864000, COOKIEPATH);
+setcookie('wp-postpass_' . COOKIEHASH, stripslashes( $_GET['post_password'] ), time() + 864000, COOKIEPATH);
 
-wp_safe_redirect(wp_get_referer());
+wp_safe_redirect($_GET['return']);
 exit;
 ?>

Now you can simply link to http://example.com/unlock.php?post_password=foobar&return=/thank-you/

Pepijn de Vos

Lego NXT With a Gamepad

I built this robot a while back, but I can’t stand the software that comes with the NXT, my own compiler isn’t ready yet, and any other remote controls I found didn’t work particularly well, if at all.

If you want it done right, you have to do it yourself – Bernd Paysan

So I threw together PyGame and nxt-python, and wrote my own little application to control the NXT. Notice that it looks very similar to this PyMouse code

I have no building plans for the robot, it’s the standard wheel base with my own pincer. I do have the code:

import pygame
from nxt import locator, motor
from time import sleep

# edit this to reflect your joystick axis and buttons
axis = {'x':0, 'y':1, 'x*':3, 'y*':5}

b = locator.find_one_brick()

left = motor.Motor(b, motor.PORT_B)
right = motor.Motor(b, motor.PORT_A)
action = motor.Motor(b, motor.PORT_C)

closed = False

def limit(nr):
    if nr > 50 or nr < -50:
        return min(127, max(-128, nr))
    else:
        return 0

def move(fwd=0, turn=0):
    lp = int((fwd - turn) * -100)
    rp = int((fwd + turn) * -100)
    left.run(limit(lp))
    right.run(limit(rp))

def pincer(button):
    global closed
    try:
        if button and not closed:
            closed = True
            action.turn(-40, 70, emulate=False)
        elif not button and closed:
            closed = False
            action.turn(30, 70, emulate=False, brake=False)
    except motor.BlockedException:
        print action.get_tacho()

pygame.init()
j = pygame.joystick.Joystick(0) # first joystick
j.init()
print 'Initialized Joystick : %s' % j.get_name()
try:
    while True:
        pygame.event.pump()
        sleep(0.1)
        
        # get_axis returns a value between -1 and 1
        move(j.get_axis(axis['y']), j.get_axis(axis['x']))
        pincer(j.get_button(0))
        
except KeyboardInterrupt:
    j.quit()
Pepijn de Vos

Twisted on Dotcloud

A while back, I wrote a Twisted email server that would sit between you and Twitter and let you reed and send tweets via email.

With the advent of free cloud hosting, I thought it’d be fun to put it online. I’ll share with you how it’s done, for those of you who are looking for a place to put your Twisted app.

The major problem with these easy hosts is that they assume a simple WSGI app or a background worker, but then the background worker is not accessible from the outside.

First I tried Heroku, then Dotcloud, and finally a couple of others, and just when I had given up, @solomonstre came in:

@pepijndevos dotcloud has beta support for arbitrary tcp/udp ports. Want to try? :)

After some fruitless tries, he shared this repo, which contains scaffolding for a web-accessible Python worker.

The core parts of this thing are dotcloud.yml, where you define a setup script, the ports you want, and the command to run your app.

worker:
  type: custom
  buildscript: builder
  ports:
    smpt: tcp
    pop3: tcp
  process: ~/run
  approot: twemail
  environment:
    PYTHON_VERSION: 2.7

builder contains a whole lot of pip/virtualenv code, while run contains something like twistd -ny yourapp.tac.

Pepijn de Vos