Doug Sparling

Mobile/Web Developer

Category: php

Delete Magento Orders

by Doug Sparling

require 'app/Mage.php';
$order_ids = array(

foreach ( $order_ids as $id ) {
    try {
        echo "order #" . $id . " has been removed" . PHP_EOL;
    }catch(Exception $e){
        echo "order #" . $id . " could not be removed: " . $e->getMessage() . PHP_EOL;
echo "Done..." . PHP_EOL;

Install PHP mcrypt extension in MacOSX

by Doug Sparling

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.

Moderate Use of Shortcodes in WordPress

by Doug Sparling

In a recent question on the 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

by Doug Sparling

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


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

by Doug Sparling

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 (

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

by Doug Sparling

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:


Installing the Alternative PHP Cache (APC) on Mac OS X

by Doug Sparling

I recently needed to install APC for a Magento project I was working on. I develop on Mac OS X (Snow Leopard), and while APC is generally a simple PECL install (or homebrew), it doesn’t work on OS X:

doug@scriptrunner:~$ pecl install apc

(Lots of verbose output eliminated for brevity)

running: make
/bin/sh /private/var/tmp/pear-build-doug/APC-3.1.9/libtool --mode=compile cc -I. -I/Users/doug/Documents/Projects/edu/php_unit/temp/APC -DPHP_ATOM_INC -I/private/var/tmp/pear-build-doug/APC-3.1.9/include -I/private/var/tmp/pear-build-doug/APC-3.1.9/main -I/Users/doug/Documents/Projects/edu/php_unit/temp/APC -I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/usr/include/php/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /Users/doug/Documents/Projects/edu/php_unit/temp/APC/apc.c -o apc.lo
mkdir .libs
cc -I. -I/Users/doug/Documents/Projects/edu/php_unit/temp/APC -DPHP_ATOM_INC -I/private/var/tmp/pear-build-doug/APC-3.1.9/include -I/private/var/tmp/pear-build-doug/APC-3.1.9/main -I/Users/doug/Documents/Projects/edu/php_unit/temp/APC -I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/usr/include/php/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /Users/doug/Documents/Projects/edu/php_unit/temp/APC/apc.c -fno-common -DPIC -o .libs/apc.o
In file included from /Users/doug/Documents/Projects/edu/php_unit/temp/APC/apc.c:44:
/usr/include/php/ext/pcre/php_pcre.h:29:18: error: pcre.h: No such file or directory
In file included from /Users/doug/Documents/Projects/edu/php_unit/temp/APC/apc.c:44:
/usr/include/php/ext/pcre/php_pcre.h:37: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
/usr/include/php/ext/pcre/php_pcre.h:38: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
/usr/include/php/ext/pcre/php_pcre.h:44: error: expected specifier-qualifier-list before ‘pcre’
/Users/doug/Documents/Projects/edu/php_unit/temp/APC/apc.c:393: error: expected specifier-qualifier-list before ‘pcre’
/Users/doug/Documents/Projects/edu/php_unit/temp/APC/apc.c: In function ‘apc_regex_compile_array’:
/Users/doug/Documents/Projects/edu/php_unit/temp/APC/apc.c:454: error: ‘apc_regex’ has no member named ‘preg’
/Users/doug/Documents/Projects/edu/php_unit/temp/APC/apc.c:454: error: ‘apc_regex’ has no member named ‘preg’
/Users/doug/Documents/Projects/edu/php_unit/temp/APC/apc.c:455: error: ‘apc_regex’ has no member named ‘nreg’
/Users/doug/Documents/Projects/edu/php_unit/temp/APC/apc.c:455: error: ‘apc_regex’ has no member named ‘nreg’
/Users/doug/Documents/Projects/edu/php_unit/temp/APC/apc.c: In function ‘apc_regex_match_array’:
/Users/doug/Documents/Projects/edu/php_unit/temp/APC/apc.c:487: error: ‘apc_regex’ has no member named ‘preg’
/Users/doug/Documents/Projects/edu/php_unit/temp/APC/apc.c:487: error: ‘apc_regex’ has no member named ‘preg’
/Users/doug/Documents/Projects/edu/php_unit/temp/APC/apc.c:488: error: ‘apc_regex’ has no member named ‘nreg’
/Users/doug/Documents/Projects/edu/php_unit/temp/APC/apc.c:488: error: ‘apc_regex’ has no member named ‘nreg’
make: *** [apc.lo] Error 1
ERROR: `make' failed

The issue is that APC is expecting the PCRE headers and libraries to be installed. Fortunately, no big deal. Just head over to:

and grab the latest release. Or here to simply get the latest:

At the time of writing, pcre-8.30.tar.gz was the latest tarball. Nothing new here:

$ tar zxvf pcre-8.30.tar.gz
$ cd pcre-8.30 $ ./configure $ make $ sudo make install

Now you should be able to do the normal PECL install:

$ sudo pecl install apc

Or if you feel so inclined, go ahead and install from source. Grab the tarball here (which happened to be APC-3.1.9.tgz at time of writing):

Once again, nothing new here (other than phpsize, perhaps):

$ tar zxvf APC-3.1.9.tgz
$ cd APC-3.19
$ phpize
$ ./configure
$ make
$ sudo make install

You now need to modify php.ini and add the following line (under Dynamic Extensions if you like) so the apc extension will get loaded at run time:


Now restart apache and you should be good to go.

One more thing (and there always is). APC-3.1.9 has a bug, and you may see this after you’ve restarted Apache:

Fatal error: Unknown: apc_fcntl_unlock failed: in Unknown on line 0

However, another easy fix. Update apc_lock.h (in the APC directory) and modify this line (line 157 in my case).


# define apc_lck_rdunlock(a) apc_fcntl_unlock(&a TSRMLS_CC)


# define apc_lck_rdunlock(a) apc_fcntl_unlock(a TSRMLS_CC)

and then you’ll need to recompile apc again.

$ cd APC-3.19
$ make clean
$ phpize
$ ./configure
$ make
$ sudo make install

Then restart Apache, and voilà, you should be good to go.