Show Social Links For Users By ACF Plugin

Quick Summary

  • Print the post author's Facebook and Twitter URLs from ACF user fields named u_facebook and u_twitter, inside the loop of single.php.
  • Set the field group location to User Form, and pass user_ plus the author ID from get_the_author_meta() as the_field()'s second argument.
  • Change the code's User_ prefix to lowercase user_ if fields come back empty, and use get_field() with esc_url() for clickable links.

Advanced Custom Fields (ACF) can add extra fields to WordPress user profiles, such as social media URLs. This snippet prints the Facebook and Twitter URLs saved on the profile of the current post’s author, for example in an author box below the post.

First create an ACF field group with two URL (or Text) fields named u_facebook and u_twitter, and set its location rule to User Form so the fields appear on the user profile screen. Then add the code to your single post template, such as single.php, inside the loop.

PHP
<?php if( function_exists( "the_field" ) ): ?>
<p>
   Facebook Url: <?php the_field( "u_facebook","User_".get_the_author_meta( "ID" ) ); ?><br/>
   Twitter Url: <?php the_field( "u_twitter","User_".get_the_author_meta( "ID" ) ); ?><br/>
</p>
<?php endif; ?>

How it works

  • function_exists( "the_field" ) skips the block when ACF isn’t active, so the page doesn’t break (more on testing whether ACF is available).
  • get_the_author_meta( "ID" ) returns the ID of the current post’s author. It relies on the loop’s post data, so use it inside the loop.
  • The second argument of the_field() tells ACF where to read the value from. For a user, that’s the prefix user_ followed by the user ID. ACF’s documentation writes the prefix in lowercase, so change "User_" to "user_" if the fields come back empty.

The URLs are printed as plain text. To make them clickable, read the values with get_field() instead and output each one inside a link, with the URL passed through esc_url(), as explained in escaping URLs and attributes in WordPress. For more networks, create more fields and repeat the line with their names.

Share on X (opens in a new tab) Share on LinkedIn (opens in a new tab) Share on Facebook (opens in a new tab)

ChatGPT (opens in a new tab) Claude (opens in a new tab) Gemini (opens in a new tab) Perplexity (opens in a new tab) Grok (opens in a new tab)

More Insights

See More