Theme & Front End PHP Intermediate

Add Share Links Without Tracking Scripts

Add plain share links for X, LinkedIn, Facebook and email below each post, with no third-party scripts or cookies.

Tested on WordPress 7.1 PHP 8.3 No plugin needed Updated
functions.php 23 lines
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;
} );

Where to add this

Recommended A code snippets plugin Survives theme changes and is easy to switch off.
Child theme At the end of your child theme's functions.php file.
Must-use plugin As a .php file in wp-content/mu-plugins, starting with <?php.

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.

How it works

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.

Things to watch

  • Style .nahnu-share in your CSS, or add icons.
  • For the best previews when shared, make sure your posts have Open Graph tags.

How to test it

Open a post and click the LinkedIn link. A LinkedIn share window should open with your post’s URL.

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only