• 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. https://example.com/page/[yo_permailnk]

    If I remove data-product_id="38" from the above it works.

    • This topic was modified 5 years, 1 month ago by YOELO.
Viewing 3 replies - 1 through 3 (of 3 total)
  • link for get_permalink. Why do you need to put it into a function

    https://developer.www.ads-software.com/reference/functions/get_permalink/

    Hi @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 5 years, 1 month ago by mark l chaves. Reason: Added two more usage examples
    Thread Starter YOELO

    (@yoebo)

    @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.