What is the best way to insert links indepently of the permalink structure?

When writing html in the code, if you use

<a href="https://wordpress.stackexchange.com/questions/111843/<?php echo get_permalink(123); ?>"></a>

The url outputted is

http://www.site.com?p=123

if you deactive pretty permalinks or, if you active pretty permalinks it change automatically according to your permalink structure.

You never have to hardcoding url in Worpress when writing html in plugin / template files.

If you intend when you have to add a link in the posts content, using the link feature from worpress editor, and “Or link to existing content” option (see image below)

worpress editor link feature

The link that is putted in the html, yes, is hardcoded, but using the current rewrite structure. If you change this structure in the future the link stay the same in the html, but WordPress is clever enough and your link still works! (I’m sure it works, but I’m not sure how: if I’m not wrong a 301 redirect will be maden).

However if after changing url structure you hate having in the post content urls with old structure (even if they works), you can create a shortcode, like

[post id=123]

that outputs the current permalink for the post.

Make this shortcode works is easy, however, someone has already do the work… see here.

tech