0

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?

0

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:

  • OK so the_permalink doesn't work for the blog index. Would it be possible to simplify it further, replacing 'elseif(....)' with just 'else()' and removing the 'endif'? Sorry for my ignorance! – Alex_TJ Mar 29 '15 at 15:17
  • get_permalink() works only for posts and pages, this is why i used elseif. – websupporter Mar 29 '15 at 15:22
  • There seems to be an error somewhere 'Parse error: syntax error, unexpected end of file' @websupporter – Alex_TJ Mar 29 '15 at 15:38
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.