Add link to WooComerce my account page using hook

628 Views Asked by At

I found this code from @LoicTheAztec that adds allows you to add some extra text to the My Account - Dashboard tab in WooComerce.

I am trying to add a hyper link to it however my php knowledge and syntax is appalling and I clearly can't use the standard a href html to do this as this throws an error : syntax error, the unexpected identifier "https", expecting ")"

Any help on how I could add a URL to this would be very much appreciated. Thank you.

Code :

add_action( 'woocommerce_account_content', 'action_woocommerce_account_content' );
function action_woocommerce_account_content(  ) {
    global $current_user; // The WP_User Object

    echo '<p>' . __("<a href="https://google.com"> Request Wholesale account </a>", "woocommerce") . '</p>';
};
3

There are 3 best solutions below

0
Ash0ur On

You are using double quotes twice which cancel each others. simpler way to do it. also you need to separate the translation function from the html.

    add_action( 'woocommerce_account_content', 'action_woocommerce_account_content' );
    function action_woocommerce_account_content() {
        global $current_user; // The WP_User Object
        ?>
        <p><a href="https://google.com"><?php _e( 'Request Wholesale account', 'woocommerce' ); ?></a></p>
        <?php
};
0
mujuonly On
add_action('woocommerce_account_content', 'action_woocommerce_account_content');

function action_woocommerce_account_content() {
    global $current_user; // The WP_User Object

    echo '<p>' . sprintf(esc_html__('%1$s Request Wholesale account %2$s', 'woocommerce'), '<a href="https://google.com">', '</a>') . '</p>';
}

Always escape/secure output for security using like esc_html__.

0
Acephalia On

Solved this with Martin's help. Thanks for all the other suggestions too.

The way I ended up doing it was to use the Dashboard hook as suggested and the single quote echo.

add_action( 'woocommerce_account_dashboard', 'custom_account_dashboard_content' );
function custom_account_dashboard_content(){
    echo '<div>' . __('<br><b><a href="https://google.com"> REQUEST WHOLESALE ACCOUNT </a></b>', 'woocommerce') . '</div>';
}