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!
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!
Share Improve this question asked Oct 8, 2020 at 10:06 user2516117user2516117 771 silver badge8 bronze badges1 Answer
Reset to default 2esc_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.