the_permalink on the homepage vs posts for sharing links

Instead of weighing the site down with plugins I've hardcoded a couple of sharing links.

I've used the_permalink to create the links, in the sidebar, which work ok on posts and pages, but on the homepage it shows the link of the last post, not the homepage.

The code I'm using is pretty basic:

a href="http://www.facebook.com/sharer.php?u=?php the_permalink();?amp;t=?php the_title(); ?" target="_blank"Share this on Facebook/a

How can I get the homepage sharing link to use the homepage url, while also working for the posts and pages?

Topic sharing permalinks homepage Wordpress

Category Web


I would turn this into a function to account for all conditions. Take the following:

function generate_facebook_sharer_link( $label = 'Share this on Facebook', $classes = '', $return = false ){
    if( is_singular() ){
        // If is a Single CPT, Post, Page, etc.
        $url   = get_permalink();
        $title = get_the_title();
    } else if( is_front_page() ){
        // If this is the "Main Page"
        $url   = home_url();
        $title = get_bloginfo( 'title' );
    } else {
        // All other Accounts (Archives, Blog Page, etc.)
        $url   = ( is_ssl() ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
        $title = get_bloginfo( 'title' );
    }

    $output = sprintf( '<a class="%s" href="http://www.facebook.com/sharer.php?u=%s&t=%s" target="_blank" rel="noopener noreferrer">%s</a>', $classes, urlencode( $url ), urlencode( $title ), $label );

    // Allow returning, default to echoing
    if( $return == true )
        return $output;

    echo $output;
}

This will output a link wherever generate_facebook_sharer_link(); is called. The is_singular() should catch all Custom Post Types, Posts, Pages, etc. is_front_page() will catch if it's the "main page", regardless of what the Reading Settings/Blog Page are, and then it falls back to building the current URI with appropriate protocol and setting the blog title.

This could also very simply be turned into a shortcode:

add_shortcode( 'facebook_share_link', function( $atts ){
    // Extract and Define Defaults
    extract( shortcode_atts( array(
        'label'   => 'Share this on Facebook',
        'classes' => '',
    ), $atts ) );

    if( is_singular() ){
        // If is a Single CPT, Post, Page, etc.
        $url   = get_permalink();
        $title = get_the_title();
    } else if( is_front_page() ){
        // If this is the "Main Page"
        $url   = home_url();
        $title = get_bloginfo( 'title' );
    } else {
        // All other Accounts (Archives, Blog Page, etc.)
        $url   = ( is_ssl() ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
        $title = get_bloginfo( 'title' );
    }

    $output = sprintf( '<a class="%s" href="http://www.facebook.com/sharer.php?u=%s&t=%s" target="_blank" rel="noopener noreferrer">%s</a>', $classes, urlencode( $url ), urlencode( $title ), $label );

    return $output;
} );

Which would allow you to plop [facebook_share_link] wherever you'd like it to show up.


This code displays takes get_permalink() on a single page and get_home_url() on the homepage. If a link is found the Share-Link will be displayed.

So the Link wouldn't be displayed right now on a category-page for example.

<?php
    if( is_home() ){
        $link = urlencode( get_home_url() );
        $title = urlencode( get_bloginfo( 'title' ) );
    }elseif( is_single() || is_page() ){
        $link = urlencode( get_permalink() );
        $title = urlencode( get_the_title() );
    }

    if( isset( $link ) ):
?>
<a href="http://www.facebook.com/sharer.php?u=<?php 
    echo $link;?>&amp;t=<?php echo $title; 
?>" target="_blank">Share this on Facebook</a>
<?php
    endif;
?>

Info:

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.