Categories
Web Development

Releasing Content over Time / Drip Campaign with WordPress

Using the Date Registered field in WordPress you can create a page that releases content over time. In this example we are creating a Drip Campaign for content released in a specific category.

In this example I have set up a category called “Campaigns” and within it I have added subcategories called Week 1, Week 2, Week 3 etc. The goal is that every week a new section will be released, so if the user registered, week 1 is available instantly, then 1 week later week 2 becomes available and so on.

$dateRegistered = strtotime($current_user->user_registered);
$now = date('Y-m-d H:i:s', strtotime('now'));
$categoryId = 99;

$catArgs = array('taxonomy' => 'category', 'parent' => $categoryId, 'hide_empty' => false);
$categories = get_terms($catArgs);
$dripIncrement = 7;
$startNumber = 0;
$counter = 0;
$number = 0;
$weeks = array();

foreach ($categories as $category) {

    if ($counter != 0) {
        $number = $number + $dripIncrement;
    }
    $weeks[$category->term_id] = date('Y-m-d H:i:s', strtotime('+' . $number . ' day', $dateRegistered));
    $counter++;
}

$listStyle = '';
foreach ($weeks as $weekId => $releaseDate) {

    // If is enabled
    if ($now >= $releaseDate) {
        $listStyle .= '
            li.cat-item.cat-item-'. $weekId .',
            li.cat-item.cat-item-'. $weekId .' a
             {
                color: green;
            }
        ';
    } else {
        $listStyle .= '
            li.cat-item.cat-item-'. $weekId .',
            li.cat-item.cat-item-'. $weekId .' a {
                color: red;
                pointer-events: none;
                cursor: default;
            }
        ';
    }
}

The above create a loop for for the subcategories in my Category Id, and using an increment of 7 days it will add one type of css if the registered date is within range and another if it is not. We then take the following and add it before the tag.

<style type="text/css">
    <?= $listStyle ?>
</style>

Finally, we use the WordPress list categories function to print a list of all the subcategories, the $args variable allows to tweak things like printing a title, printing if the subcategory has no content, etc.

<?php
    // List subcategories of Campaign
    $args = array(
        'child_of'            => $categoryId, // 10 Week Plan ID
        'title_li'            => false, // Hide Title
        'hide_empty'          => false
    );
?>

<?php wp_list_categories($args) ?>
Categories
OSX Web Development

Install ext-intl on Mac OSX using homebrew and XAMPP

This was a painful process for me, it took quite a while to figure out why such a simple implementation when using ubuntu, can be so much work in a Mac.

We can’t just install the extension with apt-get like ubuntu, and its not already waiting to be uncommented like in windows, we have to install the the shared object intl.so our selves.  and the easiest way of doing this is by using homebrew and pecl.

Step 1 – Install Homebrew

Homebrew Website: http://brew.sh

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Step 2 – Install Install icu4c

ICU4C Website: http://site.icu-project.org

brew install icu4c

Step 3 – Install Autoconf

More about Autoconf: http://brewformulas.org/Autoconf

brew install autoconf

Step 4 – Install Intl via PECL

PECL Website: https://pecl.php.net

sudo pecl update-channels
sudo pecl install intl

Step 5 – Enable intl.so in php.ini

Go to your php.ini file and at the end, add the following line:

extension=intl.so

This will enable the extension that we have just installed.

Step 6 – Restart your server

Use XAMPP to restart your server, this will reload the php.ini file with the new configuration.

Step 7 – Check it worked!

You can check if Intl was installed successfully by using the following command to list the loaded php modules.  If ‘intl‘ it is listed then it has been successfully loaded.

php -m

I am running php via Terminal and it didn’t work?

One common problem is that terminal is using the the wrong version of php, sometimes a system default is loaded rather than the php version you are using in XAMPP.  First lets check what version we are using:

which php

If you are using XAMPP correctly it should return:

  • /Applications/XAMPP/xamppfiles/bin/php 

If not and it loads from:

  • /usr/bin/php

Then we can temporarily change the path of the default php version to the XAMPP version by running the following in terminal:

PATH="/Applications/XAMPP/xamppfiles/bin:${PATH}"

Now if you run “which php” in terminal again you should get the correct path.

 

 

Categories
Web Development

Create SEO Friendly urls with this Slugify PHP Method

	/**
	 * slugify
	 * @param string $str
	 * @return mixed
	 *
	 * Converts nasty URLs to clean, SEO friendly ones
	 */
	public function slugify($str) {
	    $clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $str);
		$clean = strtolower(trim($clean, '-'));
		$clean = preg_replace("/[\/_|+ -]+/", '-', $clean);
		return $clean;
	}