Quick fix for Cloudflare and PHP REMOTE_ADDR IP Detection

Some CMS-s, Frameworks, Custom Codes have built-in functions on collecting user logs (registrations, updates, submits, payments etc.) where they are using PHP’s $_SERVER[“REMOTE_ADDR”] variable to get the visitor’s IP address.

But as you know very popular Cloudflare Cloud Service passes all your traffic through its own servers – and that’s why all log function which i mentioned above writes Cloudflare’s IP address to the DataBase, not visitor’s actual IP address.

What to do for that.

As it is very common case, of course Cloudflare provides an alternative variable for that. It is called $_SERVER[“HTTP_CF_CONNECTING_IP”].

So if we are writing a code from scratch, we can use simple universal code to get the visitor IP:

if (!empty($_SERVER["HTTP_CF_CONNECTING_IP"])){
  $real_ip=$_SERVER["HTTP_CF_CONNECTING_IP"];
}
else {
 $real_ip=$_SERVER["REMOTE_ADDR"] ;
}

But what if we are working with existing code, core, class, FW, CMS? We can’t just to get and replace all $_SERVER[“REMOTE_ADDR”] variables with correct one.

The only thing we need is to fix it at initialization process.

Let’s write an example for WordPress.

wp-config.php is very good place to put your custom codes that will take an effect before WordPress init starts.

Just put this code piece to your wp-config.php file, and that’s all. REMOTE_ADDR variables will work OK everywhere inside WordPress (themes, plugins, core etc.)

if (isset($_SERVER["HTTP_CF_CONNECTING_IP"])) {
  $_SERVER['REMOTE_ADDR'] = $_SERVER["HTTP_CF_CONNECTING_IP"];
}

How to use the plugin template file as a theme template in WordPress

If the plugin contains some UI components, we can easily use it in the theme’s UI by putting widgets, shortcodes, JS enqueued scripts etc.

But there might be some plugins which may have completely independent UI template.

What to do in such cases? The plugin may have its own template with its own CSS, JS, UI – Say active theme is built on some custom CSS framework – but your plugin should use its own UI built on Bootstrap Framework. So they can’t live together if we just enqueue the plugin’s CSS, JS and use our UI as a shortcode or widget.

The only normal ways seems to create theme template logic inside the plugin.

Fortunately, it is possible and doesn’t need additional hacks and cheats.

Let’s first create some empty template file inside the plugin’s directory, called “the_plugins_template_file.php” – Custom Template

Continue reading “How to use the plugin template file as a theme template in WordPress”

Internal 500 error after directory renaming

Today in one client’s web hosting (it is Nexcess, but same problem can appear in another hostings using the same technology) i needed to rename WordPress home directory. (It was installed in a sub-directory)

I renamed directory, then modified .htaccess file, and then i changed siteurl and home options in wp_options table.

These simple steps above are quite enough for making WP to work in new directory. But i started getting strange errors instead.

Fatal error: require_once(): Failed opening required ‘/chroot/home/sitename/OLDNAME/wp-blog-header.php’ (include_path=’.:/opt/…/php’) in /chroot/home/sitename/OLDNAME/wp-blog-header.php on line 52

I have checked cache layers (all cache layers were turned off at that time) and configurations. Nothing wrong. Everything was provided correctly. Then i tried to print directory name before the line

require( dirname( FILE ) . ‘/wp-blog-header.php’ );

at index.php file. After saving and testing i saw that above error was solved and i started getting the same error for another file:

Fatal error: require_once(): Failed opening required ‘/chroot/home/sitename/OLDNAME/wp-load’ (include_path=’.:/opt/…/php’) …

This time wp-load.php can’t be loaded. At this time i figured out that there is some hosting based software which can’t be refresh itself after renaming directory (BTW, apache or httpd restart doesn’t help in this case) – but if you modify the file, it pings that software and that file starts to be recognized by webserver.

So there were two ways in order to solve the problem under those conditions.

  • To modify thousands of WP files one by one – which would be stupid time wasting.
  • To send some “ping” to all files. – That’s what i did. I opened SSH command line and run this command:

find -type f -exec touch {} +

This command doesn’t modify any file, it just modifies their timestamp data which is quite enough in our case.

After running this command the website started working normally under its new name.

WordPress AMP Plugin – Use Adsense in Classic Mode

There are 2 popular AMP plugins for WordPress:

  1. Official WordPress AMP plugin: AMP
  2. 3rd party AMP plugin: AMP for WP

AMP for WP should have an option for Adsense. But official one not.

I have made small inspection and noticed that all existing adsense codes are converted to amp tags – without any issues. But there is not any Ads JS file which should render these tags.

Its documentation is not good and doesn’t provide easy to fix information about this.

First i tried to add the needed JS code (for all type of ads) and tag (for auto-ads) correspondingly with wp_head and wp_footer native hookd. But no luck.

Classic mode of AMP plugin doesn’t fire any UI hook. Because it has own small template file.

So i decided to inspect the plugin’s code and to see which hooks it has for own small template.

Continue reading “WordPress AMP Plugin – Use Adsense in Classic Mode”