i18n and l10n for Mustache (PHP) and Angular (Javascript)

English in fact uses just two forms of plural:

  • 0 children
  • 1 child
  • 2 children
  • 3 children

In Angular terminology it means ONE or OTHER1:

/**
 * Plural select rules for en locale
 *
 * @param {number} n  The count of items.
 * @return {goog.i18n.pluralRules.Keyword} Locale-specific plural value.
 * @private
 */
goog.i18n.pluralRules.enSelect_ = function(n) {
  if (n == 1) {
   return goog.i18n.pluralRules.Keyword.ONE;
  }
  return goog.i18n.pluralRules.Keyword.OTHER;
};
Continue reading “i18n and l10n for Mustache (PHP) and Angular (Javascript)”

PHP Elements in Pursue of a Dedicated API

A few months ago I was considering spicing my coding knowledge with Angular.JS. So I came upon the Dependency Injection concept and I fell in love with it.

I inspected Symfony’s creator thoughts about DI and concluded Symfony components being great but massive for my needs. I wrote my own very simple DependencyContainer class

Time for an excercise.

Continue reading “PHP Elements in Pursue of a Dedicated API”

The Content and Managing it (on the web)

My thoughts around creating websites, content, CMS-es (not particulary an article with a conclusion).

There are few areas I wish were more in-line with current development of HTML5, apps, websites, forms, APIs, content management if you put it in the context of Angular.JS, Ionic Framework, Macaw and AppPresser taking spotlight.

Continue reading “The Content and Managing it (on the web)”

Hide unfinished stories from home page

When I decided to start my personal site I promised myself to write more. It’s easier to say than done.

I’ve already tried that but most of my post were lengthy and writing them took me quite some time. Than I met my girlfriend. She already had a lovely blog she updated quite regularly. Her posts were not long, nor short, always with nice photos she took by herself.

What a moment of inspiration that was.

That was the moment I realised I could publish some posts silently as work in progress. It took me more than six months to start this site, but as you can see here I am writing this post.

I also renamed my Uncategorized category to Work In Progress with slug wip to get a nice URL address with some meaning.

Using the magic of WordPress filters it is possible to hide any posts from main loop a.k.a The Loop, the automatic query on the front page. And as all new posts are assigned uncategorized category by default, new posts are also hidden from the front page too.

Here’s the code 1:

/** Exclude Specific Categories From The WordPress Loop */
add_action( 'pre_get_posts', 'exclude_specific_cats' );

function exclude_specific_cats( $wp_query ) {
    if( !is_admin() && is_main_query() && is_home() ) {
        $wp_query->set( 'cat', '-1' );
    }
}

This is the way I can silence my mind and write myself out. I can link this articles, send links to my friend to proofread even though the stories are not finished yet.

I hope some people still can find useful information in the WIP posts and I can write my posts without polluting my default feed with unfinished stories.

I hacked myself to writing more (hopefully).

Filtering results from WordPress (JSON REST) API

I have just discovered a ‘wanna-be-core’ plugin designed for programmatic access to your WordPress powered sites. I looked around a little bit more, yet this ‘core’ promise was interesting.

Actually, plugin documentation are pretty decent, but soon you’ll come up with a question: “How do I query this beast?”

Digging through code revealed some answers. It appears that using an URI argument filter with any query variable from set of WordPress Query Vars filters the results of API.

GET all posts from category Uncategorized: http://api.drivenwpsiteexample.com/wp-json.php/posts?filter[cat]=1

Go and APIfy your sites.

Other Resources:

WordPress Uploads on Cookie-less subdomain

Edit wp-config.php:

/* Uploads directory PATH and URL */
define ('UPLOADS',     realpath(dirname(dirname(__FILE__))).'/static/images');
define ('UPLOADS_URL', $_SERVER['SCHEME'].'://static.'.$_SERVER['HTTP_DOMAIN'].'/images');

Edit functions.php or create a plugin:

if (defined('UPLOADS') && defined('UPLOADS_URL')) {
    // Fix upload paths/URLs
    add_filter('upload_dir', function($uploads) {
        $uploads['path'] = $uploads['basedir'] = UPLOADS;
        $uploads['url']  = $uploads['baseurl'] = UPLOADS_URL;

        return $uploads;
    }, 0);
}

Add .htaccess in the web hosting root:

Options +FollowSymlinks

# Enable engine
RewriteEngine on

# Multisite setup #

RewriteCond %{HTTP_HOST} ^(.+?).([a-z-]+.[a-z-]+)(?:.dev)?$ [NC]
RewriteCond %{REQUEST_URI} !^/%1/
RewriteRule ^(.*)$ /%1/$1 [QSA,L]

RewriteCond %{HTTP_HOST} ^([a-z-]+.[a-z-]+)(?:.dev)?$ [NC]
RewriteCond %{REQUEST_URI} !^/www/
RewriteRule ^(.*)$ /www/$1 [QSA,L]

# Make sure that once file is found, the rewriting stops
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [QSA,L]

To make sure PHP can access all directories, move website into subdirectory www (serving domain.com and www.domain.com), plus create static subdirectory for serving all static content from cookie-less domain static.domain.com.

PS: You might edit Google Analytics tracking code and add this line:

define(‘COOKIE_DOMAIN’, ‘www.yourdomain.com’);

Forget Child Themes, Use Plugins

Here’s a thing: extending a parent theme using child theme is pretty cool concept. Until you come across the caveats of maintaining multiple websites based on your “next big parent theme framework”.

When you develop a child theme for client you naturally enhance the parent. But how to decide which enhancements should make it to the ‘core’ a.k.a. parent theme? And when you decide, you need to check other themes.

Guess what. By using plugin-per-feature you can enhance the modularity of your parent theme just the same without touching your parent theme until it’s really necessary.

Codex: Writing a Plugin

Move Plugins, Content and WordPress to subdirectories

Motives

When setting a new blog or a site you have this amazing opportunity to do it a little bit different this time. From my experience, these changes can be helpful in more ways.

  1. Tracking code changes using Git is easier using submodules.
  2. Plugins and engine are separate, you can upgrade each vendor as needed (your website is ready for new features, more control over upgrading dependencies and testing).
  3. Additional security for WP Admin (e.g. using Apache Auth) but still keep plugins publicly accessible.
  4. Looks more tidy.
  5. Hide the fact that the site is running WordPress due to security reasons.

Final structure of your website:

  1. /wp-content would become /assets
  2. /wp-content/plugins would become /assets/vendor
  3. /wp-admin would become /web/wp-admin

Lets start.

1. Put WordPress in its own subdirectory

I prefer to put WP in subdirectory /web, but you might pick any name you prefer. Following the codex the process is fairly doable.

2. Change WP-Content directory path and WP-Content URL

Edit to wp-config.php:

/* Content direcotory PATH and URL */
define('WP_CONTENT_DIR'   , realpath(dirname(__FILE__)).'/assets');
define('WP_CONTENT_URL'   , "{$host}/assets");

3. Change Plugins path and Plugins URL

Simply changing wp-content directory is not enough to change path to plugins. Edit to wp-config.php:

/* Plugin PATH and URL */
define( 'WP_PLUGIN_DIR', WP_CONTENT_DIR.'/vendor');
define( 'WP_PLUGIN_URL', WP_CONTENT_URL.'/vendor');

Extra modifications

I like to disable autosave for posts:

/* Modify AutoSave Interval */
define('AUTOSAVE_INTERVAL', 60*60*24);  // a day in seconds

Sometimes even revisions to keep database row count to the minimum:

/* Specify the Number of Post Revisions */
define('WP_POST_REVISIONS', false);

To remember run database queries when debugging site load speed:

/* Store queries in $wpdb->queries */
if(WP_DEBUG && !defined('SAVEQUERIES')) {
    define( 'SAVEQUERIES', false );
}