WordPress

WordPress for Effective SNS Sharing! How to Retrieve Share Information Including Category Name and Article Title

Sharing information via blogs built with WordPress is now a common practice on many websites. Among these, making it easy for readers to share their favorite articles on social media is crucial for increasing blog traffic. While using plugins is one way, you may sometimes want to implement share buttons with more customization. This article clearly explains how to set up SNS share buttons in WordPress, even for beginners.

Setting Up SNS Share Buttons in WordPress

Retrieve the Article URL

First, retrieve the article’s URL. The URL is an essential element indicating the article’s location on the web. In WordPress, use the get_permalink() function and further encode it with urlencode() to make it suitable for SNS sharing.

<?php echo urlencode(get_permalink()); ?>

Retrieve the Article Title

Next, retrieve the article title. The title is an important keyword that represents the article’s content. Again, use the get_the_title() function with urlencode() to make it suitable for sharing.

<?php echo urlencode(get_the_title()); ?>

Retrieve the Category Name

The category to which the article belongs can also be important information when sharing. Use the get_the_category() function to retrieve the category data and encode it with urlencode().

<?php
$category = get_the_category();
$category[0]->cat_ID;	// Category ID
$category[0]->category_nicename;	// Category Slug

echo urlencode($category[0]->cat_name);	// URL encode category name
?>

Setting Share URLs for Each SNS

Facebook

For Facebook, it is common to share based on the article’s URL. You may also need to set up OGP (Open Graph Protocol) settings for Facebook sharing.
Below is an example using only the “Article URL”.

<a href="https://www.facebook.com/sharer/sharer.php?u=<?php echo urlencode(get_permalink()); ?>&amp;src=sdkpreparse" target="_blank">Facebook</a>

Twitter

On Twitter, you can include the category name and article title along with the article URL. This enriches the tweet content and can attract more attention.
The following example includes “Article URL” + “Category Name” + “|” + “Article Title”. The URL encoded character for “|” is “%7C”.

<a href="https://twitter.com/share?url=<?php echo urlencode(get_permalink()); ?>&text=<?php echo urlencode($category[0]->cat_name); ?>%7C<?php echo urlencode(get_the_title()); ?>" target="_blank">Twitter</a>

LINE

LINE sharing works similarly to Twitter, allowing you to combine the article URL, category name, and title. Since LINE is widely used for personal communication, sharing here is highly effective.
The following example includes “Article URL” + “Category Name” + “|” + “Article Title”. The URL encoded character for “|” is “%7C”.

<a href="http://line.me/R/msg/text/?<?php echo urlencode($category[0]->cat_name); ?>%7C<?php echo urlencode(get_permalink()); ?>" target="_blank">LINE</a>

Conclusion

Installing SNS share buttons is an essential feature for running a WordPress blog. Using the methods above, you can flexibly and effectively install share buttons without relying on plugins. Mastering this technique will help your blog reach more readers and gain greater influence.

This article introduced the basic method for setting up SNS share buttons in WordPress. By applying this method, you can also implement various other types of share buttons. It is easy to implement even for beginners, so feel free to give it a try.

 

*Please use at your own risk if repurposing.