Last active
December 22, 2017 09:12
-
-
Save evemilano/2e12647b488d3fe862b8326e1bad0d8a to your computer and use it in GitHub Desktop.
WordPress Plugin Dynamic Prerender Meta Tag
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: EVE Dynamic Prerender Meta Tag | |
Plugin URI: https://www.evemilano.com/2017/11/wp-prerender-plugin/ | |
Version: 3.5.3 | |
Description: This WordPress plugin creates and inject into HTML head a Dynamic Prerender Meta Tag. The system stores users navigational paths inside the database e retrieve the most common next visited page as prerender meta tag. If the plugin doesn't has data for the next probable page, it will show a prerender to the homepage. The database table WILL BE REMOVED automatically when the plugin is uninstalled but not when disabled. Do not remove the plugin if you want to keep data. The database table can grow up a lot in large websites, keep an eye on it! | |
Author: Giovanni Sacheli | |
Author URI: https://www.evemilano.com/ | |
*/ | |
function eve_prerender_create_table(){ | |
// do NOT forget this global | |
global $wpdb; | |
$table_name = $wpdb->prefix . "PRERENDER"; | |
// this if statement makes sure that the table doe not exist already | |
if ($wpdb->get_var('SHOW TABLES LIKE '.$table_name) != $table_name) | |
{ | |
$sql = "CREATE TABLE $table_name ( | |
ID INT(9) NOT NULL AUTO_INCREMENT, | |
canonical VARCHAR(255) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL, | |
prerender VARCHAR(255) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL, | |
count INT(9) NOT NULL, | |
PRIMARY KEY (ID), | |
UNIQUE canpre (canonical(255), prerender(255))) ENGINE=InnoDB;"; | |
require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); | |
dbDelta($sql); | |
} | |
} | |
// this hook will cause our creation function to run when the plugin is activated | |
register_activation_hook( __FILE__, 'eve_prerender_create_table' ); | |
// function save path | |
function eve_prerender() { | |
// se utente loggato non fare niente | |
if ( !is_user_logged_in() ) { | |
global $wpdb; | |
$table_name = $wpdb->prefix . "PRERENDER"; | |
// get referer | |
$referer = wp_get_referer(); | |
$relreferer = url_to_postid( $referer ); | |
//test | |
//echo 'rr: '.$relreferer.'<br/>'; | |
//get canonical | |
$can="{$_SERVER['REQUEST_URI']}"; | |
$canonical = url_to_postid( $can ); | |
//test | |
//echo 'c: '.$canonical; | |
//inserting data if... | |
if ( (!isset($canonical)=== false) AND (!isset($relreferer)=== false) AND ($canonical != $relreferer) AND ($canonical != 0) AND ($relreferer != 0) ) { | |
$wpdb->query("INSERT INTO $table_name (ID,canonical,prerender,count) VALUES (NULL,'$relreferer','$canonical','1') ON DUPLICATE KEY UPDATE count = count +1" ); | |
//test | |
//echo 'insert ok'; | |
} | |
//get prerender | |
$prerender = $wpdb->get_var($wpdb->prepare("SELECT prerender FROM $table_name WHERE canonical = '$canonical' ORDER BY count desc limit 1", $canonical )); | |
//test | |
//echo $prerender; | |
if ($prerender) { | |
$finale = get_permalink($prerender); | |
//if prerender exists | |
echo '<meta name="referrer" content="always"> | |
'; | |
echo ' <link rel="prerender" href="'.$finale.' "> | |
'; | |
//test | |
//echo 'pr: '.$finale; | |
}else{ | |
//if prerender does not exist | |
echo '<meta name="referrer" content="always"> | |
'; | |
echo ' <link rel="prerender" href="'.get_home_url().'/"> | |
'; | |
} | |
} | |
} | |
add_action('wp_head', 'eve_prerender' ); | |
// Delete table when uninstall | |
function remove_db() { | |
global $wpdb; | |
$table_name = $wpdb->prefix . "PRERENDER"; | |
$sql = "DROP TABLE IF EXISTS $table_name;"; | |
$wpdb->query($sql); | |
delete_option("my_plugin_db_version"); | |
} | |
register_uninstall_hook( __FILE__, 'remove_db' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Download the plugin: https://www.evemilano.com/2017/11/wp-prerender-plugin/
WordPress Plugin repository: https://wordpress.org/plugins/eve-dynamic-prerender/
The Prerender meta tag allows webmasters to hide some of the network latency through pre-loading the next page. As an AI system, this plugin saves all possible browsing pairs from page A on page B made by users. When it has data for the next page, it inserts in the HTML head the meta tag prerender, pointing to the page that most likely will be visited by the user.