Solving slowness of php mail() function

If you suddenly noticed that mail() functions works very slowly and causes long page loads, here is quick solution.

By default php mail() functions works via SendMail service which requires having correct hostname at your server.

You can check it by typing hostname

in SSH terminal.

You probably will see something like ubuntu-16gb-nb1-1 or centos-4gb-cl1 etc.

But you must have correct hostname there. Type this command and it will set correct hostname.

hostname yourwebsite.com

After getting correct hostname add newly added one to /etc/hosts.

Open /etc/hosts with

nano /etc/hosts

command and you will see the line like

127.0.1.1 ubuntu-16gb-nb1-1  ubuntu-16gb-nb1-1

Comment or remove that line and add this line instead

127.0.0.1 localhost.localdomain localhost yourwebsite.com

Save it by clicking CTRL+O and that’s all. Test mail() function again, it will work as fast as rocket.

How to keep social share counts after migrating to SSL

When you migrated your website to SSL protocol (from http to https), you probably noticed that you had lost all your previous share counts. How to keep them?

We can think on different approaches for them.

Main point: Unfortunately it is not possible via social networks APIs themselves. That’s why we need some tricks.

Here is how i solved this problem and retained back to my old posts’ social share counts.

I am using custom embed codes for social share buttons. Here is facebook embed code i am using:

Now we need to apply our trick and to retain old share counts.

Here is the logic:
1. We remember the date we migrated to SSL.
2. We check if current post was published before or after that date.
3. If it is published before that date, we pass Facebook our URL with HTTP version, in order to save old share counts.

That’s all.
I have migrated to SSL at 20th May, 2018. That’s why i check if post_date is greater or less than 2018-05-20 06:30:03, if it is less, then i change HTTPS to HTTP.

 

Before:

After:

It is possible to apply same logic to other social network embed codes.

WordPress Search – Redirect plural search terms to singular ones

Today I am sharing quick gist which can redirect plural search terms to the results with singular search terms.
Here is when it can be useful:
Let’s say you have some article/page/product which contains the word “e-book”. But your visitor enters your website and searches for the term “e-books”. By default search algorithm he/she will get “not found” message. Continue reading “WordPress Search – Redirect plural search terms to singular ones”

Scheduled Banners and Template Parts in WordPress

It is normal when we meet requirement to show/hide any section by time change. I mean when we meet a plugin or theme, it is possible to create a section which displays Content A for T1 time interval, and which diplays Content B for T2 time interval.

I give 3 common examples for such cases.

1. To switch banner image when day ends and night begins and vice-versa.
2. Weekend section – to show weekend special section. It appear when Saturday arrives, and disappears when Monday arrives.
3. Time interval specific section or Campaign section – It appears when the given campaign is active. (for example between 28-31th days of the month)

scheduled banner for wordpress

And all of these should be automatized. I mean, we should not to edit any code by arriving/ending of the given dates. All of these can be scheduled and automatized.

So let’s write sample codes for these 3 cases: (This codes don’t contain any part about managing datetime parameters via administrative interface. The codes are simple and do exactly the needed things)

1. Day-Night Banner Switching:

Weekend Banner Section:

Campaign Section for the given datetimes:

Of course, when we have global targeted project, these codes may not be enough, we may need additional Client-Time zone argument to set. To get it is not problem, we can get it through AJAX. We have related article about this topic, passing cookie via AJAX.

Getting the list of slow MySQL queries in WordPress

Saving MySQL queries is the part of WordPress Debug processes.
If the website works slowly, probably there are some problematic MySQL queries which can be from some plugin or your current theme. Without debugging we can’t know what happens under the hood.

If to define SAVEQUERIES constant in wp-config.php,  we can monitor all running queries in single process. To see that we can add simple code to our themes footer.php.  Here is how it looks like

wordpress mysql debug

But it might not be enough helpful if we can’t catch the problem in our testing process. We may not detect the problem in action, but the visitors still complain about it.  So what to do? Let’s keep SAVEQUERIES ON mode for some time and gather all slow queries for that period. At the end of this period we will be able to see all problematic queries and may be we will be able to solve them.

  1. Add this string to our wp-config.php : define( ‘SAVEQUERIES’, true );
  2. Add the code below to your theme’s footer.php. It will gather slow queries and save it to as WP Option data.
  3. After some time passed, check get_option(‘custom_mysql_debug’); value, it will show all slow MySQL queries in one string, one per line. Here is what it will look like.
    mysql-debug-result