Programming

Hide page numbers from wordpress pagination

So you want to hide or style the page numbers in wordpress pagination. The numbers have class .page-numbers but so do the previous and next buttons. paginate_links() doesn’t have any properties that will add a class to the numbers. So how do you do it?
Turns out you can use multiple .not() selectors to select them:
.page-numbers:not(.next):not(.prev){
display: none;
}

Move your OS X Server Documents out of /Library and backup with Backblaze [solved]

As a web developer, the easiest thing for me to develop on my mac was to work from /Library/WebServer/Documents, which is the default directory for OS X’s apache. There are two problems with this: First, the permissions are all root. I found it easiest to change Documents’ owner to my user, but then running a permissions repair would automatically change these back. Second, Backblaze automatically excludes your /Library directory, so none of my web work was getting automatically backed up. I contacted backblaze about including only this directory and got this response:

I am sorry but there is not a way to back up this data.
Backblaze does not back up the system files or the system library. The /Library and all subdirectories are excluded.

My solution was to create a new directory, webdocs, in my home folder, and to move everything that was in /Library/WebServer/Documents into it. Then, I edited /etc/apache2/httpd.conf with root privileges (in terminal: sudo nano /etc/apache2/httpd.conf or sudo edit /etc/apache2/httpd.conf or sudo subl /etc/apache2/httpd.conf depending on your text editor of choice), and replaced all instances of /Library/WebServer/Documents with /Users/jordan/webdocs. Then I ran sudo apachectl restart to restart apache. Now all of my web work will get backed up automatically by backblaze.

Install php-libevent on OS X [solved]

I’m experimenting with Ratchet, a websocket server written in PHP. I wanted to install lib-event for PHP because according to this thread it works better than PHP’s choose_stream function. It ended up being a pain: port install libevent worked but when I ran pecl install channel://pecl.php.net/libevent-0.0.5 it could not find the libevent headers even when I pointed it to /opt/local/lib/, which is where port contents libevent said it was installed.

I ended up following this tutorial on compiling it from source and then pointing the pecl installer to that library and it worked. Mostly. Compiling it from source put it in /usr/local/lib, which the pecl installer liked better. Then it was a question of enabling it in PHP.

OS X’s php.ini installed here:

/etc/apache2/php.ini

MacPort’s PHP is installed here:

/opt/local/etc/php5/php.ini

Unless you change your default setup, apache will serve pages with OS X’s PHP, while the command line will serve pages with MacPort’s PHP.

Most of my OS X PHP extensions are installed here:

/usr/lib/php/extensions/no-debug-non-zts-20090626

Most of my Mac Port PHP extensions are installed here:

/opt/local/lib/php/extensions/no-debug-non-zts-20090626

I had to copy libevent.so from my mac’s extension directory to the macports extension directory, and add extension=libevent.so to both php.ini files.

Free Create Shadow Action in Photoshop

I have created a Photoshop action to automatically generate a shadow for a given png. I recommend extending the canvas right 10 or so pixels first. It expects the file to have a single layer with transparency. It duplicates the layer, adds a black color overlay, rasterizes the layer effect, squishes the layer with free transform, skews it to the right, puts the layer below the first one, sets the opacity to 57%, and hides the first layer.
This is useful for generating custom pin shadows for leaflet, google maps or bing.
Create Shadow.atn
On mac you can place this in: ~/Library/Application Support/Adobe/Adobe Photoshop CS6/Presets/Actions/

Deploy data files only to new website with rsync [solved]

I wanted to copy everything from my git repo to production except images and media. This tutorial helped me out. This command will copy all js, php, html, and css files and directory structure without copying images or other media:

rsync -av --include='*/' --include='*.php' --include='*.html' --include='*.js' --include='*.css' --exclude='*' source/ destination/

This will work in linux, unix, or os x.

PHP DOMDocument stops render on errors

Several of PHP’s DomDocument functions halt rendering the page on any kind of error, and are finicky to test. I was parsing an RSS feed and wanted to display a scaled down version of the image in the feed. This code worked if the code contained an image:
$rss = @file_get_contents('http://problemstosolve.com/?feed=rss2', false);
if($rss === false)
{
$feed = false;
}else
{
$feed = mb_convert_encoding($rss, "HTML-ENTITIES", "UTF-8");
}
$result = new SimpleXMLElement($feed);
foreach($result->channel->item as $item)
{
$content = $item->children('http://purl.org/rss/1.0/modules/content/');
$dom = new DOMDocument();
$dom->loadHTML($content->encoded);
$images = $dom->getElementsByTagName('img');
echo "<img src=\"" . $images->item(0)->getAttribute('src') . "\" width=\"100\" /><br/>";
}

However, if the post didn’t contain an image, the whole page would blow up with no PHP error. To make things worse, if the problem was in one of several codeigniter views, it would make codeigniter display the views out of order. None of these tests for images worked:

//didn't work
if(!$images)
//didn't work
if(!empty($images))
//didn't work
if(!empty($images->hasAttribute('src')))
//didn't work
if(!empty($images->item(0)))
//didn't work
if(!$images->children)
//didn't work
if($images->length() == 0)

What finally worked was:
if($images->length == 0)

getElementsByTagName always returns a DOMNodeList and it’s hard to check to see if it actually contains anything. I haven’t figured out a way for PHP to handle errors better with DOMDocument.

Don’t Run Two Instances of TextWrangler at Once [Solved]

Update:Barebones support got back to me and explained why this wasn’t working, this method broke their codesigning. But they had a solution: Simply go to File > New > FTP/SFTP Browser. Thanks Barebones!


Edit: Darn, I’m getting this error when trying to open remote documents: (MacOS Error code: -25293)

When I’m working on two projects at once, or want to grab a remote file from FTP when I already have one FTP site open, it would be nice to run two instances of TextWrangler at once, since it only supports having one FTP site open at a time.

I figured out how:

-Go to Applications/ and right click on TextWrangler and select Duplicate.
-Rename Textwrangler copy to Textwrangler2
-Right click on Textwrangler and choose “Show Package Contents”
-Open info.plist inside the Contents folder
-Change the data from

	<key>CFBundleIdentifier</key>
	<string>com.barebones.textwrangler</string>



to

	<key>CFBundleIdentifier<</key>
	<string>com.barebones.textwrangler2</string>



Now you can open both TextWrangler apps.


Optionally, grab this snazzy textwrangler alternative icon from this page:
http://sethlilly.deviantart.com/art/TextWrangler-Replacement-Icon-180898652
Right click and choose "copy image". Then get info on TextWrangler2, click on the small icon in the top right, and hit command-V to paste. That way it's easy to tell the two instances apart in the Dock and switching between applications.

MySql Avg() doesn’t work with enums or sets

I had a very simple votes table with a rating column which was defined as a SET(‘1′,’2′,’3′,’4′,’5’). This was meant to be easy to rate items from 1 to 5 stars.
Then I had the following values:

item_id rating
1 3
1 4
1 4
1 5

When I ran SELECT AVG(‘rating’) FROM votes WHERE item_id = 1, I would mysteriously get ‘9’ instead of ‘4’.

It turned out that ENUM and SETs return the index of the ratings, in powers of 2 for some reason, instead of the rating values themselves. So SELECT item_id, rating, CONV(rating, UNSIGNED) produced this:
item_id rating CONV(rating, unsigned)
1 3 4
1 4 8
1 4 8
1 5 16

So the average of the indexes of the set would be 9.

Instead I had to convert the set to a string, then convert the string to an unsigned INT to get the results I wanted:
SELECT rating, AVG( CONVERT( CONVERT( rating, CHAR ) , UNSIGNED ) ) FROM votes WHERE item_id = 1;

Create an encrypted folder with OS X Disk Utility [Solved]

I run several servers on my mac, and worry about getting hacked or accidentally sharing sensitive files. In addition, I worry about sensitive files when I take the computer in for tech support, or when others use my computer. Documents such as my tax files contain my social security number.

I don’t have to worry any longer (until AES is cracked, anyway), because it is easy to create an encrypted disk image on OS X. I followed the instructions here:

http://blog.smalldog.com/article/1814/use-disk-utility-to-encrypt-files/

I chose a 2.5 GB image, which automatically suggested a CD/DVD type partition. I’m not sure what partition type to use but don’t think it matters. These were the settings I used:

This will create a .sparseimage file. Sparse images are nice because they don’t actually take up their entire hard drive space until they’re filled with contents.

Note that after you drag the files in, you still have to delete the originals. Then when you eject the encrypted image, they are protected. When you double click on the image again, it will ask you for your password. Don’t forget this password, because it is impossible to recover!

I used spotlight to search my computer for files containing my passwords or social security number. If they were necessary, I dragged them in here, otherwise I deleted them.

Get Growl Notifications on VNC connections, SSH attempts, and HTTP logs [solved]

Use this script to monitor VNC connections with Vine Server:
nice -n 100 tail -n 0 -f /Users/jordan/Library/Logs/VineServer.log | php -r 'while ($m = fgets(STDIN)) shell_exec("growlnotify -p 0 Vine Server -m ". escapeshellarg($m));'

Change “jordan” to your username (home directory), or the whole path of the log if you’re monitoring something else. “Vine Server” is the alert title, which could be anything (“Hello World”).

Save this as a file such as vncnotify.sh. Run
chmod 755 vncnotify.sh
and then you can run the command by typing ./vinenotify.sh.

Duplicate this file and name it vinenotify.command.
Finally, in System Preferences > Accounts > Start up items, add vinenotify.command, and make it hidden, and it will run when you log in.

Things I’m still working on: preventing a string of notifications when you launch VNC; hiding the terminal window when you log in; and grepping for only connect/disconnect/login failures.
Thanks to drewish.com for the growl tail code.

Next »