Steve Grunwell

Open-source contributor, speaker, and electronics tinkerer

Category: Code

Quick Tip: Prevent OneAll from Creating New Users

I’m working on a site right now that uses the Social Login plugin by OneAll, which is the first time I’ve dealt with users logging into a WordPress account via social networks. The plugin works really well, but I ran into one major issue: new user accounts were being created when matches weren’t found.

This particular project is a BuddyPress site for a fraternity; brothers can log in to see private events, content, and the full roster but the general public can only see the public pages. There’s also a public roster page with basic information about each member (name, graduation year, major, etc.), but those are dynamically generated by listing all BuddyPress members (excluding my team’s accounts, of course). Out of the box, OneAll would create a new user account for anyone who tried to log in, resulting in my mug appearing in the roster right along all the registered fraternity members…yikes!

Continue reading→

Automatically Restart PHP-FPM During Deployments

At work we’ve been rolling out PHP-FPM to more and more of our clients’ servers, which has been great for performance and keeping server loads down. The only issue we’ve had (beyond some initial configuration) was related to our deployments: many of our projects, especially our web applications, are deployed through Capistrano, a Ruby-based task runner that’s extremely popular among the Ruby on Rails crowd. While Capistrano let’s us reduce deployments to running cap production deploy, PHP-FPM typically needs to be restarted before it will start using the new code.

Having to manually SSH into a server to restart PHP-FPM after a deployment takes some of the sexy out of one-line deployments, so we sought to find a better way. The goal: be able to restart PHP-FPM (sudo service php5-fpm restart) from within a Capistrano task without being prompted for passwords (we use Public-Key authentication) or having to manually SSH in to restart the server.

Continue reading→

Laravel Application Environments without Hostnames

Forgive me if this is old news, but hostname-based environment switching isn’t as nice as it could be; I like using *.dev for my development environments, but other people I know like  *.development, *.local, etc. The truth of it is the more developers you have working on a project the higher chance you run of the application being run at a variety of hostnames, mucking up your bootstrap/start.php file.

Fortunately, Laravel doesn’t force us into using hostname-based environment switching. At work we’ve developed this little workflow, and it seems to be working really well.

Continue reading→

Automatically Set a Poster Image for WordPress Video Embeds

I’m working on a client project right now that makes heavy use of embedded videos. It’s exciting because as someone who primarily develops themes and plugins for marketing sites (and whose personal blog is almost exclusively text and code) it’s rare that I really spend much time with the rich-media embeds that have been getting so much attention from the core team over the last few releases.

As I started playing with the video embeds I immediately noticed an issue – there was nothing in the media widget that allowed authors to set a poster frame (the static image that appears in the video player before the user clicks the play button). I checked the documentation for the

[video]

shortcode and found that the shortcode does accept an optional poster attribute, which allows authors to specify a poster image.

Continue reading→

Quick Tip: Restrict a WooCommerce Shipping Method to the Contiguous United States

Right now at work I’m working on moving a site from WP eCommerce to WooCommerce and encountered an interesting request: the site offers free shipping but only to the lower 48 United States. That means no free shipping for Alaska, Hawaii, Puerto Rico, etc.

Out of the box WooCommerce supports country-based filtering (e.g. allow free shipping to the United States but not Canada) but to get into more specific restrictions you’d have to start messing with shipping tables or buying the Advanced Shipping Rates plugin which, although I’ve heard good things, will set you back $200.

Fortunately I was able to put together a code snippet that will remove a shipping method (in this case, free shipping) for restricted states. It consists of two parts: a class that extends the WooCommerce core shipping class (WC_Shipping_Free_Shipping for this example) and a filter that tells WooCommerce to use our class rather than the core shipping class it extend Post has been updated to work with newer versions of WooCommerce.

Continue reading→

WordPress Security Basics

Each year thousands of websites are hacked. Sometimes it’s by way of a crafty social engineer (someone who tricks someone into giving up information without realizing it) conning some unsuspecting user out of his/her login. Other times it’s a backdoor in some code that grants a malcontent access to a site.

If your site is running on WordPress you can rest easy – WordPress core is considered to be a very secure application. The downside to WordPress core being secure is that if someone breaks into your site you likely have nobody but yourself to blame. Never fear: these tips will help keep your site safe and sound.

Continue reading→

Quick Tip: is_login_page() Function for WordPress

When building WordPress themes, I typically try to keep as much code as possible out of the head section of my template and instead rely on the wp_head() action hook. By putting all of my wp_register_script() and wp_register_style() calls in one function I have a single place to manage (theme) assets and their dependencies. At the bottom of the function I usually like to enqueue my global scripts and styles (ones that will always be present) but am careful to keep them from appearing in the WordPress administration area.

Continue reading→

Quick Tip: Troubleshooting /etc/hosts issues

I ran into an interesting error this evening: I was working on a new WordPress site for a freelance client when my local server (I use MAMP) would start hanging. Now my MacBook Pro is certainly past its prime (spring 2008, to be exact – I can still remove the battery) but there’s no reason a bare-bones WordPress theme running on my local machine should be slower than most of the external sites I’m visiting so I decided to do some digging.

Continue reading→

Using WordPress Advanced Custom Fields Exports

I wrote about Elliot Condon’s Advanced Custom Fields plugin a while ago (see “Using Advanced Custom Fields for WordPress“), but I thought it might be helpful if I spent some time discussing how to overcome one of the most frustrating tasks related to the plugin: preserving custom field structures across WordPress environments.

The average WordPress developer may edit the custom fields in his or her development environment, write the code to display those fields, then manually re-edit the custom fields on production. Misspell a field name? Congratulations, you’re going to spend some time trying to figure out where you went wrong. The smart developer takes advantage of the Advanced Custom Fields export methods and doesn’t waste time doing the same thing over and over again.

Continue reading→

Automatically Recompile Sass upon Deployment Using Git Hooks

When I’m the only one working on a site, compiling my Sass files locally and committing the generated CSS isn’t the worst thing in the world. When you’re working in a team environment however, it’s necessary to consider that several developers of different experience levels may touch a site throughout its lifecycle. What would happen if a developer who had never used Sass needed to make an update to a site? How long would it be before someone starts complaining that the change they made to the CSS file was overwritten after the next compilation of the corresponding .scss file?

This technique has one goal: keep generated CSS files out of the git repository. In order to do this, we’ll need to make sure of a few things:

  1. Generated files are explicitly blocked from the repo (to prevent someone who doesn’t know any better from committing them)
  2. Automatically recompile our SASS files into CSS upon deployment.

Continue reading→

Page 6 of 7

Be excellent to each other.