Wordpress link manager with advanced custom fields

WordPress Link Manager with Advanced Custom Fields

Screenshot of the add new plugin screen for advanced custom fields

Creating a WordPress link manager is an easy way to organize a collection of links and is crucial for ensuring a smooth user experience. For WordPress users looking to take control of their site’s links and streamline the management process, creating a custom WordPress Link Manager using Advanced Custom Fields (ACF) and integrating redirects for Custom Post Types (CPT) offers a powerful solution. In this article, we’ll guide you through the process of setting up a Link Manager in WordPress, complete with ACF fields for easy editing and redirects for seamless navigation.

Install and activate the Advanced Custom Fields plugin on your WordPress site if you haven’t already. Once you have installed and activated the plugin, refresh your WordPress Dashboard to see a new ACF menu.

Creating a Custom Post Type for our WordPress Link Manager

Wordpress link manager with advanced custom fields

To manage links within our WordPress Links Manager, we need to create a way to store them in the database. In this tutorial, we will use the free plugin Advanced Custom Fields to create a Custom Post Type to take advantage of the WordPress data structure.

By default, WordPress already has two commonly used Post Types, Posts and Pages. This plugin allows us to piggyback off those to create a new Custom Post Type to store our Links. To accomplish this, we need to do the following:

  1. Within the ACF menu, select Post Type, then click Add New;
  2. Set the Plural Label to ‘Links’;
  3. Set the Singular Label to ‘Link’;
  4. Set the Post Type Key to ‘links’;

Once this is done, click Save Changes, and a new menu item named Links will appear on your WordPress Dashboard. If you click Links->Add New Link, you’ll see your Custom Post Type is now live and has a place for a title and excerpt but doesn’t have a specific place for the link itself. We’ll add that next.

Wordpress link manager - adding a custom field

A URL is a Uniform Resource Locator, just the fancy name for a website address. To make our WordPress Link Manager work properly, we must add a special field to the Links CPT we created in this next step.

  1. Within the ACF, create a new Custom Field Group named “Links”.
  2. Edit the new Custom Field Group and add a new Field labelled Link URL, be sure to set the field type to be a URL and verify the Field Label is “link_url” as we’ll need to use it later.
  3. Finally, assign this field group to the desired post type. We want to use the Custom Post Type we’ve already created called “Links”.

Now, when you’ve saved this Custom Field and assigned it to the Links CPT, you can return to the Links->Add New Link page and see a new field labelled Link URL that will allow you to include a website address in the box.

Wordpress link manager - testing the custom link
Wordpress link manager with advanced custom fields

The platform automatically creates a Permalink when you save a new link on your WordPress website. This Permalink serves as the unique address or URL that users can visit to access the content associated with that link. The WordPress Link Manager is a tool that helps you organize and manage all the links on your website, making it easier to keep track of them and ensure they are working correctly.

In addition to providing a convenient way to organize your links, the WordPress Link Manager also offers the benefit of automatically redirecting users to the final URL associated with each link. This means that if you ever need to change the destination of a link, you can update it in the Link Manager, and all users who click on that link will be redirected to the new URL without disrupting their browsing experience.

Overall, the WordPress Link Manager is a valuable tool for website owners who want to keep their links organized and up-to-date. By utilizing this feature, you can ensure that your users always have access to the content they are looking for, and you can easily manage and update your links as needed to provide a seamless browsing experience for your audience.

For example, if somebody visits https://thisismyurl.com/links/greengeeks/ here on my website, they’re automatically redirected to https://www.greengeeks.com/track/thisismyurl/cp-default by the WordPress Link Manager. If I ever need to update the affiliate link for GreenGeeks, I can edit the Link and make that adjustment.

Implementing a Redirect for the Link Custom Post Type

The final step for creating your WordPress Link Manager is to add some code to redirect your Custom Post Type page to the URL you’ve included on the Links page.

Download and open your theme’s function.php file.

Paste the following code at the bottom of your functions.php file:

if ( ! function_exists( 'timu_links_redirect' ) {
function timu_links_redirect() {
	if ( is_single() && get_post_type() == 'links' ) {
		global $post;
		$link_url = get_field( 'link_url', $post->ID );
		
		if ( $link_url )
			wp_redirect( $link_url );
	}
}
}

add_action( 'template_redirect', 'timu_links_redirect' ); 

Save and upload your functions.php file.

Now, when you visit the Permalink in your test post, you’ll be directed to the URL you included in the Link URL.

What Does This Code Do?

function_exists() is a PHP function that checks to see if a function exists. This code tells WordPress to load this code only if the function (‘timu_links_redirect’) doesn’t exist.

is_single() is a WordPress function that checks to see if the request being made is trying to load a post rather than a category or index page.

get_post_type() is a WordPress function that gets the Post Type or Custom Post Type. In this case, it’s checking that get_post_type() is equal to ‘links’, the CPT we created for our WordPress Link Manager.

global $post is the WordPress value that tells WordPress what specific Post or Page is currently being loaded.

$link_url = get_field( ‘link_url’, $post->ID ) looked to the database to get the link_url for this specific Post. If there is a result, then the code is allowed to continue.

wp_redirect() is a WordPress function that will redirect the current Post to the URL stored in the $link_url variable.

Finally, add_action( ‘template_redirect‘, ‘timu_links_redirect’ ) is the Hook used in WordPress to tell this function to load during the Template Redirect function within the WordPress initialization routine. We could attempt to load our redirect in hundreds of places, but the Template Redirect function loads after WordPress learns what Post is being loaded. However, before WordPress outputs any content to the web browser, it is a great place to redirect it.

By leveraging Advanced Custom Fields for link management and implementing redirects for Custom Post Types, WordPress users can take control of their site’s links and streamline navigation for visitors. Whether you’re managing affiliate links, external resources, or internal references, creating a custom Link Manager with ACF and redirects ensures a seamless and organized user experience on your WordPress site.

Don’t let your website’s links become a tangled mess. By implementing the WordPress Link Manager with Advanced Custom Fields, you can easily organize and manage your links like a pro. This article has provided you with the knowledge and tools you need to take your website to the next level. If you’re feeling overwhelmed or need a helping hand, don’t hesitate to reach out. Let’s work together to optimize your website’s link management and improve your overall user experience. Your website deserves it!

About the Author

Speaks about Technology and Improving Efficiency in the Work Place

Christopher Ross is a passionate geek with diverse skills and interests, making him a dynamic and resourceful professional. With a deep-rooted enthusiasm for technology, Christopher has built a career exploring innovative solutions and advancing his knowledge in the tech field, including his love of WordPress. His journey is marked by a relentless curiosity and a commitment to continuous learning, which he applies to his professional endeavours and projects. A passable woodworker and recovering photographer, Christopher’s creative pursuits showcase his ability to balance precision and artistry. As a father and mentor, he takes pride in guiding others, fostering a spirit of curiosity and growth in those around him.

Education: Currently working on my Master of Arts in Learning and Technology, Royal Roads University
Experience: Training Specialist, Sherwin-Williams Company

Social Links

Subscribe to My Newsletter

Would you like to receive updates when I post? Please read my privacy policy and subscribe!

Similar Posts

Leave a Reply