| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Custom Canonical redirect for Facebook and Twitter referrers. |
| 5 | */ |
| 6 | add_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(); |