In questo articolo ho voluto racchiudere sei funzioni fondamentali a livello SEO da aggiungere ai nostri temi di WordPress tramite le action del CMS. Potete aggiungerle direttamente al vostro file functions.php
. Spero che le troviate utili.
Le sei funzioni sono le seguenti:
add_action( 'wp_head', 'seo_meta_robots', 1 );
add_action( 'wp_head', 'seo_meta_author', 1 );
add_action( 'wp_head', 'seo_meta_copyright', 1 );
add_action( 'wp_head', 'seo_meta_revised', 1 );
add_action( 'wp_head', 'seo_meta_description', 1 );
add_action( 'wp_head', 'seo_meta_keywords', 1 );
function seo_meta_robots() {
if ( !get_option( 'blog_public' ) ) {
return;
}
$robots = '<meta name="robots" content="index,follow" />' . "\n";
echo apply_atomic( 'meta_robots', $robots );
}
function seo_meta_author() {
$author = '';
$object = get_queried_object();
if ( is_singular() ) {
$author = get_the_author_meta( 'display_name', $object->post_author );
} elseif ( is_author() ) {
$author = get_the_author_meta( 'display_name', get_queried_object_id() );
}
if ( !empty( $author ) ) {
$author = '<meta name="author" content="' . esc_attr( $author ) . '" />' . "\n";
}
echo apply_atomic( 'meta_author', $author );
}
function seo_meta_copyright() {
if ( is_singular() ) {
$date = get_the_time( esc_attr__( 'F Y') );
} else {
$date = date( esc_attr__( 'Y', $domain ) );
}
$copyright = '<meta name="copyright" content="' . sprintf( esc_attr__( 'Copyright (c) %1$s'), $date ) . '" />' . "\n";
echo apply_atomic( 'meta_copyright', $copyright );
}
function seo_meta_revised() {
$revised = '';
if ( is_singular() ) {
$revised = '<meta name="revised" content="' . get_the_modified_time( esc_attr__( 'l, F jS, Y, g:i a') ) . '" />' . "\n";
}
echo apply_atomic( 'meta_revised', $revised );
}
function seo_meta_description() {
$description = '';
if ( is_home() ) {
$description = get_bloginfo( 'description' );
} elseif ( is_singular() ) {
$description = get_post_meta( get_queried_object_id(), 'Description', true );
if ( empty( $description ) && is_front_page() )
$description = get_bloginfo( 'description' );
elseif ( empty( $description ) )
$description = get_post_field( 'post_excerpt', get_queried_object_id() );
}
elseif ( is_archive() ) {
if ( is_author() ) {
$description = get_user_meta( get_query_var( 'author' ), 'Description', true );
if ( empty( $description ) )
$description = get_the_author_meta( 'description', get_query_var( 'author' ) );
}
elseif ( is_category() || is_tag() || is_tax() )
$description = term_description( '', get_query_var( 'taxonomy' ) );
elseif ( is_post_type_archive() ) {
$post_type = get_post_type_object( get_query_var( 'post_type' ) );
if ( isset( $post_type->description ) )
$description = $post_type->description;
}
}
if ( !empty( $description ) )
$description = '<meta name="description" content="' . str_replace( array( "\r", "\n", "\t" ), '', esc_attr( strip_tags( $description ) ) ) . '" />' . "\n";
echo apply_atomic( 'meta_description', $description );
}
function seo_meta_keywords() {
$keywords = '';
if ( is_singular() && !is_preview() ) {
$post = get_queried_object();
$keywords = get_post_meta( get_queried_object_id(), 'Keywords', true );
if ( empty( $keywords ) ) {
$taxonomies = get_object_taxonomies( $post->post_type );
if ( is_array( $taxonomies ) ) {
foreach ( $taxonomies as $tax ) {
if ( $terms = get_the_term_list( get_queried_object_id(), $tax, '', ', ', '' ) )
$keywords[] = $terms;
}
if ( !empty( $keywords ) )
$keywords = join( ', ', $keywords );
}
}
}
elseif ( is_author() ) {
$keywords = get_user_meta( get_query_var( 'author' ), 'Keywords', true );
}
if ( !empty( $keywords ) )
$keywords = '<meta name="keywords" content="' . esc_attr( strip_tags( $keywords ) ) . '" />' . "\n";
echo apply_atomic( 'meta_keywords', $keywords );
}