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.
Opens WPIDE on
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
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.