W3tc plugin feed problem

When your WordPress site has a great number of requests it could happen that your server can’t handle all the requests and can become very slow, rising errors, getting timeouts and -at last- can stop working. This happens because your WordPress site make a lot of work to render a page.

To avoid those problems, usually, developers use a cache. This means that when a visitor requests a page, the server create the page and stores it in a ready-to-serve version, an html static copy og page that doesn’t need database anymore. When there is a cache working, new requests are sent to the php WordPress site and processed as usual, instead if a page has been already processed, the static version stored in the cache is served:

This lets your WordPress site go faster.

Wt3c plugin is a tool for WordPress that lets you control how your WordPress site handles cache at many levels: you can control page cache, db cache, javascript and css cache, you can handle an external cdn link to handle files better and much more. It’s one of the best caching plugin service, but it has a problem: it caches also rss feed, and you can’t tell him to not cache feed unless you tell him to also don’t cache categories and tags pages.

In the next version (now it’s 0.9.2.4) this control will be fixed, they said, but at the moment this issue prevents correct working for services like Feedburner, or the refresh in Google reader or any other feed reader you use, since w3tc keeps serving the bad version of your feed until the cache becomes old or someone manually clears the cache.

You can fix it by editing the .htaccess and telling w3tc to handle your feed as an admin page, without caching it, this way, from this:


RewriteCond %{REQUEST_URI} !(\/wp-admin\/|\/xmlrpc.php|
\/wp-(app|cron|login|register|mail)\.php|wp-.*\.php|index\.php) [NC,OR]

to:


RewriteCond %{REQUEST_URI} !(\/wp-admin\/|\/xmlrpc.php|
\/feed\/|\/wp-(app|cron|login|register|mail)\.php|wp-.*\.php|index\.php) [NC,OR]

(this is on two lines but you have to piut it on a single line).

This seems to work for me. :-)

13 mysql tips that you don’t know

Here is a list of thirteen tips that can be usefull for your queries. If you know more tips send comments.

1 REMAP VALUES INSIDE A QUERY USING CASE…WHEN SYNTAX

SELECT id,title,
   (CASE date WHEN '0000-00-00' THEN '' ELSE date END) AS date
   FROM your_table

SELECT id,title,
   (CASE status WHEN 0 THEN 'open' WHEN 1 THEN 'close' ELSE 'standby' END) AS status 
   FROM your_table

2 FIND DUPLICATE RECORDS WITH EMAIL FIELD

SELECT email, COUNT(email) AS q 
   FROM emails_table GROUP BY email HAVING q > 1 
   ORDER BY q DESC

3 EXTRACT RECORDS WITH A RANDOM ORDER

SELECT * FROM your_table ORDER BY RAND()

4 REPLACE STRINGS IN A FIELD WITH AN UPDATE

UPDATE your_table 
   SET name=REPLACE(name, 'John', 'Johnny')
   WHERE name LIKE '%John%';

5 RESET THE AUTOINCREMENT COUNTER IN A TABLE

ALTER TABLE your_table AUTO_INCREMENT = 100

Next record you insert will have id=100.

6 ADD AN AUTOMATIC INCREMENT COLUMN FOR A SELECT

set @N = 0;
SELECT @N := @N +1 AS number, name, surname FROM people;

7 JOINING FIELDS WITH CONCAT FUNCTION

SELECT CONCAT(name,' ',surname) AS complete_name FROM users

8 SELECT PARTIAL DATE VALUES WITH DATE FUNCTIONS

SELECT id,title, YEAR(date_field) FROM your_table
SELECT id,title, 
   CONCAT(MONTH(date_field),'/',YEAR(date_field)) as new_date
   FROM your_table

9 INSERTING ROWS IGNORING DUPLICATES ON A FIELD WITH UNIQUE KEY
On a table “tags” with a unique key on ‘tag’ field:

INSERT IGNORE INTO tags (tag) VALUES ('good');

You can run this query many times, no error will be returned.

10 USING FULLTEXT INDEX AND MATCH AGAINST SEARCH

select * from articles where MATCH(content_column) AGAINST ('music')

To work, this tip, need to add the full text index on the content_column. Note that if you already have a table filled with data, adding the index will not create it… so you have to start from an empty table.

11 HOW TO SAY “ONE MONTH AGO” IN MYSQL

SELECT user, count(*) AS logins 
   FROM stat_log 
   WHERE action='LOGIN' AND dt_when >= DATE_ADD(CURDATE(), INTERVAL -1 MONTH)
   GROUP BY user

This where clause with dt_when lets you count the records that has date greater or equal to one month ago.

12 SET CORRECT CHARSET

SET NAMES 'utf8';

Run this query after your connection starts. More info here.

13 INSERTING FROM A TABLE TO ANOTHER

INSERT INTO yourtable (field1,field2,field3) 
   SELECT newfield1,newfield2,'fixed value'
   FROM yourtable2

You can use this construct to copy rows from a table to another and add also some values that you specify in the second part of the query.

Do you know other tips? Write them in the comments.

How many times a web link has been shared on Twitter

Twitter share button and Facebook share button are the most used buttons to share links on Internet. You can read Facebook count as I’ve explained here, and you can also read the number of the times that your urls have been tweeted on Twitter.

The code is very simple because there is a very simple public api to call. So, let’s call the twitter api, and parse the results. The results are parsed with the preg_match function but you can use also the json_decode function, probably it is better. As you can see there is no need to use curl library, we can just use file_get_contents.

If you store this count and the facebook total like count, you can show those data better on your site and you can use those data to make some “top stories” list.

function readTwitterShares($url) {
 $s = file_get_contents("http://urls.api.twitter.com/1/urls/count.json".
   "?callback=?&url=".urlencode($url));
   preg_match("#(\"count\"):([0-9]*)#",$s,$ar);
   return isset($ar[2]) ? $ar[2] : 0;
}

echo readTwitterShares("http://www.dailybest.it");

This function will be soon included in the Mini Bots Class.

Updated version of the the Facebook Connect Tutorial

I’ve written un updated version of the Facebook Connect Tutorial, you can find it in the top menu of Barattalo.it. This tutorial shows you how to create an app on the facebook developer site and how to integrate the facebook php libraries to develop the login process on your site.

How to read facebook likes count from PHP


When you add facebook like button to your site, probably, you also want to save the number of likes of your pages to your database.These data are very useful and very important because they tell you which of your articles are better than others. Moreover, these pages are better than others because the user has decided to tell his friends about it.

So you can use these likes to make a top stories section on your site as I make on the top of DailyBest.

You can read these data because facebook likes and facebook shares are public data in Open Graph, you don’t need any authentication process to read likes, they are public and you can easily read facebook likes count with php or with jquery. Here is the code to read facebook likes and shares count with php.

function readFacebookLikes($url) {
	$query = "select total_count from link_stat WHERE url ='" . $url ."'";
	$s = file_get_contents("https://api.facebook.com/method/fql.query?query=".
             urlencode($query)."&format=json");
	preg_match("#(\"total_count\"):([0-9]*)#",$s,$ar);
	if(isset($ar[2])) return $ar[2]; else return null;
}
echo readFacebookLikes("https://www.facebook.com/rockit.tuttarobaitaliana");
echo readFacebookLikes("http://www.dailybest.it");

This function will be added to Minibot class.

As you can see in the code you can just make a query using the fql language (it’s very similar to normal sql language) and retrieve data from the the facebook link_stat table. You can retrieve different data, I’ve used “total_count”:

share_count The number of times users have shared the page on Facebook.
like_count The number of times Facebook users have “Liked” the page, or liked any comments or re-shares of this page.
comment_count The number of comments users have made on the shared story.
total_count The total number of times the URL has been shared, liked, or commented on.
click_count The number of times Facebook users have clicked a link to the page from a share or like.