Modifying ohShopify.php to work with private apps

I recently needed to write a quick and dirty private app for a client’s shop to automatically tag products on creation and update, but ohShopify.php didn’t seem to support private app authentication. Here’s my equally quick and dirty modification to ohShopify.php to get it working with my private app.



The modified ohShopify.php still works with your regular OAuth Shopify Apps. I just added an extra parameter to the constructor that takes the password that’s used with private apps. You can skip all the usual steps involving installation and tokens.

$ret = $sc->call('GET', '/admin/products/count.json'); Sortable.create onUpdate event isn’t firing

I recently ran into a really annoying issue where the Sortable.create onUpdate event just would not fire for me.

<ul id="list">
  <li>Something #1</li>
  <li>Something #2</li>
  <li>Something #3</li>
<script type="text/javascript"><!--
Sortable.create("list", {onUpdate: function() { alert("Something moved...") }});
// --></script>

It turns out that each draggable item needs to have a unique id value. Since this code was in a PHP page, I just added this to fix it:

<?php $i = 0; ?>
<li id="item_<?php echo $i++ ?>">

Excluding a single file from HTTP Authentication in .htaccess

We recently ran into an issue where we needed a custom HTTP 401 (Authorization Required) error page on a site that was protected by basic HTTP authentication implemented in a .htaccess file.

Since I’m not very familiar with .htaccess stuff, it took quite a bit of Googling on my part and the solution turned out to be very simple:

<files maintenance.html>
Satisfy Any

AuthUserFile /www/.htpasswd
AuthName "Restricted Area"
AuthType Basic
AuthGroupFile /dev/null
Require User admin

“maintenance.html” is a file that sits in the root of my web directory.

Replacing whole words in strings in PHP

I was recently tasked with creating a simple dirty word filter in PHP that relies on our blacklist of about 600 words. The filter should simply replace the word with its character length in asterisks.

My first attempt just used str_replace, but I quickly realized that even dirty words embedded in clean words would get filtered. For example, words like “glass” would get filtered to “gl***”.

The solution? Regular expressions:

function str_replace_word($needle,$replacement,$haystack){
    $pattern = "/\b$needle\b/i";
    $haystack = preg_replace($pattern, $replacement, $haystack);
    return $haystack;

The pattern is pretty simple, but not being very familiar with regular expressions, I had to do a couple Google searches before I found what I needed.

Here’s a simple breakdown of the pattern string:

The pattern delimiter

The pattern string begins and ends with a forward slash / character. These two characters are called the pattern delimiter and are required by the preg_match function. They can actually be any non-alphanumeric and non-backslash character, so you can pick your pattern delimiters to help with readability.

$pattern = '/\bMatch me\b/';
$pattern = '@\bMatch me\b@';
$pattern = '#\bMatch me\b#';

If you don’t supply a pattern delimiter, PHP will spit out this warning:

Warning: preg_replace() [function.preg-replace]: Delimiter must not be alphanumeric or backslash in

The word boundary \b

The \b escape character acts as a word boundary. For a nice explanation of this metacharacter, take a look at Jan Goyvaerts’ excellent regular expression tutorial page.

The ‘i’ at the end

The ‘i’ at the end of the pattern string just tells preg_match that the search should be case insensitive.

Adding leading zeros in PHP

function leadingZeros($num,$numDigits) {
   return sprintf("%0".$numDigits."d",$num);

echo leadingZeros(1,2);     // 01
echo leadingZeros(11,2);    // 11
echo leadingZeros(11,1);    // 11
echo leadingZeros(253,4);   // 0253

According to a post on the page for str_pad(), sprintf() is much faster for this purpose.

Viewing your site before domain propagation

So you just bought a new domain name and/or a new host and you want to see everything in action, but you can’t yet because your domain hasn’t propagated! Luckily, there’s an easy way to temporarily jump forward in time.

The quickest and easiest work-around is to just edit your hosts file so that your domain points to the IP address of your new hosting server. The hosts file is just a regular text file, but it doesn’t have a file extension like .txt attached to it. Here’s how:

Continue reading