Create wordpress language file with poedit

Giving users the ability to translate your plugin is important. So you should be using __, _e, _x …etc in your plugins as myuch as possible.

In your plugin folder, create a lang sub-foldder to store your language files. In your init plugin file, make sure you have the localisation code:

$domain = 'your-plugin-name';
$locale = apply_filters('plugin_locale', get_locale(), $domain);
// let user customise the lang in a dfferent folder.
load_textdomain($domain, WP_LANG_DIR.'/my-plugin/'.$domain.'-'.$locale.'.mo');
// load plugin locale
load_plugin_textdomain( 'your-plugin-name', false, dirname( plugin_basename( __FILE__ ) ) . '/lang/' );

next download poedit from http://www.poedit.net/

1) Open poedit and fill in the project info.
2) Under the paths tab, fill in the base path, eg /home/username/your-wordpress-project/wp-content/plugins/plugin-name/. Then add a “.” under the Paths input box below.
3) Under the keywords tab, add “__” and “_e”
4) Click ok and when prompted to save the file, save it in your plugin lang folder in the form of your-plugin-name_lang_locale format, eg your-plugin-name_en_AU

You should get 2 files of with the .mo and .po extension.

good luck.

H264 video encoding and transcoding using winff in ubuntu

winff is the gui version of ffmpeg (now superseded by avconv). Its the perfect tool to convert, resize and manipulate your video files.

Installing winff is easy, if using ubuntu/debian, just do a “apt-get install winff h264enc libav-tools” in command line.

Step 1: use avconv instead of ffmpeg

now I want to use avconv instead of ffmpeg. After opening up winff under applications/sound and video, go to edit/preferences/linux change the ffmpeg exec field to /usr/bin/avconv and ffplay exec field to /usr/bin/avplay

Step 2: Add new h264 profile

None of the current presets look good for h264 encoding. Under edit/presets, Add a new preset name under the websites category. In the preset command, enter

// refer to "man avconv" documentation on the options to use
-f mp4 -acodec copy -vcodec libx264

Step 3: now back to the main panel, select “websites” category and “h264” profile. Then change the video, audio…etc settings accordingly. Click on “Convert” and you will see a pop up terminal. The popup terminal provides tonnes of information on the video stats and debugging information should you wish to google for solutions.

I really hope there would be more documentation in this area rather than bits and pieces everywhere.

transmageddon is also good and simple linux video transcoder alternative but lacked the flexibility that winff has.

vimeo also provides some good compression chart.

Good luck.

Recommended bitrate for streaming videos

1000kbps for 360p (640 x 360px) seems to be spot on in most cases. vbr is preferred for http streaming, so it can be 750kbps – 1500kbps. we can still go higher for high motion videos. Many people use kush gauge as a rough guide..

Frame width * frame height * frame rate * motion rate (1,2 or 4) * constant (0.07)/1,000 = target bit rate in kilobits per second (kbps)

more information about kush gauge can be found here

Looks like the market doesn’t have any standard. Many enterprise businesses pump up their bitrate a bit higher to have better quality videos. We shouldn’t have disk space constraint. the only constraint is the user’s internet connection. The world’s average internet connection speed is 2300kbps (http://www.tomshardware.com/news/akamai-internet-broadband-speeds-bandwidth,15525.html) – a figure that you want to take note of when streaming videos.

good luck.

Woocommerce: Allow users to extend downloadable products expiry after login

There are a few ways that we can protect downloadable product links in woocommerce.

Option A: Limit the number of download per user

Option B: Enforce Logged-in before download.

Option C: Set an expiry date for the download link.

Personally, I dislike option A and B because they are a hassle. Option C sounds more viable but the complain might come in after the expiry date. I think the best solution is to allow users to download the product immediately upon purchase (or in the email) and subsequently, they have to be logged in order to download the product. However, woocommerce doesn’t allow “log in to download” per product (we have to do it globally). So another option is to extend the expiry date everytime the user login. We can do this using the woocommerce_after_my_account hook.

# extend download expiry date by 1 day everytime user logins to dashboard
add_action('woocommerce_after_my_account', 'update_downloadable_expiry_now');
function update_downloadable_expiry_now() {
  global $wpdb;
  $table = $wpdb->prefix.'woocommerce_downloadable_product_permissions';
  $query = $wpdb->prepare("update $table set access_expires=CURDATE() + INTERVAL 1 DAY where user_id='".wp_get_current_user()->ID."' and access_expires is not NULL");
  if ($wpdb->query($query) === FALSE) {
    _e("could not update downloadable product access expiry date.", 'woocommerce');
  }
}

put wordpress in maintenance mode quickly

while there may be nice wordpress plugins to put wordpress into maintenance mode, there is also a quick solution. WordPress is notorious for having bloated plugins that may slow down the site… so I love quick and less intrusive solutions.

Just create a an arbitrary file say .maintenance- under the wordpress installation dir. Put this code in the file:


<?php
$upgrading = time() - 1;
?>

When doing maintenance, just rename the file to .maintenance and change it back to the arbitrary filename .maintenance- when done. That’s it.

If you want a custom maintenance page, create the page under /wp-content/maintenance.php

if you want to allow admin to access the site while others see the maintenance mode, use this code:

function is_user_logged_in() {
$loggedin = false;
foreach ( (array) $_COOKIE as $cookie => $value ) {
        if ( stristr($cookie, 'wordpress_logged_in_') )
            $loggedin = true;
    }
    return $loggedin;
}
if ( ! stristr($_SERVER['REQUEST_URI'], '/wp-admin') && ! stristr($_SERVER['REQUEST_URI'], '/wp-login.php') && ! is_user_logged_in() )
    $upgrading = time();

w3tc – enchanced disk – Page Cache URL rewriting is not working

If you get this error when using “disk enchanced” for page cache

“It appears Page Cache URL rewriting is not working. If using apache, verify that the server configuration allows .htaccess or if using nginx verify all configuration files are included in the configuration.”

put the wordpress .htaccess rules under root below the w3tc insertion

……..

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ – [L]

# uploaded files
RewriteRule ^([_0-9a-zA-Z-]+/)?files/(.+) wp-includes/ms-files.php?file=$2 [L]

# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ – [L]
RewriteRule  ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]
RewriteRule  ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
RewriteRule . index.php [L]

</IfModule>

 

using set_transient in wordpress

set_transient is an excellent caching mechanism in wordpress. However one thing to note is that it depends on the caching plugin that is installed. So for example, if the plugin caching mechanism is memcache, the cache will be stored in memcache instead of in the db. Hence clearing the memcache from the plugin will clear your set_transient cache as well.

see
see http://codex.wordpress.org/Class_Reference/WP_Object_Cache

wordpress caching and mobile theme plugins

when using caching plugin such as w3tc in wordpress, it will try to cache the mobile version as well and there could be a possibility that mobile users might see the desktop version and vice versa. So the idea is to disable the caching plugin for mobile agents:

if using w3tc, under page cache -> rejected user agents, enter the full list of mobile agents and w3tc will not cache pages requested by them.

android
blackberry9500
blackberry9530
cupcake
dream
incognito
iphone
ipod
mini
webOS
webmate
2.0 MMP
240√ó320
ASUS
AU-MIC
Alcatel
Amoi
Audiovox
AvantGo
BenQ
Bird
BlackBerry
Blazer
CDM
Cellphone
DDIPOCKET
Danger
DoCoMo
Elaine/3.0
Ericsson
EudoraWeb
Fly
HP.iPAQ
Haier
Huawei
IEMobile
J-PHONE
KDDI
KONKA
KWC
KYOCERA/WX310K
LG
LG/U990
Lenovo
MIDP-2.0
MMEF20
MOT-V
MobilePhone
Motorola
NEWGEN
NetFront
Newt
Nintendo Wii
Nitro
Nokia
Novarra
O2
Opera Mini
Opera.Mobi
PANTECH
PDXGW
PG
PPC
PT
Palm
Panasonic
Philips
Playstation Portable
ProxiNet
Proxinet
Qtek
SCH
SEC
SGH
SHARP-TQ-GX10
SIE
SPH
Sagem
Samsung
Sanyo
Sendo
Sharp
Small
Smartphone
SoftBank
SonyEricsson
Symbian
Symbian OS
SymbianOS
TS21i-10
Toshiba
Treo
UP.Browser
UP.Link
UTS
Vertu
WILLCOME
WinWAP
Windows CE
Windows.CE
Xda
ZTE
dopod
hiptop
htc
i-mobile
nokia
portalmmm
vodafone

playing with cat and awk

cat can print out line number and awk lets you format it. This combo is excellent to manipulate files

creating a csv for eg.

cat -n phpbook.csv | awk '{print $2","$1}' > phpbook1.csv