WordPress.org

Making WordPress.org

Ticket #3946: 3946.diff

File 3946.diff, 1.1 KB (added by dd32, 3 years ago)

Something that will probably work, but needs some extra conditionals put on it, etc. redirect_canonical() doesn't have any useful filters for this scenario.

  • mu-plugins/pub/wporg-canonical-tweaks.php

     
     1<?php
     2
     3/**
     4 * Custom Canonical redirect for Facebook and Twitter referrers.
     5*/
     6add_action( 'template_redirect', function() {
     7
     8        // Only run on pages with canonical enabled.
     9        if ( ! has_action( 'template_redirect', 'redirect_canonical' ) ) {
     10                return;
     11        }
     12
     13        $url = false;
     14        if ( isset( $_GET['fbclid'] ) ) {
     15                $url = remove_query_arg( 'fbclid' ) . '#utm_medium=referral&utm_source=facebook.com&utm_content=social';
     16        } elseif ( isset( $_GET['__twitter_impression'] ) ) {
     17                $url = remove_query_arg( '__twitter_impression' ) . '#utm_medium=referral&utm_source=twitter.com&utm_content=social';
     18        }
     19
     20        if ( $url ) {
     21                wp_redirect( $url, 301 );
     22                exit;
     23        }
     24
     25}, 11 ); // After redirect_canonical();