Magazine Consigli Utili

Arriva Humans.txt per WordPress

Da Iwebdesigner @Iwebdesigner_it

arriva-humanstxt-per-wordpress

Ogni volta che guardate un sito, vi siete mai chiesti chi l’ha fatto? In che linguaggio l’ha sviluppato? Ci sono molti metodi per dare una risposta a queste domande, soprattutto con strumenti di visualizzazione codice. Ma tutto questo da oggi e’ vecchio, infatti e’ stato introdotta una soluzione semplice e legale per vedere la paternita’ di un sito: HUMANS.TXT.

E’ un’iniziativa di un team spagnolo per indicare chi ha costruito il sito. L’idea e’ semplice: includere in un file tutte le informazioni dell’autore e degli strumenti usati per la progettazione. Questo file nominato humans.txt sara’ messo nella root del sito proprio vicino al robots.txt, visualizzabile digitando l’indirizzo:

http://www.tuosito.com/humans.txt

Come e’ nata l’esigenza di creare un file di questo tipo? Perche’ spesso evolentieri, soprattutto nelle grandi aziende, non e’ sempre possibile lasciare la propria impronta sul lavoro svolto. Con questo articolo mostriamo il medoto per costruire il file humans.txt da inserire in tutti i vostri lavori.

Il codice

Vediamo i passi che bisogna affrontare per la costruzione del file.

-  generare il contenuto di humans.txt

- rendere il vostro CMS (WP) in grado di rendere accessibile il file humans.txt

- includere un link nel Head del sito a humans.txt

Vediamo il file:

/* Generate humans.txt content */
function do_humans() {

//serve correct headers
header( 'Content-Type: text/plain; charset=utf-8' );

//let some other plugins do something here
do_action( 'do_humanstxt' );

//prepare default content
$content = "
/* TEAM */

Developer: John Doe
Contact: hello [at] johndoe.com
Twitter: @johndoe

/* THANKS */

Indispensable contributor: John's mom
Twitter: @johnsmom

/* SITE */
Language: English
Standards: HTML5, CSS3
CMS: WordPress
";
//make it filterable
$content = apply_filters('humans_txt', $content);

//correct line ends
$content = str_replace("\r\n", "\n", $content);
$content = str_replace("\r", "\n", $content);

//output
echo $content;
}

/* Link to humans.txt for head section of site */
function frl_humanstxt_link(){

$url = esc_url(home_url('humans.txt'));
echo "";
}

/* Print humans.txt button somewhere on the site */
function frl_humanstxt_button(){

//make sure that this is correct path to button image !!!
$src = esc_url(trailingslashit(plugins_url('', __FILE__)).'humanstxt-isolated-orange.gif');
$url = esc_url(home_url('humans.txt'));
echo "
";
}

/* Make WP understand humans.txt url */
function frl_humanstxt_init(){
global $wp_rewrite, $wp;

//root install check
$homeurl = parse_url(home_url());
if (isset($homeurl['path']) & !empty($homeurl['path']) & $homeurl['path'] != '/')
return;

//check for pretty permalinks
$permalink_test = get_option('permalink_structure');
if(empty($permalink_test))
return;

//register rewrite rule for humans.txt request
add_rewrite_rule('humans\.txt$', $wp_rewrite->index.'?humans=1', 'top');

// flush rewrite rules if 'humans' one is missing
$rewrite_rules = get_option('rewrite_rules');
if (!isset($rewrite_rules['humans\.txt$']))
flush_rewrite_rules(false);

//add 'humans' query variable to WP
$wp->add_query_var('humans');

// display links to humans txt (when it's properly registered)
if(function_exists('frl_humanstxt_link'))
add_action('wp_head', 'frl_humanstxt_link');

if(function_exists('frl_humanstxt_button'))
add_action('wp_footer', 'frl_humanstxt_button');
}
add_action('init', 'frl_humanstxt_init');

/* Conditional tag to check for humans.txt request */
function is_humans(){

if( 1 == get_query_var('humans'))
return true;

return false;
}

/* Load dynamic content instead or regular WP template */
function frl_humanstxt_load(){
if(is_humans()){
do_humans();
die();
}
}
add_action('template_redirect', 'frl_humanstxt_load');

Andiamo ad analizzare cosa e’ contenuto in humans.txt e che informazioni deve contenere:

function do_humans() {

//serve correct headers
header( 'Content-Type: text/plain; charset=utf-8' );

//let some other plugins do something here
do_action( 'do_humanstxt' );

//prepare default content
$content = "
/* TEAM */

Developer: John Doe
Contact: hello [at] johndoe.com
Twitter: @johndoe

/* THANKS */

Indispensable contributor: John's mom
Twitter: @johnsmom

/* SITE */
Language: English
Standards: HTML5, CSS3
CMS: WordPress
";

//make it filterable
$content = apply_filters('humans_txt', $content);

//correct line ends
$content = str_replace("\r\n", "\n", $content);
$content = str_replace("\r", "\n", $content);

//output
echo $content;
}

Poi vengono definiti due tag per la visualizzazione e la stampa nel nostro sito del file humans.txt:

/* TEAM */
Your title: Your name.
Site: email, link to a contact form, etc.
Twitter: your Twitter username.
Location: City, Country.
[...]

/* THANKS */
Name: name or url
[...]

/* SITE */
Last update: YYYY/MM/DD
Standards: HTML5, CSS3,..
Components: Modernizr, jQuery, etc.
Software: Software used for the development

Humanstxt.org ci fornisce vari elementi grafici da utilizzare.

Andiamo avanti; la funzione frl_humanstxt_init registra l’url humans.txt su WordPress:

function frl_humanstxt_link(){

$url = esc_url(home_url('humans.txt'));
echo "";
}

function frl_humanstxt_button(){

//make sure that this is correct path to button image !!!
$src = esc_url(trailingslashit(plugins_url('', __FILE__)).'humanstxt-isolated-orange.gif');
$url = esc_url(home_url('humans.txt'));
echo "
y sul database.
/root install check
$homeurl = parse_url(home_url());
if (isset($homeurl['path']) & !empty($homeurl['path']) & $homeurl['path'] != '/')
return;

//check for pretty permalinks
$permalink_test = get_option('permalink_structure');
if(empty($permalink_test))
return;
//register rewrite rule for humans.txt request
add_rewrite_rule('humans\.txt$', $wp_rewrite->index.'?humans=1', 'top');
// flush rewrite rules if 'humans' one is missing
$rewrite_rules = get_option('rewrite_rules');
if (!isset($rewrite_rules['humans\.txt$']))
flush_rewrite_rules(false);
//add 'humans' query variable to WP
$wp->add_query_var('humans');

Adesso la funzione per i link al file humans.txt sia nell’header che nel footer:

// display links to humans txt (when it's properly registered)
if(function_exists('frl_humanstxt_link'))
add_action('wp_head', 'frl_humanstxt_link');

if(function_exists('frl_humanstxt_button'))
add_action('wp_footer', 'frl_humanstxt_button');

Questa e’ la funzione che fa caricare il file humans.txt all’interno del nostro layout grafico.

function is_humans(){
if( 1 == get_query_var('humans'))
return true;

return false;
}
function frl_humanstxt_load(){
if(is_humans()){
do_humans();
die();
}
}
add_action('template_redirect', 'frl_humanstxt_load');

Questo e’ tutto. Abbiamo dimostrato la potenza delle Api di WordPress, abbiamo visto le azioni per richiamare un file txt e il metodo di rewrite URL per un file completamente esterno al sistema. Credo che l’iniziativa di humanstxt.org sia ammirevole, li ringraziamo e spero che vi sia utile da integrare nei vostri progetti.


Potrebbero interessarti anche :

Ritornare alla prima pagina di Logo Paperblog

Possono interessarti anche questi articoli :