Add plain share links for X, LinkedIn, Facebook and email below each post, with no third-party scripts or cookies.
add_filter( 'the_content', function ( string $content ): string {
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() ) );
$links = [
'X' => "https://x.com/intent/post?url={$url}&text={$title}",
'LinkedIn' => "https://www.linkedin.com/sharing/share-offsite/?url={$url}",
'Facebook' => "https://www.facebook.com/sharer/sharer.php?u={$url}",
'Email' => "mailto:?subject={$title}&body={$url}",
];
$html = '<nav class="nahnu-share" aria-label="Share this post"><strong>Share:</strong>';
foreach ( $links as $name => $href ) {
$html .= sprintf( ' <a href="%s" target="_blank" rel="noopener noreferrer">%s</a>', esc_url( $href ), esc_html( $name ) );
}
$html .= '</nav>';
return $content . $html;
} );
Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.
Social sharing plugins often load scripts from each network, which slows pages and sets tracking cookies. Plain share links do the same job with none of that.
Each network has a share URL that takes your page address and title as parameters. The snippet builds those URLs for the current post, encodes them safely and adds a small “Share:” list after the content of single posts.
Nothing loads from the social networks until a reader clicks, so there are no cookies, no consent needed and no slowdown.
.nahnu-share in your CSS, or add icons.Open a post and click the LinkedIn link. A LinkedIn share window should open with your post’s URL.