Category Archives: wordpress

Remove qTranslate from WordPress and keep translations

This website goes BACK in time, hell if it does. It started maybe in 2001, with static HTML. Then I played for a while with SHTML, before jumping head on to PHP and writing my own, ugly CMS.

I already had italian and english articles translated in here, and when I switched to wordpress (oh, joy and pain) I found this nifty plugin called qTranslate that kind of automated the translation management.

Looked like a good idea back then, so I installed it and moved all bilingual articles in it.

Yeah, looked like a good idea back then.

After a while though, as WordPress updates progressed, I noticed how I couldn’t write new posts anymore because the custom editor changes broke… either that, or I had to manually add the language tags, or I had to keep back the version of WordPress not to lose editor function. NO BUENO!

Until qTranslate stopped working altogether, and sorry guys, it’s not mantained anymore, f*ck you!

Luckily qTranslate-X was released, giving some more oxygen to my rare yet constant contribution to this blog.

Then, guess what, even qTranslate-X was discontinued.

Luckily qTranslate-XT came out, and it’s on GitHub, so as far as I see it’s actively followed, developed, improved… stil it doesn’t cut it for me.

I mean, developers are doing a GREAT job… can you imagine what a huge hassle is following development of a tool so complex, and coordinating the efforts of several people, while trying to keep the code working after major WordPress updates are released?

There must be a ot of people who thank anything that is sacred for qTranslate-XT’s existence.

I’m not one of those, since especially lately, I’m either releasing articles in italian, or in english, so there is not a lot of translations going on.

Everytime I searched for methods to remove qTranslate, every strategy involved choosing a language to keep, and just thrasing the others! As if I didn’t invest a LOT of time translating! Why should I waste al this work?

I used to think the work to do that by myself was going to be immense so I never tried, until today, when I achieved the objective, and am now happily composing, on Gutenberg editor, with a qTranslate-less version of my blog, where every article has been kept, and the URL redirection of “ephestione.it/it/title” has been fixed and redirected to “ephestione.it/it-title”.

What’s the strategy? Well, I built the code for my own website (and I am NOT willing to customize it for yours, unless you offer to pay me), so these are the premises:

  1. Not every article is bilingual, on the contrary most are either in english or italian
  2. I obviously want to mantain both languages
  3. I don’t care if the blogroll will show both italian and english articles (some the translation of the other) in the same sequence
  4. I want to keep the existing database entry, and add another database entry for the additional language (if present), in this second case english is kept to its original database row, and italian is inserted as a new row
  5. It is made to work with bilingual sites, but in reality it will most definitely work fine with multilingual sites and you may even not need to edit anything; still, you are expected to have familiarity with PHP to run it with confidence (BACKUP DATABASE FIRST!!!!11!!!1oneone)

Following is the code.

<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$dbhost="localhost";
$dbname="dbname";
$dbuser="dbuser";
$dbpass="dbpass";

function doquery($query) {
	$result=mysqli_query($GLOBALS["dbcon"],$query);
	if ($report=mysqli_error($GLOBALS["dbcon"])) {
		die($report);
	}
	return $result;
}

function excerpt($string) {
	return substr($string, 0, 30)."...";
}

$erroremysql=false;

$GLOBALS["dbcon"]=@mysqli_connect($dbhost, $dbuser, $dbpass);
if (mysqli_error($GLOBALS["dbcon"])) $erroremysql=true;
@mysqli_select_db($GLOBALS["dbcon"],$dbname);
if (mysqli_error($GLOBALS["dbcon"])) $erroremysql=true;
@mysqli_set_charset($GLOBALS["dbcon"],'utf8');
if (mysqli_error($GLOBALS["dbcon"])) $erroremysql=true;

$posts=doquery("SELECT * FROM wp_posts WHERE post_type='post'");

$a=array("<!--:","-->");
$b=array("\[:","\]");
$lang=array("it","en");
$main="en";

echo '<font face="Courier New">';

while ($post=mysqli_fetch_assoc($posts)) {
	echo "<strong>post {$post["ID"]}</strong>:<br/>";
	if (strpos($post["post_title"],"[:en]")!==false || strpos($post["post_title"],"[:it]")!==false) {
		$s=$b;
	}
	else if (strpos($post["post_title"],"<!--:en-->")!==false || strpos($post["post_title"],"<!--:it-->")!==false) {
		$s=$a;
	}
	$data=array();
	foreach ($lang as $l) {
		if (preg_match('/'.$s[0].$l.$s[1].'([\s\S]+?)'.$s[0].$s[1].'/',$post["post_title"],$matches)) {
			$data[$l][0]=$matches[1];
			preg_match('/'.$s[0].$l.$s[1].'([\s\S]+?)'.$s[0].$s[1].'/',$post["post_content"],$matches);
			$data[$l][1]=$matches[1];
		}
	}
	if (count($data)>1) {
		foreach ($data as $k=>$v) {
			echo "$k: ".excerpt($v[0])." - ".excerpt(strip_tags($v[1])).(($k==$main)?" main":"")."<br/>";
			//it is the main language, just updates post stripping the other language
			if ($k==$main) {
				doquery("UPDATE wp_posts SET post_title='".mysqli_real_escape_string($dbcon,$v[0])."', post_content='".mysqli_real_escape_string($dbcon,$v[1])."' WHERE ID=".$post["ID"]);
				echo "\n";
			}
			//it is not, so creates a new post copying over the rest of the data
			else {
				doquery("
					INSERT INTO wp_posts (
						post_author,
						post_date,
						post_date_gmt,
						post_title,
						post_content,
						post_modified,
						post_modified_gmt,
						post_name)
					VALUES (
						{$post["post_author"]},
						'{$post["post_date"]}',
						'{$post["post_date_gmt"]}',
						'".mysqli_real_escape_string($dbcon,$v[0])."',
						'".mysqli_real_escape_string($dbcon,$v[1])."',
						'{$post["post_modified"]}',
						'{$post["post_modified_gmt"]}',
						'".$k."-{$post["post_name"]}')
				");
				echo "\n";
			}
		}
	}
	else  {
		echo "1: ".excerpt($data[key($data)][0])." - ".excerpt(strip_tags($data[key($data)][1]))." main"."<br/>";
		doquery("UPDATE wp_posts SET post_title='".mysqli_real_escape_string($dbcon,$data[key($data)][0])."', post_content='".mysqli_real_escape_string($dbcon,$data[key($data)][1])."' WHERE ID=".$post["ID"]);
		echo "\n";
	}
}

This is what you need to add to .htaccess in the root of your public_html folder, obviously adapting it to your needs, and adding more, similar rows if you have additional languages:

RewriteRule ^it/([a-z0-9\-\_]+)/$ /it-$1/ [R=301,L]

In my case, it worked like a charm, even if not without some cold sweats.

Center caption images not centered inside WordPress posts

Just got tired of how the images inside my posts didn’t get centered, but instead were aligned to the left, even if I had set them to be centered, and they were rendered to inside the WordPress WYSIWYG editor.

I use the Arclite theme, which lets you add personalized CSS styles inside the settings, but I guess you can use other methods to do the same with your them, or add them manually if it comes to that

What I added to the CSS was:

.aligncenter {margin-left:auto;margin-right:auto;}

which is apparently working, as I just did it, and the images are centered now in firefox. Didn’t bother to test IE.

  This article has been Digiproved

Protect your WordPress contents from plagiarism with a digital timestamp

Since a while ago, I decided to release all the contents I produce with a Creative Commons license. Why? Well, once I publish my stuff online, anyone with an internet connection can read it; so, if they like it and want to make it more widely available, a stricter copyright policy would compell them to ask me for permission and blah blah; if people like the contents I produce and distribute them around, I’m happy about it, as long as it’s made clear that I am the original author and noone else tries to take credit for my work (because that would obviously piss me off big time). The CC license I chose is the NC-SA, which also forbids any commercial use of my contents (if someone’s going to earn money out of my work, then I want my fair share), and allows re-use of the contents as long as they are released with the same license (I wouldn’t want someone to take my work, reassemble it at his own liking, and then lock it down even if it’s not really theirs).

Well, all’s good until now, but what if someone tries to “steal” your contents? The things you write are legally yours since the moment they are written (as long as they are original, naturally), yet you need to prove they’re yours in case of a dispute. If someone copies one or more articles off your website, he could very well decide to say that those contents are his, and you copied them off him; your word against his.

The only way to “prove” that a written text is yours, is to prove that it was in your possession before anyone else got it; “timestamping” is the keyword, a timestamp which is legally valid (meaning that noone would be able to prove it’s fake), and that binds the digital works with your identity; it’s best if you get a digital timestamp before releasing your work, in fact, if an “attacker” has the means to download the contents off your website and certify them before you do, he would become the new technical owner of your contents.

There are several ways to get a timestamp, free and paid, more and less solid. A free digital timestamp service is provided by Timemarker, but it’s only available for manual timestamping, that is you have to go to their site and provide them with your contents, then download their timestamp PGP signature. It’s good if you need to timestamp images, or archives.

On the other side, if the contents you want to timestamp are written posts on your WordPress blog (and this is all this article is about), the only real good service available to occasional bloggers like me (those who do not make a living with their website) is the free Copyright Proof WordPress Plugin (wordpress.org plugin repository has the most updated changelog), which gets you the best for its price. As soon as you register a free account on Digiprove (they also offer paid timestamping for professional, non-Wordpress users), you can insert your own API key in the plugin, and it will take care of everything: as soon as you hit the publish button in the WordPress editor, before actually publishing the article its written contents will be submitted to Digiprove server for secure timestamping, and once the timestamp information is obtained, the article will be online along with its certificate (you can control how the digiprove timestamp appears both on your website and on their servers, and you can also see the badge at the bottom of this post). Same thing happens when you edit the article, the certificate will be updated to reflect the changes on the article, and the older certificates, even if not linked anymore, will still be valid for the previous revisions of the article.

This service, again, is free, and does its job perfectly. Cian Kinsella, Digiprove’s CEO, told me they’re going to improve the way the plugin works, to make it dynamically create the certificate HTML code inside the post (it’s currently hardcoded, with CSS styling done inside the tags), and he also anticipated that, after this change has been done, they will be probably adding a feature I requested, that is a sort of “older certificates drawer”, thanks to which the reader can access the links to the older timestamps of the previous revisions of your page which reside on their servers, and are currently lost (the links, not the certificates); this will greatly improve the functionality of the plugin for those who prefer updating their older posts with newer info, instead of opening new posts.
By the way, even if Cian is CEO of two different companies, and I don’t know his daily schedule but I suppose it should be pretty busy, he still managed to personally reply to my mails to Digiprove’s support, which is good!

Since a while ago, I decided to release all the contents I produce with a Creative Commons license. Why? Well, once I publish my stuff online, anyone with an internet connection can read it; so, if they like it and want to make it more widely available, a stricter copyright policy would compell them to ask me for permission and blah blah; if people like the contents I produce and distribute them around, I’m happy about it, as long as it’s made clear that I am the original author and noone else tries to take credit for my work (because that would obviously piss me off big time). The CC license I chose is the NC-SA, which also forbids any commercial use of my contents (if someone’s going to earn money out of my work, then I want my fair share), and allows re-use of the contents as long as they are released with the same license (I wouldn’t want someone to take my work, reassemble it at his own liking, and then lock it down even if it’s not really theirs).

Well, all’s good until now, but what if someone tries to “steal” your contents? The things you write are legally yours from the moment they are written (as long as they are original, naturally), yet you need to prove they’re yours in case of a dispute. If someone copies one or more articles off your website, he could very well decide to say that those contents are his, and you copied them off him; your word against his.

The only way to “prove” that a written text is yours, is to prove that it was in your possession before anyone else got it; “timestamping” is the keyword, a timestamp which is legally valid (meaning that noone would be able to prove it’s fake), and that binds the digital works with your identity; it’s best if you get a digital timestamp before releasing your work, in fact, if an “attacker” has the means to download the contents off your website and certify them before you do, he would become the new technical owner of your contents.

There are several ways to get a timestamp, free and paid, more and less solid. A free digital timestamp service is provided by Timemarker, but it’s only available for manual timestamping, that is you have to go to their site and provide them with your contents, then download their timestamp signature. It’s good if you need to timestamp images, or archives.

On the other side, if the contents you want to timestamp are written posts on your WordPress blog (and this is all this article is about), the only real good service available to occasional bloggers like me (those who do not make a living with their website) is the free Copyright Proof WordPress Plugin (wordpress.org plugin repository has the most updated changelog), which gets you the best for its price. As soon as you register a free account on Digiprove (they also offer paid timestamping for professional, non-Wordpress users), you can insert your own API key in the plugin, and it will take care of everything: as soon as you hit the publish button in the WordPress editor, before actually publishing the article its written contents will be submitted to Digiprove server for secure timstamping, and once the timestamp information is obtained, the article will be online along with its certificate (you can control how the digiprove timestamp appears both on your website and on their servers, and you can also see the badge at the bottom of this post). Same thing happens when you edit the article, the certificate will be updated to reflect the changes on the article, and the older certificates, even if not linked anymore, will still be valid for the previous revisions of the article.

This service, again, is free, and does its job perfectly. Cian Kinsella, Digiprove’s CEO, told me they’re going to improve the way the plugin works, to make it dynamically create the certificate HTML code inside the post (it’s currently hardcoded, with CSS styling done inside the tags), and he also anticipated that, after this change has been done, they will be probably adding a feature I requested, that is a sort of “older certificates drawer”, thanks to which the reader can access the links to the older timestamps of the previous revisions of your page which reside on their servers, and are currently lost (the links, not the certificates); this will greatly improve the functionality of the plugin for those who prefer updating their older posts with newer info, instead of opening new posts.
By the way, even if Cian is CEO of two different companies, and I don’t know his daily schedule but I suppose it should be pretty busy, he still managed to personally reply to my mails to Digiprove’s support, which is good!

  This article has been Digiproved

reCAPTCHA Input error: Invalid referer and WordPress

Just today someone (thank you) pointed out there was an error with the recaptcha plugin in my WordPress comments. Checking it out, that “Input error: Invalid referer” is a generic error noone has ever pointed out to a single cause.

At first I made sure the keys used by the reCAPTCHA plugin were correct, and created a brand new website domain in the reCAPTCHA admin panel, and updated the values in the plugin settings. That way, I could see the captcha image correctly when loaded while being logged in, but as soon as I logged out the error was back there. I checkd the page source, and I noticed the “challenge” value of the HTML line that called the external javascript was different from the public key I updated in the plugin settings.

After some thinking, it turns out that the WP Super Cache plugin in some cases interferes with reCAPTCHA plugin, as it caches the old plublic key value which of course makes reCAPTCHA mad. Clearing the cache, thus reloading the whole page, solved the problem, and the captcha image was back there to be solved.

How to fix Fatal error: Allowed memory size exhausted in WordPress 3

This error is nothing new, per se, as far as I know it was present also in previous versions of wordpress (I’m a fresh new user and I’ve been using v3+ for more than I’ve been with 2.9.7 (first one I installed).
When this error occurred in previous versions, all you needed to do was open the [WORDPRESS FOLDER] > wp-settings.php file, and change the maximum allowed allocable ram from there. And that’s what most guides still tell you to do now, leaving you stranded as that line you need to change is not there anymore.
What you see though, is that at the beginning of the wp-settings.php file there is a line that says:

require( ABSPATH . WPINC . '/default-constants.php' );

which pretty much says it all, unless you are not the least bit PHP savvy (hence, this guide is for you).
All you need to do is open the [WORDPRESS FOLDER] > wp-includes > default-constants.php file, and change the line:

define('WP_MEMORY_LIMIT', '32M');

into

define('WP_MEMORY_LIMIT', '40M');

or anything of your liking (may be 36M, 48M, or whatever depending on your setup); obviously, the lower the better for the performance of your server, especially if you’re on a shared hosting.

  This article has been Digiproved