Setting user-agent in curl or wget

$ wget --user-agent="User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:32.0) Gecko/20100101 Firefox/32.0" -c http://www.example.com/file.txt

$ curl -A "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:32.0) Gecko/20100101 Firefox/32.0" -O http://www.example.com/file.txt

Install the current version of Go in Ubuntu

After running:

$ sudo apt-get install golang

I ended up with an older version of Go than I needed:

$ go version
go version go1

Wanting to use scanner from bufio, I needed Go 1.1. Fortunately, you can get the latest version from https://launchpad.net/~duh/+archive/golang/+packages.

Simply run the following:

$ sudo add-apt-repository ppa:duh/golang
$ sudo apt-get update
$ sudo apt-get install golang

Now verify that you’ve got Go updated:

$ go version
go version go1.1.1 linux/amd64

Install PHP mcrypt extension in MacOSX

In an earlier post, Installing Multiple Versions of PHP on Mac OS X with Homebrew-PHP,  I mentioned using Homebrew-PHP to install multiple versions of PHP. I ran into an issue installing Magento due to the mcrypt extension not being loaded.

PHP extension “mcrypt” must be loaded.

Note: I’m assuming here that you have already checked your php.ini file and uncommented the appropriate line that loads the mcrypt extension.

Using Homebrew, it’s a simple process to get mcrypt added to PHP.

First, tap (which may be unnecessary if you used Homebrew-PHP):

$ brew tap josegonzalez/php

And then do the install:

$ brew install PHP53-mcrypt

This also works with PHP 5.4 (PHP54-mcrypt) and PHP 5.5 (PHP55-mcrypt).

You may be prompted to link:

Error: You must `brew link php53' before php53-mcrypt can be installed

You may also get another error, something like:

Error: Could not symlink file: /usr/local/Cellar/php53/5.3.26/bin/phpize
Target /usr/local/bin/phpize already exists. You may need to delete it.
To force the link and overwrite all other conflicting files, do:
brew link --overwrite formula_name

To list all files that would be deleted:
brew link --overwrite --dry-run formula_name

I didn’t have any issues with this and simply ran:

$ brew link --overwrite php53

If you get an error about not having zlib installed, they try:

$ brew tap homebrew/dupes

Restart Apache and with a little bit of luck you should have mcrypt loaded.

Using cURL to Test the WordPress Theme and Plugin APIs

Occasionally (rarely) when trying to search for a theme or plugin via the WordPress dashboard, you may see an error like this:

An unexpected error occurred. Something may be wrong with WordPress.org or this server’s configuration. If you continue to have problems, please try the support forums.

This means that the request WordPress has made to the theme or plugins api has failed, or that the body of the response is bad or empty. Often web hosts will turn off outbound http requests and this will be the source of your problem. However, there can be a myriad of other issues that may cause this error. WordPress will use one of three “transports” and search for them on your server in this order: curl, streams, and fsockopen. Since the focus of this article is on using cURL, that’s what will use at the command line.

To check if cURL is installed on your server, use the Unix ‘which’ command to find it’s install location.

$ which curl

and you should get a response something like this (your path may vary):

/usr/bin/curl

To simply check connectivity with the WordPress theme and plugin apis, you can make an http HEAD request with cURL:

$ curl -I http://api.wordpress.org/plugins/info/1.0/

and

$ curl -I http://api.wordpress.org/themes/info/1.0/

You should see output something like this:

<br />HTTP/1.1 200 OK<br />Server: nginx<br />Date: Tue, 15 Oct 2013 14:06:36 GMT<br />Content-Type: text/html; charset=utf-8<br />Connection: close<br />Vary: Accept-Encoding<br />

If you don’t have connectivity, you may see something like this:

<br />curl: (6) Could not resolve host: api.wordpress.org; nodename nor servname provided, or not known<br />

If you want to duplicate the request made when you’re actually on the WordPress dashboard, you’ll have to make a POST request with serialized data parameters. To mimic a search for a “blue” theme, use this cURL command:

$ curl --data 'action=query_themes&amp;request=O:8:"stdClass":4:{s:4:"page";i:1;s:8:"per_page";i:36;s:6:"fields";N;s:6:"search";s:4:"blue";}' http://api.wordpress.org/themes/info/1.0/

To mimic a search for a “cache” plugin, use this command:

$ curl --data 'action=query_plugins&amp;request=O:8:"stdClass":3:{s:4:"page";i:1;s:8:"per_page";i:30;s:6:"search";s:5:"cache";}' http://api.wordpress.org/plugins/info/1.0/

A successful request will return quite a bit of HTML and serialized data (which I won’t post here).

Contributing to WordPress

Contributing to an Open Source project is fun. (and it’s good for you!) Not only that it’s educational. I can’t tell you how much I’ve learned by answering questions in the WordPress.org Support Forums (you can find me here), doing an occasional bit of work on the WordPress Documentation Team, and writing and releasing a handful of WordPress plugins.

As you can see, there are many ways to get involved and contribute to WordPress. (and I’ve just done a few of them) Whether you’re a WordPress developer, theme author, or just simply a fan or user of WordPress, there’s probably a way you can contribute.

And one important point I’ve neglected to mention…you’ll be helping others and you’ll be helping WordPress! And I don’t have to tell you, but by giving back to the fabulous WordPress community you’ll make the world just a little better place than it was before.

If you’re interested (and I hope you are), I’ve curated a few links to help you get started.

WordPress.org – Codex

Make WordPress

WordPress.org –  Make WordPress Core

General

Core

Documentation

Video

Misc

If you got any additional resources, don’t hesitate to send them my way.

Moderate Use of Shortcodes in WordPress

In a recent question on the WordPress.org forums a user wanted to know if there was a way to prevent non-admins from using shortcodes when creating/editing posts.

The solution I came up won’t prevent non-admin users from entering shortcodes, but it will prevent WordPress from saving them.

Add this to your theme’s functions.php:

function filter_handler( $data , $postarr ) {
    if ( !current_user_can( 'manage_options' ) ) {
        $data['post_content'] = preg_replace( "/\[([^\[\]]++|(?R))*+\]/", "", $data['post_content'] );
    }
    return $data;
}
add_filter( 'wp_insert_post_data', 'filter_handler', '99', 2 );

This will prevent anyone other than admin/superadmin from inserting shortcodes (well, it will delete a pair of square brackets and anything in between when a non-admin inserts or updates a page/post – if there’s a better way I haven’t found it yet).

WordPress Posts Page ID

To find the ID of the page that contains the posts (the listing of posts), you use the WordPress function:

get_option('page_for_posts')

The caveat is that this value will only be set if you assign a static page as your front page and assign another page as your posts page (via Settings->Reading). When you run WordPress where the front page displays your blogs posts, that’s a special case and there is no “posts page” id. (actually, it comes back as 0).

$posts_page_id = get_option( 'page_for_posts' );
echo "page id: " . $posts_page_id;

WordPress 3.6.1: How To Upload swf Files

If you have tried to upload a swf (or exe) file and gotten an error message something like this:

“filename.swf” has failed to upload due to an error
Sorry, this file type is not permitted for security reasons.

That’s because WordPress 3.6.1 has added some new security restrictions in regards to file uploads (http://codex.wordpress.org/Version_3.6.1)

Additional security hardening:

Updated security restrictions around file uploads to mitigate the potential for cross-site scripting. The extensions .swf and .exe are no longer allowed by default, and .htm and .html are only allowed if the user has the ability to use unfiltered HTML.

And while it’s best to take these security changes seriously, in come cases you may need to upload a swf file (or an exe, but I’d never see a need to do that). So if you’d like to allow uploading of a swf file via the Media Manager in WordPress 3.6.1, just add this code to your theme’s functions.php file:

function add_upload_mime_types( $mimes ) {
    if ( function_exists( 'current_user_can' ) )
        $unfiltered = $user ? user_can( $user, 'unfiltered_html' ) : current_user_can( 'unfiltered_html' );
    if ( !empty( $unfiltered ) ) {
        $mimes['swf'] = 'application/x-shockwave-flash';
    }
    return $mimes;
}
add_filter( 'upload_mimes', 'add_upload_mime_types' );

Installing Multiple Versions of PHP on Mac OS X with Homebrew-PHP

When developing WordPress plugins I always test with different versions of WordPress, but I generally just use my default version of PHP (5.3.26 in my case). I had a case where a PHP function had been deprecated in PHP 5.3 and removed from PHP 5.4, so I wanted to test my plugin with PHP 5.3, 5.4, and 5.6. Homebrew-PHP by Jose Diaz-Gonzalez and other contributors couldn’t make it any easier. Just follow the link for some Homebrew goodness:

homebrew-php

Magento – Command Line Installation

You may be like me and have encountered errors trying to install Magento on Dreamhost. Or maybe you’d just prefer to bypass the whole web install. Either way, here’s how you can install Magento via PHP CLI. Of course, you’ll want to replace the path to PHP to match your server environment and update the values of the arguments to work with your setup.

$ /usr/local/bin/php -f install.php -- \
--license_agreement_accepted "yes" \
--locale "en_US" \
--timezone "America/Los_Angeles" \
--default_currency "USD" \
--db_host "mysql.example.com" \
--db_name "your_db_name" \
--db_user "your_db_username" \
--db_pass "your_db_password" \
--db_prefix "" \
--admin_frontname "admin" \
--url "http://www.examplesite.com/store" \
--use_rewrites "yes" \
--use_secure "no" \
--secure_base_url "" \
--use_secure_admin "no" \
--admin_firstname "your_first_name" \
--admin_lastname "your_last_name" \
--admin_email "your_email@example.com" \
--admin_username "your_admin_username" \
--admin_password "your_admin_password"

The install will take some time, so be patient. Once it’s all done (and assuming the installation is successful), you will see output similar to the following:

SUCCESS: 90f02d045c51767ccee093107b2c5eee

Hang on to that string. It’s your Magento encryption key. But if you forget, fear not, it can be found here:

app/etc/local.xml