Articles

Wordpress Post Friendly Code With JavaScript Replace

I recently talked about adding code to blogs and comments to Wordpress and making sure that certain characters are encoded properly. So to simplify things I thought I would create a little set of regular expressions that takes a sample of code and convert it into a Wordress friendly format. It consists of the following function, which takes the value of a text area called tochange and runs some regular expression replace functions on it. I have kept the expressions as simple as possible so they are quite easy to understand. The g argument for each expression means that the replace will be done for all of the text.

Irritating JavaScript Virus Message Popup And Redirect

The other day I was approached by a friend who had this odd looking virus message on their screen. They said that they hadn't been doing anything in particular, just writing an email and surfing the net in Firefox when all of a sudden this pop-up appeared on screen and told them they had a virus.

The thing that caught my friend by surprise was that they were using Firefox and therefore shouldn't be able to get pop-ups. However, on closer inspection is appeared to be a JavaScript confirm message. My friend clicked on the Cancel button and one of the pages they had open redirected to this very dodgy looking site which promptly started to do a dummy virus scan.

Write To The Output Buffer In PHP

The first thing you learn about in PHP is probably how to print something. This is usually done with a call to the echo or print, but there is another way to print things by writing content directly to the output buffer. The following code looks like you are writing to a file, but the text will appear in the browser window because we are writing to the php://output output stream.

$fp = fopen("php://output", 'r+');
fputs($fp, "Hello World");

Or another way...

file_put_contents("php://output", "Hello World");

The php://output stream is an encapsulation between PHP and the browser. The stream doesn't really exist, but PHP knows what to do with it.

Things To Know About If Statements In PHP

Going back to basics can be useful, even for the most experienced developers. Different languages act and respond in different ways and knowing exactly how some control structures work can be very useful in the long run.

I remember when coding in VB (some years ago now) and realizing that an if statement didn't work like I had expected it to work. I had expected that as soon as a condition was met the code would execute whatever was in that condition and drop out of the if statement. What was actually happening was that the code for any condition met was being executed. I'm not sure if VB still works like that, but it made me realize that a developer must know these things or they could write a system crippling bug without even realizing it.

PHP if statements have been written with efficiency in mind. If a condition is met then the code in the condition is run and the rest of the if statement is skipped. Take the following simple example:

Adding Code To Wordpress Blogs And Comments

Wordpress is a pretty neat blogging platform, but it falls over quite spectacularly when trying to write code in posts. I write a lot of code for #! code and so I have understand what needs to be encoded to make code examples work.

For code example on #! code I use the code tag and I encode the following characters.

< into <
> into >
" into "
' into '

Note: You must be in HTML mode in your Wordpress editor or everything will be double encoded.

Typewriter Script

The following script can be used if you want to simulate a typewriter in an element on screen. I have put in a lot of comments to describe what is happening but the script works by taking each array element in turn and adding it character by character to the content of the selected element.

Using Redirection Inside a Plugin In Zend Framework

I had a situation the other day where I had an application in Zend Framework and I wanted to redirect a user to another page. This is fine if you are inside a controller as you can use the _redirect() controller helper, but in this instance I was running the code from inside a plugin and so therefore didn't have direct access to the controller.

The solution is to use the getResponse() method, which is accessible to plugins, and which will retrieve the response object. The response object has a function called setRedirect() that is used to redirect. Any headers that have been issued will be overwritten by this function. The following code can be run inside your zend framework plugins to redirect the user to a different page.

Paamayim Nekudotayim In PHP

What? Don't worry, I can't say it either. It is officially called the Scope Resolution Operator (but also just a double colon) and is used to reference static properties and functions of a class. It is also used to reference overridden functions of classes.

To reference a constant of a class you do something similar to the following.

class MyClass {
 const CONST_VALUE = 'A constant value';
}
 
echo MyClass::CONST_VALUE;

To call a static function or a parameter you need to include the word static in the function or parameter definition. You can then reference this function through the scope resolution operator.

Remembering Authenticated Sessions With Zend Framework

After setting up your session management in your application using one of the Zend_Auth adapters you might want to allow users to stay logged in. What you need to do is set some configuration options in the Zend_Session object. Zend_Auth uses Zend_Session as an object orientated way of manipulating the $_SESSION variable. Any changes you make to the Zend_Session object will affect the Zend_Auth object, as long as you set these options before the sessions are started.

There are a number of configuration options available, but for the effect I was looking for I only needed to change the ones below. You might not need to set all of these, but it gave me the best cross browser behaviour.

7 Tips To Improve Website Usability

Website usability should be an essential part of your online strategy, but it is so easily overlooked or overshadowed by pretty design. Here is a quick list of 7 things that can cause your users to get frustrated and go elsewhere.

1. Search
Search is a very important part of any site, and if it doesn't work then your users will just get frustrated and go to another company. Website search usability comes in two parts, the search box itself and the results.

When creating a search box you need to have a single text box and a button labelled "Search". That is about as complicated as you need to go, although it is okay to have a drop down box of categories, as long as the default is "All Categories". When the user has done a search, the search box should contain what they searched for, so that they can refine their query if the need to.