Find the file where the given function is defined

In single-app, micro-service development almost all functions belong to you or to your team, so no need to know who defined the given function – you probably know it already.

But in CMS development some you may want to find the file where the given function is defined.

It might be WP core, template functions, theme or any of active plugins.

To find it you can use built-in PHP class called “ReflectionFunction”.

For example create such simple hook.

Continue reading “Find the file where the given function is defined”

Cookie based permission in htaccess – restrict WordPress admin access by IP address

In my previous post about htaccess based restriction i showed how to restrict wp-admin or other file/page access by IP address. That’s very good solution when we are using VPN static IP.

But if we are using multiple connections (office, home, caffee, or multiple VPN-s with different IP addresses), then we need to have some easier solution.

And that can be cookie-based permission. Let’s see how to do that.

Continue reading “Cookie based permission in htaccess – restrict WordPress admin access by IP address”

Ready to use snippet for WP REST API

WP-REST is great tool to connect 3rd party applications to your website and interact with it.

If you want to use it deeply in your website, you need to go to official website and read its handbook.

but what if you just want to use it for some small task and you don’t want to lose more time on reading docs? Then you might need JS code for frontend, and some backend codes for initialization and processing.

In this article i am giving you ready to use snippet, you just put it to your theme’s functions.php and it works.

Of course you will need to change the codes inside response function. In my example i am giving you an example which counts post_views.

Let’s create simple WordPress rest api custom endpoint. Here we go:

Continue reading “Ready to use snippet for WP REST API”

How to log all WP-admin activities in WordPress

If you have several admin accounts, it is usual to see some options, settings changes where there is no any trace to see who has done that any change.

Of course it is not about Post, Page updates, they have some information about who updated the given post object.

But for the rest wp-admin pages there is no such trace collector.

Such kind of data can be useful if you have suspicion that someone you don’t have stolen admin password and logs in to your wp-admin as a wordpress ghost admin. So this snippet might be helpful when you want to stop/prevent wordpress admin panel hack too.

So, let’s write small snippet which will collect, who and when did any activity inside wp-admin.

Continue reading “How to log all WP-admin activities in WordPress”

WooCommerce – Add short link support when it returns 404

This quick post is about the issue when in WooCommerce website your WP short links (aka WordPress Plain URL) returns 404 error.

f.e. yourwebsite.com?p=912 should open (redirected to user friendly URL) the page of single post, where 912 is the ID of that post.

But in some WooCommerce websites due to complex permalinks rules, this redirection doesn’t work.

The reason is simple – The route requires the value for post_type – in order to determine what kind of post type is that $_GET[“p”] is.

So, let’s just help the route to find it:

Continue reading “WooCommerce – Add short link support when it returns 404”

Simple way to run WP Scheduled Crons

WordPress and Cron Jobs

As we know, by default WordPress is using its own pseudo-cron logic. It just runs at background, and while visitors don’t notice it, actual cron jobs runs when they are browsing our website.

Due to performance issues some developers decide disabling this auto-run at background and use custom calling instead.

For this purpose they simple add

 define('DISABLE_WP_CRON', 'true');

to wp-config.php file and then they add real cron job to their server’s crontab. Like this

* * * * * php /var/www/html/wp-cron.php

So it solves all problems. But, not all actually. I have faced some plugin’s where scheduled tasks are using some front-end data which doesn’t exist in php-cli mode.

That’s why for those cases i use simple trick.

Hybrid Solution – Keeping WP_Cron disabled at front-end, but with one exception.

Here is my code in crontab:

#you can keep this as well * * * * * php /var/www/html/wp-cron.php
*/5 * * * * wget --delete-after -qO- https://www.SITENAME.com/?custom_cron=1

And here is my code in wp-config.php

if(empty($_GET["custom_cron"])) define('DISABLE_WP_CRON', 'true');

These 2 small codes are doing this:

  • They are keeping wp_cron disabled for all visitors. So, no any according slowing down.
  • They let wp_cron to be enabled when custom_cron GET parameter exists.
  • And each 5 minutes, crontab calls https://www.SITENAME.com/?custom_cron=1 URL and runs all tasks via wp’s own cron.