WordPress: creare uno shortcode per la condivisione social dei post e delle pagine

WordPress: creare uno shortcode per la condivisione social dei post e delle pagine

In WordPress è semplice creare uno shortcode per la condivisione social.

La soluzione è la seguente:


<?php
function my_social() {
  global $post;
  $id = $post->ID;
  $link = get_permalink( $id );
  $title = get_the_title( $id );
  $image = wp_get_attachment_image_src( get_post_thumbnail_id( $id ), 'full' );

    $html = '<div class="social-icons">';

    $icons = array(
        array(
          'icon' => 'fa fa-phone',
          'url' => 'whatsapp://send?text=' . urlencode( $title ) .'-' . urlencode( $link )
        ),
        array(
          'icon' => 'fa fa-facebook',
          'url' => 'http://www.facebook.com/sharer.php?u=' . $link
        ),
        array(
          'icon' => 'fa fa-twitter',
          'url' => 'https://twitter.com/share?url=' . $link
        ),
        array(
          'icon' => 'fa fa-envelope-o',
          'url' => 'mailto:enteryour@addresshere.com?subject=' . urlencode( $title ) . '&amp;body=Check%20this%20out:%20' . $link
        ),
        array(
          'icon' => 'fa fa-pinterest',
          'url' => '//pinterest.com/pin/create/button/?url=' . $link . '&amp;media=' . $image[0] . '&amp;description=' . urlencode( $title )
        ),
        array(
          'icon' => 'fa fa-google-plus',
          'url' => '//plus.google.com/share?url=' . $link
        ),
        array(
          'icon' => 'fa fa-tumblr',
          'url' => '//tumblr.com/widgets/share/tool?canonicalUrl='
        )
    );

    foreach( $icons as $icon ) {
        $html .= '<a href="' . $icon['url'] . '" target="_blank"><span class="' . $icon['icon'] . '"></span></a>';
    }

    $html .= '</div>';
    return $html;
}

add_shortcode( 'my-social', 'my_social' );
?>

Torna su