Run Wordpress functions at the edge?

I’m running Cloudflare Pro with APO.

Is it possible to run any Wordpress functions at the edge?

I’d like to be able to redirect certain user agents that land on posts with certain taxonomies.

My example:

/**
 * Redirect user-agent when using taxonomy redirect
 */
add_action( 'template_redirect', 'device_redirect' );

function device_redirect(){
if (has_term('redirect','manage') &&
     (strpos($_SERVER['HTTP_USER_AGENT'], "bot-1") !== false
     || strpos($_SERVER['HTTP_USER_AGENT'], "bot-2") !== false
     || strpos($_SERVER['HTTP_USER_AGENT'], "bot-3") !== false)) {
        wp_redirect( 'https://domain.com', 301);
    }
}

The above works without Cloudflare, I’d just love to be able to use it with Cloudflare.

Thanks for any insight!

Hi,

Assuming theses posts have /category/ or equivalent as part of their URL, you can accomplish this with a Redirect Rule:

When incoming requests match...
URL Path starts with "/category/"
AND
User Agent contains "bot-1"

OR

URL Path starts with "/category/"
AND
User Agent contains "bot-2"

OR
...etc


Then

Redirect:
Type: Static "https://example.com/"
Status: 301

You can also use () to group the conditions in the Expression Editor.

1 Like

Many moons ago I had a URL structure that would have allowed me this, but I simplified it for the better. Yet, it does remove the ability to do just this.

Maybe one day we can run functions at the edge…:grin: