Tag Archive for php

Mavericks Broke My Apache!

Note: Instead of doing everything below, you could just restore the backup of your httpd.conf that the Mavericks install created at /etc/apache2/httpd.conf~previous. Wish I knew that 8 hours ago.

Because I like shiny new things, I decided to upgrade to Mavericks (OS X 10.9) last night.  The upgrade finished around 1am, at which point I was tired, so I went to sleep.  I woke up this morning to start my day, only to realize that all of my Apache Virtual Hosts were throwing 403: Forbidden errors.  After a couple hours of chmod, chown, editing conf files and banging my head against the wall, I finally got it working.  So if this happened to you, hopefully the following steps will help.

Hooking into Widget Delete Action in WordPress

The title of this post is technically a lie, since there is no widget delete action (that I’m aware of anyway).  However if you want to hook into the action that occurs when a widget is deleted you can use the following code in your functions.php :

add_action( 'sidebar_admin_setup', 'my_sidebar_admin_setup' );

function my_sidebar_admin_setup() {

     if ( 'post' == strtolower( $_SERVER['REQUEST_METHOD'] ) ) {

          $widget_id = $_POST['widget-id'];

          if ( isset( $_POST['delete_widget'] ) ) {
               if ( 1 === (int) $_POST['delete_widget'] ) {
                    // Widget deleted; do something
               }
          }

     }

}

The ‘sidebar_admin_setup’  action gets triggered, seemingly, anytime a change occurs on the Widget screen.  Dragging a widget, saving a widget, deleting a widget – all of these trigger this action.  It’s a pretty handy hook to know.

PHP Simple HTML DOM Parser

PHP

I discovered this little beauty awhile ago, but used it once then forgot about it. I rediscovered it today while working on an awesome WordPress theme framework that I’m developing. However I ran into some issues with it and wanted to document them here for future reference, and to help others avoid the debugging grief I just went through.

Accessing Hyphenated Attributes

The documentation states that to access the attribute of an element, use the arrow operator. This works fine in the example:

// Get a attribute ( If the attribute is non-value attribute (eg. checked, selected...), it will returns true or false)
$value = $e->href;

However it doesn’t work so well when the attribute we’re trying to access is data-role or data-inset, for obvious reasons. Turns out there’s an attr[] array that stores these values as well, and is much friendlier for these types of attributes. So to access the data-role attribute we’d just use:

$value = $e->attr['data-role'];

Bug When Adding Attributes

It seems that when we add an attribute that didn’t already exist on the element using the aforementioned attr[] array, it fails to add a leading space. We end up with <uldata-role="listview"data-inset="true">, which isn’t really what we’re looking for. The fix is quite easy. Just add the leading space when you assign the attribute, though doing some sanity checks doesn’t hurt:

if ( ! $ul->attr['data-role'] )
	// Add leading space on attribute name
	$ul->attr[' data-role'] = 'listview';
else
	$ul->attr['data-role'] = 'listview';

That’s all I’ve discovered so far.  I do highly recommend this helper class as it lets you manipulate the DOM quite well. I’m normally not opposed to JavaScript, but most of the things I do these days tend to be fairly JS-heavy, so getting some of this lifting done on the server (and caching it) will definitely help with page load times.

PHP Simple HTML DOM Parser on Sourceforge

Overriding the Default Search Form

WordPress LogoI’m working on building an HTML5 WordPress theme that I’ll use as a foundation for all my projects. I’m sort of taking the shoot-first, ask-questions-later approach. That is, I’m using as much HTML5 goodness as I can, and I’ll deal with backwards compatibility later. So, I wanted to use a proper HTML5 search input.

In order to override the default search form, all you have to do is create a file called searchform.php in the root of your theme’s directory. WordPress will pick this up and use it. For reference, my form looks like this:

<form role="search" method="get" id="searchform" action="<?php bloginfo( 'wpurl' ); ?>">
<div>
<input type="search" value="" name="s" id="s" results="5" autosave="gg_search" placeholder="Search..." />
<input type="submit" id="searchsubmit" value="Search" />
</div>
</form>

Get Page ID of Posts Page

WordPress LogoYet another of those “it took me forever to find the answer so I figured I’d post it” posts.

The common answer to the question of “how do I get the ID of the current page?” is

$post->ID

But that fails when the page ID you’re looking for is actually the Posts page. In that case the above statement returns the ID of the first post, not the page itself. Really annoying.