I have added a custom field to my site called "dansk_url", to which a link to the version in Danish (different domain) will be entered. I would like to be able to output this as a shortcode to add anywhere within the post, and it should appear as a link (anchor text will always be "Dansk" and whichever url has been entered in the custom field for that post.

Is this possible? I prefer a non-plugin solution.

  • 1
    How did you add the custom field? Using custom code? If so, what's your code?
    – Sally CJ
    Commented Apr 26, 2019 at 3:36
  • 1
    post some code so that other developers could see where you stuck at.
    – maverick
    Commented Apr 26, 2019 at 3:45
  • Sorry, I didn't add any code. I used the instructions here link
    – auntie
    Commented Apr 26, 2019 at 3:49

2 Answers 2


Put this in functions.php of your theme.

add_shortcode( 'dansk_url', 'dansk_url' );
function dansk_url() {
    $the_url= get_post_meta( get_the_ID(), 'dansk_url', true );

    if ( $the_url)  $output .= '<a href="' . esc_url( $the_url) . '">Dansk</a>';

    return $output;

and use shortcode [dansk_url] in the post.

  • 1
    There's a syntax error here: $output .= '<a href="' . esc_url( $the_url) . '">Dansk</a>' (missing ; at the end)...
    – Sally CJ
    Commented Apr 26, 2019 at 7:30
  • 1
    @SallyCJ, Stay blessed! Thanks for correction. Commented Apr 26, 2019 at 11:14
  • This worked perfectly for me. Thanks a million!
    – auntie
    Commented Apr 26, 2019 at 16:43
  • @auntie glad to know that it worked. Please mark this answer as Accepted Answer an up-vote, to help other people searching for similar issue to find the solution Commented Apr 26, 2019 at 16:52

I would like to be able to output this as a shortcode to add anywhere within the post

So since you said, "I prefer a non-plugin solution", then you can use this (which you'd add to the theme functions.php file) to create the shortcode:

add_shortcode( 'dansk_link', 'dansk_link_shortcode' );
function dansk_link_shortcode() {
    $url = get_post_meta( get_the_ID(), 'dansk_url', true );
    if ( $url ) : ?>
        <a href="<?php echo esc_url( $url ); ?>">Dansk</a>
    endif; // end $url
    return ob_get_clean();

And in the post content, add [dansk_link] anywhere you like.

The above PHP code is still considered a plugin, but it's basically "your own" plugin. :)

PS: For the function reference, you can check add_shortcode() and get_post_meta().


  1. I changed the shortcode name to dansk_link because that seems better than dansk_url for what the shortcode is for (i.e. outputting a link).

  2. In my original answer, I used output buffering (i.e. those ob_start() and ob_get_clean()) so that's it's easier for you (the question author) to edit the HTML output.

Having said the point #2 above, you could also use:

add_shortcode( 'dansk_link', 'dansk_link_shortcode' );
function dansk_link_shortcode() {
    if ( $url = get_post_meta( get_the_ID(), 'dansk_url', true ) ) {
        return '<a href="' . esc_url( $url ) . '">Dansk</a>';
    return '';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.