<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>GIS and Web Tricks &#187; iphone</title>
	<atom:link href="http://www.bernawebdesign.ch/byteblog/tagged/iphone/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bernawebdesign.ch/byteblog</link>
	<description>Thougts and tricks on GIS, web development and FOSS</description>
	<lastBuildDate>Tue, 31 Jan 2012 23:52:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>wordpress iphone app with wpml</title>
		<link>http://www.bernawebdesign.ch/byteblog/2009/08/07/wordpress-wpml-iphone-app/</link>
		<comments>http://www.bernawebdesign.ch/byteblog/2009/08/07/wordpress-wpml-iphone-app/#comments</comments>
		<pubDate>Fri, 07 Aug 2009 20:30:34 +0000</pubDate>
		<dc:creator>marco</dc:creator>
				<category><![CDATA[web 2.0]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[wpml]]></category>

		<guid isPermaLink="false">http://www.bernawebdesign.ch/byteblog/?p=68</guid>
		<description><![CDATA[Solution to use the iphone application for multilingual blogging with wpml]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve a client using a wordpress multilingual blog with the wpml plugin (<a href="http://wpml.org/">wpml.org/</a>) and that wants to use the wordpress iphone app (<a href="http://iphone.wordpress.org/">iphone.wordpress.org</a>). The app and the plugin work like a charm. the only problem is that in the app you can&#8217;t set the post language, hence you are (well by now were <img src='http://www.bernawebdesign.ch/byteblog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  ) able to post only using the default language.<br />
I wrote a small PHP script<span id="more-68"></span> 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.<br />
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&#8217;t because the script does something very small and irrelevant from the security point of view.<br />
And  here the code, enjoy.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #009933; font-style: italic;">/**
 * 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
 */</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//___________________SETTINGS________________</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;BLOG_ID&quot;</span><span style="color: #339933;">,</span>  <span style="color: #0000ff;">&quot;0&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//id of the blog</span>
<span style="color: #000088;">$active_languages</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;it&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;de&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//use lowercase codes of the languages active in your blog</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//___________________SCRIPT__________________</span>
<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'wp-config.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//used to get hostname, DB, USER &amp; PASSW</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/*check if the passed language is a valid one*/</span>
<span style="color: #000088;">$set_to</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;to&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$set_to</span><span style="color: #339933;">,</span> <span style="color: #000088;">$active_languages</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'invalid language'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/*open DB connection */</span>
<span style="color: #000088;">$mysqli</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> mysqli<span style="color: #009900;">&#40;</span>DB_HOST<span style="color: #339933;">,</span> DB_USER<span style="color: #339933;">,</span> DB_PASSWORD<span style="color: #339933;">,</span> DB_NAME<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">/* check connection */</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">mysqli_connect_errno</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'DB Connect failed'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* get the actual settings of the wpml plugin */</span>
<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'SELECT option_value from `'</span><span style="color: #339933;">.</span>DB_NAME<span style="color: #339933;">.</span><span style="color: #0000ff;">'`.`wp_options`
          WHERE CONVERT(`wp_options`.`option_name` USING utf8) = &quot;icl_sitepress_settings&quot;
          AND `wp_options`.`blog_id` = '</span><span style="color: #339933;">.</span>BLOG_ID<span style="color: #339933;">.</span><span style="color: #0000ff;">' 
          LIMIT 1;'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$mysqli</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">num_rows</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetch_assoc</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$value</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;option_value&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>  
  <span style="color: #666666; font-style: italic;">/* free result set */</span>
  <span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span> <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Error getting the actual setting'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* check if update needed */</span>
<span style="color: #000088;">$new_pattern</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'&quot;default_language&quot;;s:2:&quot;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$set_to</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&quot;'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$replace</span> <span style="color: #339933;">=</span> <span style="color: #339933;">!</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/<span style="color: #006699; font-weight: bold;">$new_pattern</span>/&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* change the language then update the DB */</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$replace</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$pattern</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'/&quot;default_language&quot;;s:2:&quot;[a-z]{2}&quot;/'</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$value</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pattern</span> <span style="color: #339933;">,</span> <span style="color: #000088;">$new_pattern</span><span style="color: #339933;">,</span> <span style="color: #000088;">$value</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'UPDATE `'</span><span style="color: #339933;">.</span>DB_NAME<span style="color: #339933;">.</span><span style="color: #0000ff;">'`.`wp_options` 
            SET `option_value` = \''</span><span style="color: #339933;">.</span><span style="color: #000088;">$value</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'\'
            WHERE CONVERT(`wp_options`.`option_name` USING utf8) = &quot;icl_sitepress_settings&quot;
            AND `wp_options`.`blog_id` = '</span><span style="color: #339933;">.</span>BLOG_ID<span style="color: #339933;">.</span><span style="color: #0000ff;">'
            LIMIT 1;'</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$mysqli</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Default post language is now set to '</span><span style="color: #339933;">.</span><span style="color: #990000;">strtoupper</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$set_to</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Problem updating the language, please try again'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Default post language is already set to '</span><span style="color: #339933;">.</span><span style="color: #990000;">strtoupper</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$set_to</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//close DB connection</span>
<span style="color: #000088;">$mysqli</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.bernawebdesign.ch/byteblog/2009/08/07/wordpress-wpml-iphone-app/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

