1

I use a shortcode that retrieves the url of woocommerce products and builds a link:

function woo_url_shortcode( $atts ) {
    $atts = shortcode_atts( array(
        'id' => null,
    ), $atts, 'bartag' );
    $html = '';
    if( intval( $atts['id'] ) > 0 && function_exists( 'wc_get_product' ) ) {
        $_product = wc_get_product( $atts['id'] );
        $html = $_product->get_permalink();
    }
    $text = 'Some text';
        return '<p class="woo-booking"><a href="'.$html.'">'.$text.'</a></p>';
}

Works fine. But now I need the text translated like this:

esc_html_e( 'Some text', 'generatepress-child' );

So, the code would be:

function woo_url_shortcode( $atts ) {
    $atts = shortcode_atts( array(
        'id' => null,
    ), $atts, 'bartag' );
    $html = '';
    if( intval( $atts['id'] ) > 0 && function_exists( 'wc_get_product' ) ) {
        $_product = wc_get_product( $atts['id'] );
        $html = $_product->get_permalink();
    }
        return '<p class="woo-booking"><a href="'.$html.'">'.esc_html_e( 'Some text', 'generatepress-child' ).'</a></p>';
}

Language files are there and working. But not with this shortcode. The string is translated but appears at the top of the page and is repeated several times. What exactly am I missing, and how can I fix this?

Thank you!

1 Answer 1

2

esc_html_e() displays (echo) the output, hence it appears at the wrong place. So you should instead use esc_html__() which returns the translated string.

1
  • 1
    Awesome. Thanks a bunch for the help! Commented Oct 8, 2020 at 11:35

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.