Blog

  • How to load theme and plugin translations

    You may ask yourself what is so special about this post. Does not the codex cover load_plugin_textdomain() and load_theme_textdomain() but this posts will show what else you can do with it.

    With this code for the theme and plugin you can add the translations in different places. The two reasons for this are:

    1. This allows for the translation to be placed in multiple places when the theme/plugin author does not add the translations to the theme/plugin and not have them deleted in every update.
    2. This also allows for the translations to be edited without needing to maintain the whole translation. For example there might be only one string that needs to be different for a site and so can be added to language directory before the theme/plugin language directory.

    How do .mo files work?

    So with the translation in the mo files if two of the same translations are loaded only the first translation will be displayed. So that is why in these code examples the plugin/theme language folders are loaded last. With if you only need to change a single string you would only need to add a single translation string in the directory that is loaded before.

    For the theme there are three different places; the WordPress language directory, the child theme language directory and the parent theme language directory.

    <?php
    function theme_name_setup(){
    $domain = ‘theme-name’;
    // wp-content/languages/theme-name/de_DE.mo
    load_theme_textdomain( $domain, trailingslashit( WP_LANG_DIR ) . $domain );
    // wp-content/themes/child-theme-name/languages/de_DE.mo
    load_theme_textdomain( $domain, get_stylesheet_directory() . ‘/languages’ );
    // wp-content/themes/theme-name/languages/de_DE.mo
    load_theme_textdomain( $domain, get_template_directory() . ‘/languages’ );
    }
    add_action( ‘after_setup_theme’, ‘theme_name_setup’ );
    view raw functions.php hosted with ❤ by GitHub

    For the plugin there are two different places; the WordPress language directory and the plugin language directory.

    <?php
    function plugin_name_load_plugin_textdomain() {
    $domain = ‘plugin-name’;
    $locale = apply_filters( ‘plugin_locale’, get_locale(), $domain );
    // wp-content/languages/plugin-name/plugin-name-de_DE.mo
    load_textdomain( $domain, trailingslashit( WP_LANG_DIR ) . $domain . ‘/’ . $domain . ‘-‘ . $locale . ‘.mo’ );
    // wp-content/plugins/plugin-name/languages/plugin-name-de_DE.mo
    load_plugin_textdomain( $domain, FALSE, basename( dirname( __FILE__ ) ) . ‘/languages/’ );
    }
    add_action( ‘init’, ‘plugin_name_load_plugin_textdomain’ );
    view raw plugin-name.php hosted with ❤ by GitHub

    WordPress language directory

    The WordPress language directory can be found in wp-content. The code that we used would mean we would need a plugin or theme directory in the language directory for the translations.
    wp-content
    – languages
    – – plugin-name
    – – – plugin-name-de_DE.po
    – – – plugin-name-de_DE.mo
    – – theme-name
    – – – theme-name-de_DE.po
    – – – theme-name-de_DE.mo

    WordPress 3.7

    With the changes in WordPress 3.7 it will add support for automatically installing the right language files and keeping them up to date. These translations will be stored in the WordPress language directory under one directory for themes and another for plugins. The standard directory structure will look like this.
    wp-content
    – languages
    – – plugins
    – – – plugin-name-de_DE.po
    – – – plugin-name-de_DE.mo
    – – themes
    – – – theme-name-de_DE.po
    – – – theme-name-de_DE.mo

    You might ask yourself how this code will work with WordPress 3.7. What ever you are doing in now to load the text domain will work fine in WordPress 3.7. As the code uses the WordPress language directory but not the themes or plugins directory in the WordPress language directory all will be fine.

    WordPress checks if there are translation is in plugin/theme and if not then it will load the translation from WordPress language directory. If the translation are in the custom location in the WordPress language directory then the translations will be merged but the translations from the custom location will used unless not defined.

    In the future plugin and themes on WordPress.org repository will be able to update translations separately to the plugin or theme updates(This will launched separately to WordPress 3.7).

    Otto has a great post on this that must be checked out.

    Here is how you could conditionally load only one translation file and all of them one after each other.

    <?php
    function theme_name_setup(){
    $domain = ‘theme-name’;
    if ( $loaded = load_theme_textdomain( $domain, trailingslashit( WP_LANG_DIR ) . $domain ) ) {
    return $loaded;
    } elseif ( $loaded = load_theme_textdomain( $domain, get_stylesheet_directory() . ‘/languages’ ) {
    return $loaded;
    } else {
    load_theme_textdomain( $domain, get_template_directory() . ‘/languages’ );
    }
    }
    add_action( ‘after_setup_theme’, ‘theme_name_setup’ );
    view raw functions.php hosted with ❤ by GitHub
    <?php
    function plugin_name_load_plugin_textdomain() {
    $domain = ‘plugin-name’;
    $locale = apply_filters( ‘plugin_locale’, get_locale(), $domain );
    if ( $loaded = load_textdomain( $domain, trailingslashit( WP_LANG_DIR ) . $domain . ‘/’ . $domain . ‘-‘ . $locale . ‘.mo’ ) ) {
    return $loaded;
    } else {
    load_plugin_textdomain( $domain, FALSE, basename( dirname( __FILE__ ) ) . ‘/languages/’ );
    }
    }
    add_action( ‘init’, ‘plugin_name_load_plugin_textdomain’ );
    view raw plugin-name.php hosted with ❤ by GitHub
  • Help: WordPress i18n Tools

    When I was at WordCamp Europe 2013 I listened to Otto’s talk on internationalisation. I have had time to play around with the WordPress i18n tools. I would like to add a few more features and see how the code can be improved.

    I have set the tools on GitHub and I am looking for people to help me. I have started on cleaning up the code to the WordPress coding standards and found and implemented a fix for the strict standards error. A few issues that I see can be found on the repo issues. I think the ultimate goal would be to make a plugin out of it. This way everyone can use it easily.

  • WordCamp Europe 2013

    WordCamp Europe was the first WordCamp that I have been to and it was great. After reading the great post “WordCamp Europe 2013 – A Welshman’s Thoughts” I thought I would write my own as I visited different sessions to Rhys.

    First Day

    Unit Testing like a Pirate – Ptah Dunbar

    Ptah had his eye patch on and had red and white striped t-shirt on. Here are his slides.

    Better Site Stacks with Composer by Andrey “Rarst” Savchenko

    It was great to have Rarst explained how to use composer for deployment and user testing. For me it was really nice to see a developer use Windows. His slides can be seen on his site.

    The Life of a Theme by Tammie Lister

    An interesting take on building themes from a designers perspective.

       

    Developing WordPress Themes with Git by Kirsten Schelper & Elisabeth Hölzl

    The German developer and designer duo explained the git process using diagrams for beginners.

    Perfect Your Images Using WordPress by Marko Heijnen & Mike Schroder

    This was nice see what type of tools are coming into WordPress and how it getting easier for developers to manipulate images. I only found out after the talk that GD and IMAGICK are php image manipulation tools.
     

    Writing Secure WordPress Code by Brad Williams

    This was one of my favourite talks as I was able to understand a few things I  was a bit unsure of before.

    Second Day

    Practical WordPress Accessibility by Bram Duvigneau

    This session did not have any slides. Bram is blind and he showed us how it is like to use WordPress for a blind person with a live demo. He showed the issues with page structure and took the event site as an example. Bram also showed some issues in WordPress how functionality should work with just keyboard.

    To OOP or not to OOP by Nikolay Bachiyski

    The main principle here is that OOP is perceived difficult and we just need to work at it.

     

    Learnings from Growing Local WordPress Communities in Japan by Naoko Takano

    This was a really interesting talk as what can be achieved by a community and how important translations are. Naoko wrote a post on it.

    On Internationalization: Plugins and themes for the whole world by Samuel “Otto” Wood

    This presentation is a must see. Otto covers on common mistakes and explains why translations are so important. English WordPress 3.5.2 downloads contributed only 60% of the total downloads. 40% of WordPress 3.5.2 were downloaded in other languages.

    The State of Multilingual WordPress by Simon Wheatley, Frank Bültge & Amir Helzer

    We have a new plugin in town. Code For The People released their new multilingual plugin Babble.

    The presentation for Multilingual Press. MarketPress also wrote a roundup on the different solutions.

    This is what I understood from the presentations and my experience with the plugins.

    • WPML, a very popular WordPress plugin that has a lot of functionality and they provide support in multiple languages
    • Babble, a rather new plugin focussed on meeting WordPress.com VIP hosting standards but with reduced functionality
    • Multilingual Press, based upon the default WordPress Multisite feature, great for having different language sites
  • How to update translations quickly

    This is for Windows users.

    Set Up WebTranslateIt Synchronization Tool

    I use webtranslateit for managing all of the theme and plugin translation. Up till recently I have always download the files in a zip and then extracted them to the language folder.

    Recently I looked for a way to improve automation and found the WebTranslateIt Synchronization Tool. It is very easy to set up.

    The installation process is explained on GitHub.

    Setup .mo file generation

    To automate the conversion of the mo files you will need to install Cygwin. It is a simple installation process but takes some time to install.

    Then create a file called potomo.sh. Then add this code:

    Then go to the language folder and open the file .wti. Replace # after_pull: "touch tmp/restart.txt" with after_pull: "I: &amp; cd I:/path/to/folder/<wbr />languages &amp; c:/cygwin/bin/bash -c /cygdrive/i/path/to/<wbr />script/directory/potomo.<wbr />sh".

    Run Script

    To be able to run the whole thing Start Command Prompt with Ruby. Then move to the location of the language folder and type wti pull

  • Child themes, IE8 and Media Queries

    In my previous post I mentioned that one of the issues of using @import in the child theme is that the JavaScripts that help media queries in IE8 and lower so not work.

    So there  are three solutions.

    When using @import copy the media queries from the parent theme and paste in the child theme stylesheet.

    Pro

    • Simple and easy to do

    Con

    • For the rest of the browsers you have the media queries twice which makes debugging complicated
    • The media queries needs to be updated if there are any changes in the parent theme

    Use the solution of using functions.php that I presented in my previous post.

    Pro

    • Improves the loading speed of the stylesheets
    • The media queries are only in one place and thus do not need to be updated

    Con

    • This is a bit more complicated and perhaps not so easy to understand for beginners

    Theme authors add the media queries in a separate file and have the file always loaded from the parent theme

    Pro

    • The user is not confronted by this issue

    Con

    • An extra file is load just for the media queries
  • Sync GitHub repository to live server via SFTP

    Sync GitHub repository to live server via SFTP

    I came across this great service today DepolyButton. Easily add version control to your WordPress site, plugin or theme very easily.

    It is as simple as

    1. click on “Set up a Project”,
    2. enter your email address,
    3. add your SFTP login info,
    4. connect with GitHub and
    5. deploy.
  • Improved way of loading parent styles in child themes

    The prefered method in the codex and with plugins that generate child theme is to load the parent stylesheet by using @import in the child themes stylesheet.

    @import url("../twentytwelve/style.css");

    There are a few problems with this.

    Speed

    Using CSS @import in an external stylesheet can add additional delays during the loading of a web page.

    Media Query support in IE

    Media Queries are not supported in IE8 or lower. So to support media queries on IE 8 and lower JavaScript is using like css3-mediaqueries-js and respond.js.

    Note: Doesn’t work on @import’ed stylesheets (which you shouldn’t use anyway for performance reasons). Also won’t listen to the media attribute of the <link> and <style> elements.

     

    @import rules are not supported by respond.js

    A solution to this could be to copy the media queries from the parent theme and include them in the child theme but this does not make it easy when there are changes in the media queries.

    More Flexibility

    This is not really a huge thing but it does make it easier to disable loading the parent stylesheet.

    Solution

    Now for the solution. Instead of using @import just add this PHP code to the functions.php of your Child Theme. If you don’t have one, simply create new file and name it functions.php

    Here is a variation that add the current parent theme version to the stylesheet url.

    There might be in some themes where the child theme stylesheet will load before parent theme style sheet. Those themes will have the code like this in the header

    For those cases add this PHP code to the functions.php in the child theme.

    In the perfect world the parent themes would load the stylesheet like _s does.

  • Responsive Opt-Out plugin

    Responsive Opt-Out allows visitors of your site to switch between the flexible and fixed width layout.

    This simple setup creates a body class called “responsive” and “fixed-width” both are added to the body class of your Theme. These two classes are used to define whether or not visitor wants a “responsive” environment.

    Here is a demo site that I have created using Responsive:

    Demo

    This the code you use for the switch buttons.

    <a href="#" onclick="return FixedWidth();" class="fixed-width-link">Fixed Width</a>
    <a href="#" onclick="return Responsive()" class="responsive-link">Reponsive</a>

    This CSS would display the right button in the right view.

    .responsive .fixed-width-link {
    	display: block!important;
    }
    .responsive .responsive-link {
    	display: none!important;
    }
    .fixed-width .fixed-width-link {
    	display: none!important;
    }
    .fixed-width .responsive-link {
    	display: block!important;
    }

    Here I was working in a child theme so was unable to edit the parent media queries but was still able to get a good result resting the css in the media queries.

    The basic fixed width CSS needed for Responsive would be:

    /* =Fixed Width
    -------------------------------------------------------- */
    .fixed-width #container,
    .fixed-width #footer {
    	width: 960px!important;
    }
    
    .fixed-width .grid, 
    .fixed-width .grid-right,
    .fixed-width .menu ul, 
    .fixed-width .menu li, 
    .fixed-width .footer-menu li, 
    .fixed-width .sub-header-menu li {
    	float: left!important;
    }
    
    .fixed-width .top-menu {
    	float: right!important;
    }
    
    .fixed-width #logo {
    	float: left!important;
    }
    
    .fixed-width .tinynav {
    	display: none!important;
    }
    
    .fixed-width .sb-holder {
    	display: none!important;
    }
    
    .fixed-width .menu {
    	display: block!important;
    }
    
    .fixed-width #featured p {
    	font-size: 18px!important;
    	line-height: 27px!important;
    }
    
    .fixed-width .featured-title {
    	font-size: 60px!important;
    }
    
    .fixed-width .featured-subtitle {
    	font-size: 2.25em!important;
    }
    
    .fixed-width .call-to-action a.button {
    	font-size: 24px!important;
    	padding: 15px 35px!important;
    }
    
    .top-widget {
    	float: right!important;
    }
    
    .fixed-width .top-widget area,
    .fixed-width .top-widget select,
    .fixed-width .top-widget textarea,
    .fixed-width .top-widget input[type="text"], 
    .fixed-width .top-widget input[type="password"] {
    	width: auto!important;
    }
    
    .fixed-width .widget-title,
    .fixed-width .widget-title-home h3 {
    	font-size: 24px!important;
    	height: 23px!important;
    	line-height: 23px!important;
    	text-align: left!important;
    }
    
    .fixed-width .main-nav #responsive_current_menu_item {
    	display: none!important;
    }
    
    .fixed-width .main-nav .menu {
    	top: 0;
    }
    
    .fixed-width .main-nav .menu a {
    	font-weight: 700!important;
    }
    
    .fixed-width .main-nav .menu li a {
    	color: #fff;
    	border: none;
    }
    
    .fixed-width .main-nav .menu li.current_page_item,
    .fixed-width .menu .current_page_item a,
    .fixed-width .menu .current-menu-item a,
    .fixed-width .main-nav .menu li {
    	background-color: transparent;
    }
    
    .js .fixed-width .main-nav .menu li a:hover,
    .js .fixed-width .main-nav .menu li li a:hover {
    	background-color: #808080;
    	background-image: -webkit-gradient(linear,left top,left bottom,from(#808080),to(#363636));
    	background-image: -webkit-linear-gradient(top,#808080,#363636);
    	background-image: -moz-linear-gradient(top,#808080,#363636);
    	background-image: -ms-linear-gradient(top,#808080,#363636);
    	background-image: -o-linear-gradient(top,#808080,#363636);
    	background-image: linear-gradient(top,#808080,#363636);
    	color: #fff;
    	filter: progid:DXImageTransform.Microsoft.gradient(startColorstr=#808080,endColorstr=#363636);
    }

    Download

    You can also find the plugin on github.

    Extra

    I would be interested to see how many visitor use the button. You can track the number of click of the buttons with the use of Google analytic events. The results can be viewed in the Google Analytic dashboard. The html for these buttons will look like this.

    <a href="#" onclick="_gaq.push(['_trackEvent', 'Click', 'Responsive Opt-Out', 'Fixed Width']); return FixedWidth();" class="fixed-width-link">Fixed Width</a>
    
    <a href="#" onclick="_gaq.push(['_trackEvent', 'Click', 'Responsive Opt-Out', 'Responsive']); return Responsive()" class="responsive-link">Reponsive</a>

    Thank you Andrew for supporting this plugin.

  • Unable to boot or enter CMW mode

    Ok, I just update to MIUI 2.6.22 and my SGS2 wouldnt start up and I was unable to enter the CWM mode. So I followed this guide How to Root Galaxy S2 i9100! As I wanted to use the Siyah Kernel so instead of downloading the kernel they had suggested I download the Siyah Kernel for Odin and used that instead. It worked! I was able to get my self out of a sticky situation.

     

    I hope this helps anyone else with this problem.

  • How to install WordPress on a sub-domain

    I wanted to install WordPress on ulrich.pogson.ch. I started by installing it first and then trying to make the sub domain but that didn’t work. So this is how you do it.

    First you create a sub domain. I think every hosting company does it slightly differntly so if you don’t know then ask them. I had to connect the sub domain with a folder on a server.

    Secondly you can install it using your installing WordPress in this folder. If your hosting server let you do this auomaticly then well and good but other wise there are lots of create tutorials on the web how to do this.

    Once your finished just go to sub.domain.com/wp-admin/install.php tp create your admin account and thats how you install WordPress on a sub domain.

    Write about your experiences or views below.