Title: Get permalink
Last modified: October 16, 2019

---

# Get permalink

 *  [YOELO](https://wordpress.org/support/users/yoebo/)
 * (@yoebo)
 * [6 years, 5 months ago](https://wordpress.org/support/topic/get-permalink-2/)
 * I’d like to use a shortcode in my page editor to insert the permalink into a 
   link but I can’t get it to work and or is buggy.
 *     ```
       add_shortcode( 'yo_permalink', 'yo_permalink' );
       function yo_permalink($atts, $content) {
          return get_the_permalink();
       }
       ```
   
 * and on my page editor I add
    `<a href="[yo_permalink]" data-product_id="38"> 
   add to cart </a>`
 * This creates a hyperlink but with the following address e.g. `http://example.
   com/page/[yo_permailnk]`
 * If I remove `data-product_id="38"` from the above it works.
    -  This topic was modified 6 years, 5 months ago by [YOELO](https://wordpress.org/support/users/yoebo/).

Viewing 3 replies - 1 through 3 (of 3 total)

 *  [mrtom414](https://wordpress.org/support/users/mrtom414/)
 * (@mrtom414)
 * [6 years, 5 months ago](https://wordpress.org/support/topic/get-permalink-2/#post-12037163)
 * link for get_permalink. Why do you need to put it into a function
 * [https://developer.wordpress.org/reference/functions/get_permalink/](https://developer.wordpress.org/reference/functions/get_permalink/)
 *  [mark l chaves](https://wordpress.org/support/users/mlchaves/)
 * (@mlchaves)
 * [6 years, 5 months ago](https://wordpress.org/support/topic/get-permalink-2/#post-12037257)
 * Hi [@yoebo](https://wordpress.org/support/users/yoebo/),
 * Please use this as a trial example.
 *     ```
       // Custom link shortcode function with attribute and variable inputs.
       function custom_link_shortcode( $atts ) {
           extract( shortcode_atts( array(
               'c_link' => get_permalink(),
               'data_product_id' => '0',
               'target' => '_blank',
               'link_text' => 'this is a link',
           ), $atts, 'customlink' ) );
   
           return '<a href="'.esc_url( $c_link ).'" data-product_id="'.$data_product_id.'" target="'.$target.'">'.esc_html($link_text).'</a>';
       }
       add_shortcode( 'custom_link', 'custom_link_shortcode' );
   
       /* Example Usage
   
       [custom_link]
       [custom_link data_product_id="5" link_text="link with default url and product ID of 5"]
       [custom_link c_link="https://m.marklchaves.com" data_product_id="3" target="_self" link_text="check out my page on Amp!"]
   
       */
       ```
   
 * P.s. I’ve tested this on my site WP 5.2.4 PHP 7.1.30.
    -  This reply was modified 6 years, 5 months ago by [mark l chaves](https://wordpress.org/support/users/mlchaves/).
      Reason: Added two more usage examples
 *  Thread Starter [YOELO](https://wordpress.org/support/users/yoebo/)
 * (@yoebo)
 * [6 years, 5 months ago](https://wordpress.org/support/topic/get-permalink-2/#post-12038144)
 * [@mrtom414](https://wordpress.org/support/users/mrtom414/) Because PHP doesn’t
   parse when added into page text editor.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Get permalink’ is closed to new replies.

## Tags

 * [a tag](https://wordpress.org/support/topic-tag/a-tag/)
 * [custom shortcode](https://wordpress.org/support/topic-tag/custom-shortcode/)
 * [hyperlink](https://wordpress.org/support/topic-tag/hyperlink/)
 * [links](https://wordpress.org/support/topic-tag/links/)
 * [php](https://wordpress.org/support/topic-tag/php/)
 * [shortcode](https://wordpress.org/support/topic-tag/shortcode/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 3 replies
 * 3 participants
 * Last reply from: [YOELO](https://wordpress.org/support/users/yoebo/)
 * Last activity: [6 years, 5 months ago](https://wordpress.org/support/topic/get-permalink-2/#post-12038144)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
