Skip to content

Instantly share code, notes, and snippets.

@salvatorecapolupo
Created July 14, 2017 09:49
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 salvatorecapolupo/6e7487e26dbb3080e7b6ce40dc4f6cfb to your computer and use it in GitHub Desktop.
Save salvatorecapolupo/6e7487e26dbb3080e7b6ce40dc4f6cfb to your computer and use it in GitHub Desktop.
Put this file on your mu-plugins WordPress directory, and map your custom URL, title, meta-description as a CSV file. Used i.e http://rubbettinoprint.it/
<?php
/**
*
* Plugin Name: Universal SEO Plugin
* Description: Set and overwrite title/meta description via url, i.e mapped in a .txt file
* Author: Salvatore Capolupo
* Version: 1.0
* Author URI: http://www.rubbettinoprint.it
*/
/**
seodata.txt example
----
# this is a comment, please use ; as a separator
# format: URL;title;description;
# home
/;Home title;Home description;
#...
#...
----
**/
class SEOUtility{
var $data;
var $csv_sep = ";";
var $sep = " * ";
var $nodata = "-";
function init(){
$basepath = plugin_dir_path( __FILE__ );
$file = fopen( $basepath . "seodata.txt", "r");//or exit("seodata.txt non presente o non leggibile.");
while( !feof($file) ){
$riga = fgets($file);
if ( !preg_match("/\#/", $riga) ){
$riga = explode( $this->csv_sep, $riga );
$this->data[ $riga[0] ] = $riga[1] . $this->sep .$riga[2];
}
}
fclose($file);
}
public function get_current_url( ){
return "$_SERVER[REQUEST_URI]";
}
public function was_set_data_for( $url ){
return array_key_exists( $url, $this->data);
}
public function get_title_for( $url ){
if ( !array_key_exists( $url, $this->data) ) return $this->nodata;
$tmp = $this->data[$url];
$riga = explode( $this->sep, $tmp);
return $riga[0];
}
public function get_description_for( $url ){
if ( !array_key_exists( $url, $this->data) ) return $this->nodata;
$tmp = $this->data[$url];
$riga = explode( $this->sep, $tmp);
return $riga[1];
}
}
//abilita supporto ai title, se non presente
function theme_slug_setup() {
add_theme_support( 'title-tag' );
}
add_action( 'after_setup_theme', 'theme_slug_setup' );
//title rewrite
add_filter('document_title_parts', 'rp_override_post_title', 1);
function rp_override_post_title($title){
$SEOobj = new SEOUtility();
$SEOobj -> init();
if ( $SEOobj->was_set_data_for( $SEOobj->get_current_url() ) )
$title['title'] = $SEOobj->get_title_for( $SEOobj->get_current_url() ) . " - ". get_bloginfo('name');
return $title;
}
// add meta-description
add_action( 'wp_head', 'cyb_author_archive_meta_desc' );
function cyb_author_archive_meta_desc() {
$SEOobj = new SEOUtilityCapolupo();
$SEOobj -> init();
if ( $SEOobj->was_set_data_for( $SEOobj->get_current_url() ) )
echo '<meta name="description" content="' . $SEOobj->get_description_for( $SEOobj->get_current_url() ) . '">';
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment