Skip to content

Instantly share code, notes, and snippets.

@MillennialDIYer
Last active June 17, 2018 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MillennialDIYer/511cd033989c348046c352fb313400a2 to your computer and use it in GitHub Desktop.
Save MillennialDIYer/511cd033989c348046c352fb313400a2 to your computer and use it in GitHub Desktop.
Fallback Domain Redirect with UTM - In Progress
<?php
/*
Plugin Name: Fallback Domain Redirect with UTM
Plugin URI: N/A
Description: This plugin wildcard redirects to a fallback domain in case there isn't a match for your short URL.
This would allow the short domain to be used as an alternative for any existing URL of the fallack domain.
- Does not work installed in a subfolder
Version: 0.4
Author: MVS12
Author URI: https://xyzpolr.tk
*/
// Die if no direct call to stop external access to file
if( !defined( 'YOURLS_ABSPATH' ) ) die();
// Save URI present in yourls-loader.php
yourls_add_action( 'pre_load_template' , 'fdr_save_uri' );
function fdr_save_uri()
{
// Start session to conserve variable across pages
session_start();
$fdr_loader_uri = yourls_get_request();
$_SESSION['fdr_loader_uri'] = $fdr_loader_uri;
}
// Run fdr_fallback_dom function if the keyword is not found
yourls_add_action( 'redirect_keyword_not_found', 'fdr_fallback_dom' );
// Run fdr_fallback_dom function if the loader fails to load due to extraordinary characters
yourls_add_action( 'loader_failed', 'fdr_fallback_dom' );
// fdr_fallback_dom is the function that redirecs the
function fdr_fallback_dom()
{
// Check if the plugin is activce and if so, perform the redirect
if( yourls_is_active_plugin( 'fallback-domain-redirect-w-utm/plugin.php' ) )
{
// Start the session to retreive fdr_loader_uri variable later
session_start();
// Get values from the database
$fallback_dom = yourls_get_option( 'fallback_dom' );
$fdr_utm_source = yourls_get_option( 'fdr_utm_source' );
// Retreive fdr_loader_uri variable via the session
$mod_url = $_SESSION['fdr_loader_uri'];
// Remove existing UTM Parameters
$mod_url = preg_replace( '/&?utm_.+?(&|$)$/', '', $mod_url );
// Append the initial request plus UTM Parameter to the fallback domain
$fallback_dom_ext = $fallback_dom.$mod_url.$fdr_utm_source;
// Redirect to the fallback domain
yourls_redirect( $fallback_dom_ext, 307 ); //Use a temporal redirect in case there is a valid keyword in the future
}
}
// Register plugin config page
yourls_add_action( 'plugins_loaded', 'fdr_config_add_page' );
function fdr_config_add_page()
{
// parameters: page slug, page title, and function that will display the page itself
yourls_register_plugin_page( 'fallback_url_config', 'Fallback Domain Redirect with UTM Plugin Config', 'fdr_config_do_page' );
}
// Display Plugin Configuration Page
function fdr_config_do_page()
{
// Submit form
fdr_config_update_option();
// Get values from the database
$fallback_dom = yourls_get_option( 'fallback_dom' );
$fdr_utm_source = yourls_get_option( 'fdr_utm_source' );
// Remove "?utm_source=" from utm_source if applicable
$fdr_utm_source = str_replace("?utm_source=" , "", $fdr_utm_source);
echo <<<HTML
<h2>Fallback Domain Redirect with UTM Plugin Configuration</h2>
<hr>
<h3>Fallback Domain</h3>
<p>Here you can configure the Domain to wildcard-redirect to, in case the keyword is not found in database.</p>
<form method="post">
<p><label for="fallback_dom">Domain to wildcard redirect to</label> <input type="text" id="fallback_dom" name="fallback_dom" value="$fallback_dom" size="40" /></p>
<p><strong>Note</strong> - Please include protocol and closing slash. For example: <strong>https://</strong>example.com<strong>/</strong> </p>
<hr>
<h3>UTM Source Parameter - Optional</h3>
<p>The following UTM parameter's primary purpose is added information in <a href="https://support.google.com/analytics/answer/1033863#parameters">Google Analytics</a>. Its use is optional.</p>
<p>Keep in mind that UTM parameters are case-sensitive. Lowercase letters and hyphens for spaces are recommended.</p>
<p><label for="fdr_utm_source">utm_source=</label> <input type="text" id="fdr_utm_source" name="fdr_utm_source" value="$fdr_utm_source" size="40" /></p>
<hr>
<p><input type="submit" value="Update values" /></p>
</form>
HTML;
}
// Update fallback domain configuration in database
function fdr_config_update_option()
{
$in1 = $_POST['fallback_dom'];
if( $in1 )
{ // Performs validation of fallback domain.
// To assure the domain ends in a '/', it is stripped if applicable and added.
rtrim($in1,"/").'/';
// Gets string value of variable
$in1 = strval( $in1);
// Update value in database
yourls_update_option( 'fallback_dom', $in1 );
}
$in2 = $_POST['fdr_utm_source'];
if ( $in2 )
{
// Performs validation on the utm_source parameter.
// Gets string value of variable
$in2 = strval( $in2);
if ( $in2 != "")
{
// Adds utm_source prefix
$in2 = '?utm_source='.$in2;
// Update value in database
yourls_update_option( 'fdr_utm_source', $in2 );
}
elseif ( $in2 == "")
{
// Update value in database
yourls_update_option( 'fdr_utm_source', $in2 );
}
}
}
// PHP tag left unclosed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment