How to Add Internal Links to Your WordPress Content for Better Navigation

Home » Snippets » How to Add Internal Links to Your WordPress Content for Better Navigation
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Internal links are a crucial aspect of website structure and optimization. In WordPress, adding internal links is simple, but it can make a significant difference in promoting your content and helping your visitors navigate your site. In this article, we will guide you through the process of adding internal links to your WordPress site and explain why they are essential for your website’s success.

					function wpturbo_add_internal_link($text) {
    $url = get_permalink();
    $link_text = "Read more on " . get_the_title();
    $link = '<a href="' . esc_url($url) . '">' . esc_html($link_text) . '</a>';
    $text .= ' ' . $link;
    return $text;
}
add_filter('the_content', 'wpturbo_add_internal_link');
				

The code snippet here showcases how to add an internal link on a WordPress site that takes the user to another page within the website. It employs the use of a WordPress filter hook, the_content, which allows you to modify the content before it is displayed on the page.

The wpturbo_add_internal_link() function creates the internal link to be appended to the end of each post on the website. Inside the function, we use the get_permalink() function to retrieve the URL of the current post, which we will use as the destination URL for our internal link.

We then create a textual description for the internal link by concatenating the string "Read more on" and the post title, which we grab with the get_the_title() function. We assign this text to the $link_text variable.

The next step is to create the link HTML by concatenating the $url and $link_text variables. We use the esc_url() function to sanitize the URL and the esc_html() function to sanitize the link text, which helps to prevent cross-site scripting (XSS) attacks.

Finally, we append the link to the end of the post content by concatenating the $text and $link variables with a space in between. Doing so ensures that the internal link is added to the end of every post.

The last step is to hook the wpturbo_add_internal_link() function into the the_content filter using the add_filter() function. This ensures that the function is executed on every post before it is displayed to the user.

Register an account to save your snippets or go Pro to get more features.