Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

WordPress From Bootstrap to WordPress Create Bootstrap Styled Theme Templates Creating a static front page

Gabriel Ward
Gabriel Ward
20,222 Points

Hard coding links and best practice

In this video, Zac creates a button and hard codes a link for it. He then says that it's not best practice to do this. I'm wondering, what is the best way to go about coding in links to buttons...

Any advice on this would be greatly appreciated.

6 Answers

Hi Gabriel, Zach is correct regarding best practice as absolute paths tend to be bad for internal links on your site. Example would be site moves to a new domain, this would cause any absolute link for anything to break. While a relative link would work fine as long as the file structure remained the same.

Also how you generate the links themselves can make your life easier depending on how the site is done. An example for this would be on a WordPress site you could use the ID to generate the link. This would then cause the link to work correctly as a relative link regardless of changing the name or URL of the link itself.

There are more reasons to avoid absolute links/paths like SEO etc. as well.

Gabriel Ward
Gabriel Ward
20,222 Points

Hi Mark,

Cool. So a best practice would be to use ID's? I understand why using absolute paths is not a good idea, however Zac simply says 'this is not a good idea' without actually suggesting the best alternatives. So what are the best practices/alternatives for links?

Relative/root-relative links would be best practice for anything internal as you have full control over these. My reference to using ID's with Wordpress allows for changes to the permalink for SEO etc. and would allow for the links to still work correctly even after changes vs looking through your site and manually adjusting them.

Example here is say I have a contact page that has multiple subpages for different depts in a company and I have these all linked in the sidebar of my site. If the link is generated from the page ID, I can customize the permalink to anything I want later on and never have to worry about this breaking that link.

Gabriel Ward
Gabriel Ward
20,222 Points

Ok great, would you need to use 'get_template_directory_uri' at all?

Eg

<a href='get_template_directory_uri() . /examplepage-id'></a>

I'm just a bit confused about the use of the word 'relative'

That would depend on how your adding the links. If your using the wysiwyg to add a link for in the content for example you can not place PHP directly in it. So for the contact page the href would be /contact.

If you are trying to hard code the link it would be similar to this

<a href="<?php echo get_permalink(154);?>">Shop</a>

This would pull my Shop page for my site

Gabriel Ward
Gabriel Ward
20,222 Points

Ok great, so you could add this in the text editor. And the 154 refers to the page id?

Correct, you would just refer to whatever is in the URL for the page ID and replace it with whatever one you needed/wanted to link.

Gabriel Ward
Gabriel Ward
20,222 Points

Ok great thanks Mark. Out of interest, do you work professionally as a Wordpress developer, and/or more general web developer?

Your welcome and Wordpress/Magento with experience in custom CMSs.

Gabriel Ward
Gabriel Ward
20,222 Points

Nice, freelance and/or for a digital agency?

Digital Agency