Social Profiles Widget
Adds a "Social Profiles" widget with links to your profiles on Facebook, Instagram, X, LinkedIn, YouTube, TikTok, Bluesky, Mastodon and more, as an accessible list of text links.
Opens WPIDE on
Code
if ( ! class_exists( 'WPIDE_Social_Profiles_Widget' ) ) {
class WPIDE_Social_Profiles_Widget extends WP_Widget {
// Networks offered in the widget form, in display order.
const NETWORKS = array(
'facebook' => 'Facebook',
'instagram' => 'Instagram',
'x' => 'X',
'linkedin' => 'LinkedIn',
'youtube' => 'YouTube',
'tiktok' => 'TikTok',
'pinterest' => 'Pinterest',
'bluesky' => 'Bluesky',
'mastodon' => 'Mastodon',
'threads' => 'Threads',
'github' => 'GitHub',
);
public function __construct() {
parent::__construct( 'wpide_social_profiles', 'Social Profiles', array(
'description' => 'Links to your profiles on social networks.',
'customize_selective_refresh' => true,
) );
}
public function widget( $args, $instance ) {
$links = array();
foreach ( self::NETWORKS as $key => $label ) {
if ( ! empty( $instance[ $key ] ) ) {
// rel="me" lets Mastodon and similar services verify the link back to your site.
$links[] = '<li><a href="' . esc_url( $instance[ $key ] ) . '" rel="me noopener" target="_blank">' . esc_html( $label ) . '<span class="screen-reader-text"> (opens in a new tab)</span></a></li>';
}
}
if ( ! $links ) {
return;
}
$title = isset( $instance['title'] ) ? $instance['title'] : 'Follow Us';
$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput -- theme markup.
if ( '' !== (string) $title ) {
echo $args['before_title'] . esc_html( $title ) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput
}
echo '<ul class="wpide-social-profiles" style="display:flex;flex-wrap:wrap;gap:.5em;list-style:none;margin:0;padding:0;">' . implode( '', $links ) . '</ul>'; // phpcs:ignore WordPress.Security.EscapeOutput -- escaped above.
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput
}
public function form( $instance ) {
$title = isset( $instance['title'] ) ? $instance['title'] : 'Follow Us';
echo '<p><label for="' . esc_attr( $this->get_field_id( 'title' ) ) . '">Title:</label><input class="widefat" type="text" id="' . esc_attr( $this->get_field_id( 'title' ) ) . '" name="' . esc_attr( $this->get_field_name( 'title' ) ) . '" value="' . esc_attr( $title ) . '" /></p>';
foreach ( self::NETWORKS as $key => $label ) {
echo '<p><label for="' . esc_attr( $this->get_field_id( $key ) ) . '">' . esc_html( $label ) . ' URL:</label><input class="widefat" type="url" id="' . esc_attr( $this->get_field_id( $key ) ) . '" name="' . esc_attr( $this->get_field_name( $key ) ) . '" value="' . esc_attr( isset( $instance[ $key ] ) ? $instance[ $key ] : '' ) . '" /></p>';
}
return '';
}
public function update( $new_instance, $old_instance ) {
$clean = array( 'title' => isset( $new_instance['title'] ) ? sanitize_text_field( $new_instance['title'] ) : '' );
foreach ( array_keys( self::NETWORKS ) as $key ) {
$clean[ $key ] = isset( $new_instance[ $key ] ) ? esc_url_raw( trim( $new_instance[ $key ] ), array( 'https', 'http' ) ) : '';
}
return $clean;
}
}
}
add_action( 'widgets_init', function () {
register_widget( 'WPIDE_Social_Profiles_Widget' );
} );
Notes
Leave a network empty to hide it; links carry rel="me" so Mastodon can verify your site.
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.