Development

Clean wp_head() output

I’ve never liked the wp_head() output – WordPress’s default output is great for blogs and accessibility but if you’re a developer whose source-code is basically your resume, you don’t want irrelevant and un-indented code leaving skid marks behind the scenes.

Throwing this in functions.php will remove most of it.

https://gist.github.com/davidsword/82f1a1248904d4fd8df465dd5b3a453b

Increase WordPress Memory Limit

Nothings worse then getting an email about someone being unable to upload a 6mb .PDF because it “exceeds the upload file size limit

Here’s the fix – but important note: raising post_max_size increases the possibility of DoS attacks on your server:

In functions.php add:

https://gist.github.com/davidsword/e5fc6499ca234221cef3d2ee7bd04ebf

If that doesn’t work, it could be a apache restriction, in .htaccess add:

https://gist.github.com/davidsword/18c5b0f064ba7b5aa65c6b084f799bdd

If you’re still unable to it could be a php restriction, in php.ini add:

https://gist.github.com/davidsword/4f3fe6d6ba9f64538f07fcff9cc90c51
Obviously change the 32mb/32000kb as needed.

Update:

The above worked for me 99% of the time, but I had once instance where I need to add the following into functions.php as well:

https://gist.github.com/davidsword/87e0c067528becc7ae3d0919e1a0432a

Responsive Height, Maintain Ratio

Say you have an element #banner with a background-image. On a responsive design that’s width:100%; you’ll notice your image will be auto croped by the viewport change. The solution is simple: background-size: cover; but a pain that follows is the height of the element will not scale down or up with the width change, leaving this weird empty vertical gap or a very cropped image.

Here’s a fix to figure out and change the height end_h based on the current window width, keeping background-image ratio making it responsive/scalable:

https://gist.github.com/davidsword/76fb01f32d2dae5f002788987b542f80

This resize happens when your script is loaded, as well as on any browser resize event.

wp_get_attachment_thumb_url() Alternative

If you have any experience with wpgetattachmentthumburl() you’ll know it’s really slow.. It’s a maze trying to figure out why, it just seems to require querying a lot of meta data.

So, to simplify WP’s queries I wrote this function to grab a thumbnail from the image’s full URL:

https://gist.github.com/davidsword/1f3bcf36b4d161ecd0f4cde483c2e4d4

Then just get my thumbnail with the url

https://gist.github.com/davidsword/6fc4fa4e3d79a06d0a6a51c8264f4752

It’s not fool proof – it works for me because I know all my images are jpeg, and I know the URL of the full image, so this takes away all those extra queries (useful when my media plugin is pre-finding thumbnail URL’s of hundreds of images).

Loading GravityForms Outside of WordPress

I had a WordPress installation in /blog/, but I needed a WordPress’s GravityForm to render /contact.php an external non-Wordpress php page. I could of coded a form myself, but there are so many advantages to using GravityForms.

in contact.php in a perfect world, the following would work:

https://gist.github.com/davidsword/1f5e376793ef2de086e6bd532048e607

but it doesn’t — upon submission the page reloads with entered values in-tacked but no sucessful submission message or errors.

Looking in the first file in GravityForm plugin gravityforms.php, there’s a ton of actions hooked into the wp action. So by simply firing wp, the GravityForm now works, outside of a WordPress installation.

I’m neglecting wp_head() and wp_footer() which means style sheets, ajax submission and conditional logic won’t work. I also think loading wordpress this way neglects to run init, which several functions are hooks into, but I got what I needed out of simply:

https://gist.github.com/davidsword/a6dfcb66f5d9832c5e6b33e1ecde1e29

Show WordPress Query Load Times

Here’s a good way to find out how many and what queries are being executed and how long it takes. Your code may be inefficient and here’s a good way to tell.

When troubleshooting

https://gist.github.com/davidsword/24fa2e27b4c62900bc8c66e3c3cec490

I’ve used admin_footer hook so this will show in the admin, you can use wp_footer or other late hooks to show on main site. Note $wpdb->queries will reveal table names to the source code so this shouldn’t be on public sites or proper user conditional tags should be added.

Change the Color of First Few Words PHP

If you have a header or similar and would like the first few words highlighted, here’s a nice little script. In below the class red is defined in my css, and $color would be the number of words I want coloured.

https://gist.github.com/davidsword/9ed30f0cc4daddf9dd99fe885971c094

Usage would be similar to:

https://gist.github.com/davidsword/aa655e47c48c4be14c1432566eeb4842

Domains Added on Plesk Not Receiving Emails

Servers running plesk with domains using an external DNS Zone file and external MX record may experience an email issue as qmail thinks it is sending emails to itself because of the plesk records and will try to bypasses the external DNS Zone.

To fix this, we have to tell qmail that these domains email solutions are not handled on the server.

To do this, remove the line with the problematic domain(s) from both files below:

https://gist.github.com/davidsword/23ab8449299404673321dfd7fe463e1e