Manually Adding WPML Strings for Translation

September 10, 2013

I have a new project which is a multi language web site and it requires me to insert to the WPML string tables so that the client can get all her property types and property features (of which there are hundreds) translated so that they will appear in 4 different languages. So rather than wasting time by manually adding them one by one, I have made a function which I can include in a loop to mass insert strings from a data source.

This php function assumes that your base language for the wordpress site is in english. If not then just change the 2 character language code in the sql statement below.

1
2
3
4
56
7
8
9
1011
12
//this is a special function so we can add strings to the WPML system in WordPress
function insert_new_wpml_string($name, $context, $string ) {
    //first we need to check if there is a string and context for the string to be inserted
    $sql="SELECT * from wp_icl_strings where language = 'en' and context = '".$context."' and value = '".$string."' ";
    $result=mysql_query($sql) or die(mysql_error());    $rows=mysql_num_rows($result);
        //if there is no record of the string in the context then add a new one
        if(!$rows){
            $guid = md5(uniqid(mt_rand(0,1000)));
            icl_register_string($context, $name . ' - ' .$guid, $string);        }
}

To test this as a function you can do it like this in php and include it in a loop to mass insert strings from a remote source.

1
2
3
4
$name = "Property Type";
$context = "Property Types";
$string = 'Semi Detached';
insert_new_wpml_string($name, $context, $string );

So for example, if you had an api with a list of property types, you can include this code to insert to the WPML strings table on a cron job, so when new property types are found, wordpress will do the rest for you.