Hashtags from Tags
27 Giugno 2021 | Categoria: Codice, Blog |
Il suo nome è già più che esplicativo ma cerchiamo di capire meglio cosa fa. Una volta attivato cercherà una corrispondenza tra i tags dell’articolo del vostro sito WordPress. Tutte le parole corrispondenti verranno modificate aggiungendo un # prima, per creare un hashtag simil Instagram, e un link all’archivio di quel tag.
Hashtags from Tags è un piccolo e semplicissimo plugin per WordPress
Potete vedere Hashtags from Tags su questo sito.
Vediamo il codice
- Il primo passo è aggiungere il commento richiesto come indicato da WordPress
/**
* Plugin Name: Hashtags from Tags
* Plugin URI: https://www.maiowebdesign.it/hashtags-from-tags
* Description: Find the tags in the content of an article and transform them in ashtags with anchor tag to the tag archive.
* Version: 1.0
* Requires at least: 5.2
* Requires PHP: 7.2
* Author: Matteo Barbero -Maio Web Design
* Author URI: https://www.maiowebdesign.it/
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: mwd-hahstag
* Domain Path: /languages
*/
2. Mettiamo in sicurezza il plugin. ABSPATH è una costante di WordPress, definita nel suo core, e se questa dovesse mancare, il che vorrebbe dire che non ci troviamo all’interno del CMS, viene richiamata la funzione die().
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
3. Finalmente arriviamo al cuore del plugin: vengono ricercati i tags all’interno del test e per ognuno viene creato un hashtag. Per farlo usa semplicemente la funzione str_replace().
function tag_in_text($content){
// If post has tags modify its content
if ( $tags ){
$content_edited= $content;
foreach($tags as $tag){
// Add hashtags and anchor
$content_edited = str_replace(' '.$tag->name.' ', ' <a href="'. get_tag_link($tag).'">#'.$tag->name.'</a> ', $content_edited);
}
return $content_edited;
}
}
4. Abilitiamo il plugin solo nei post con la funzione di WordPress is_single()
function tag_in_text($content){
if (is_single() && !is_page() && !is_singular( $post_types = 'portfolio' )){
// Get all the tags
$tags = get_the_tags();
// If post has tags modify its content
if ( $tags ){
$content_edited= $content;
foreach($tags as $tag){
// Add hashtags and anchor
$content_edited = str_replace(' '.$tag->name.' ', ' <a href="'. get_tag_link($tag).'">#'.$tag->name.'</a> ', $content_edited);
}
return $content_edited;
}
}else{
return $content;
}
}
5. Ecco finalmente il codice del nostro plugin finito
<?php
/**
* Plugin Name: Hashtags from Tags
* Plugin URI: https://www.maiowebdesign.it/hashtags-from-tags
* Description: Find the tags in the content of an article and transform them in ashtags with anchor tag to the tag archive.
* Version: 1.0
* Requires at least: 5.2
* Requires PHP: 7.2
* Author: Matteo Barbero -Maio Web Design
* Author URI: https://www.maiowebdesign.it/
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: mwd-hahstag
* Domain Path: /languages
*/
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
function tag_in_text($content){
if (is_single() && !is_page() && !is_singular( $post_types = 'portfolio' )){
// Get all the tags
$tags = get_the_tags();
// If post has tags modify its content
if ( $tags ){
$content_edited= $content;
foreach($tags as $tag){
// Add hashtags and anchor
$content_edited = str_replace(' '.$tag->name.' ', ' <a href="'. get_tag_link($tag).'">#'.$tag->name.'</a> ', $content_edited);
}
return $content_edited;
}
}else{
return $content;
}
}
add_filter('the_content', 'tag_in_text');
?>