wordpress iphone app with wpml
I’ve a client using a wordpress multilingual blog with the wpml plugin (wpml.org/) and that wants to use the wordpress iphone app (iphone.wordpress.org). The app and the plugin work like a charm. the only problem is that in the app you can’t set the post language, hence you are (well by now were
) able to post only using the default language.
I wrote a small PHP script to put in the root dir of your blog that updates the default language of the blog when called. so basically before using the iphone app you call the script using safari (http://domain.tdl/blog/set_post_lang.php?to=de) and then use the iphone application to post to your blog.
The script requires minimal configuration, you have to set the blog_id (if you have a single install, 0 should be ok, but you can find this by looking at the wp_options table in the DB) and an array of valid languages. Please note that the script uses no authentication methods, up to you to decide if you want to implement it (very easy actually for example with http_auth or with a key). I didn’t because the script does something very small and irrelevant from the security point of view.
And here the code, enjoy.
<?php /** * script to change the default language of a wpml installation * so that a new post written from fot example the iphone app is set to the right language * * @use call it like this: www.domain.tld/set_post_lang.php?to=it * @author Marco Bernasocchi marco@bernawebdesign.ch * @copyright LGPLv3 */ //___________________SETTINGS________________ define("BLOG_ID", "0"); //id of the blog $active_languages = array("it", "de"); //use lowercase codes of the languages active in your blog //___________________SCRIPT__________________ require_once('wp-config.php');//used to get hostname, DB, USER & PASSW /*check if the passed language is a valid one*/ $set_to = strtolower($_GET["to"]); if(!in_array($set_to, $active_languages)) die('invalid language'); /*open DB connection */ $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); /* check connection */ if (mysqli_connect_errno()) { die('DB Connect failed'); } /* get the actual settings of the wpml plugin */ $query = 'SELECT option_value from `'.DB_NAME.'`.`wp_options` WHERE CONVERT(`wp_options`.`option_name` USING utf8) = "icl_sitepress_settings" AND `wp_options`.`blog_id` = '.BLOG_ID.' LIMIT 1;'; if (($result = $mysqli->query($query)) && $result->num_rows == 1) { while ($row = $result->fetch_assoc()) { $value = $row["option_value"]; } /* free result set */ $result->close(); } else die('Error getting the actual setting'); /* check if update needed */ $new_pattern = '"default_language";s:2:"'.$set_to.'"'; $replace = !preg_match("/$new_pattern/",$value); /* change the language then update the DB */ if($replace){ $pattern = '/"default_language";s:2:"[a-z]{2}"/'; $value = preg_replace($pattern , $new_pattern, $value, 1); $query = 'UPDATE `'.DB_NAME.'`.`wp_options` SET `option_value` = \''.$value.'\' WHERE CONVERT(`wp_options`.`option_name` USING utf8) = "icl_sitepress_settings" AND `wp_options`.`blog_id` = '.BLOG_ID.' LIMIT 1;'; if ($result = $mysqli->query($query)) { echo 'Default post language is now set to '.strtoupper($set_to); } else echo 'Problem updating the language, please try again'; } else echo 'Default post language is already set to '.strtoupper($set_to); //close DB connection $mysqli->close(); ?>
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.