Halloween Sale 15% off Ends in See the plans
PHP

Simple Share Buttons After Posts

Adds lightweight share links for X, Facebook, LinkedIn, WhatsApp and email at the end of each post, with no third-party scripts or tracking.

Don’t have WPIDE yet? Install it on your site

Code

add_filter( 'the_content', function ( $content ) {
    if ( ! is_singular( 'post' ) || ! in_the_loop() || ! is_main_query() ) {
        return $content;
    }

    $url   = rawurlencode( get_permalink() );
    $title = rawurlencode( wp_strip_all_tags( get_the_title() ) );

    // Network => share URL.
    $networks = array(
        'X'        => "https://x.com/intent/post?url={$url}&text={$title}",
        'Facebook' => "https://www.facebook.com/sharer/sharer.php?u={$url}",
        'LinkedIn' => "https://www.linkedin.com/sharing/share-offsite/?url={$url}",
        'WhatsApp' => "https://wa.me/?text={$title}%20{$url}",
        'Email'    => "mailto:?subject={$title}&body={$url}",
    );

    $links = '';

    foreach ( $networks as $name => $share ) {
        $links .= sprintf(
            '<a href="%1$s" %2$s style="display:inline-block;margin:0 8px 8px 0;padding:6px 14px;border:1px solid currentColor;border-radius:20px;font-size:14px;text-decoration:none;">%3$s</a>',
            esc_url( $share ),
            'Email' === $name ? '' : 'target="_blank" rel="noopener nofollow"',
            esc_html( $name )
        );
    }

    return $content . '<div class="wpide-share" style="margin-top:2em;"><p style="margin-bottom:.5em;font-weight:600;">Share this post</p>' . $links . '</div>';
}, 30 );

Notes

Edit $networks to remove networks you do not need. Uses plain links, so nothing loads until a visitor clicks.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.