Last active
April 19, 2023 09:36
-
-
Save divienginesupport/75b8284c1779b0bff4b4b1c4828ebadf to your computer and use it in GitHub Desktop.
WooCommerce Snippet - Shortcode for My Account text if user is logged in, or Login/Register if not
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Shortcode function for generating a link to the "My Account" page if the user is logged in, | |
* or to the "Register/Login" page if the user is not logged in. | |
* | |
* You can pass it a code from the Divi Icon Font for the icon to use | |
* -> https://www.elegantthemes.com/blog/resources/elegant-icon-font | |
* | |
* Shortcode tag: [de_login_register_link icon="e08a"] | |
* | |
* Make sure to add this CSS also https://gist.github.com/divienginesupport/ea2d56f435c94ca7607c243b9c8d829e | |
* | |
* @param array $atts Shortcode attributes. | |
* @return string The generated HTML link. | |
*/ | |
function de_login_register_link_shortcode($atts) { | |
// Set default attributes and merge with provided attributes | |
$atts = shortcode_atts(array( | |
'icon' => 'e08a', // Default value for the icon attribute | |
), $atts); | |
// If an icon code point is provided, format the icon HTML | |
$icon_html = $atts['icon'] ? '<span class="de-icon">&#x' . esc_attr($atts['icon']) . ';</span> ' : ''; | |
// Check if the user is logged in | |
if (is_user_logged_in()) { | |
// If the user is logged in, show the "My Account" link | |
$my_account_page_url = wc_get_page_permalink('myaccount'); | |
return '<a href="' . esc_url($my_account_page_url) . '">' . $icon_html . 'My Account</a>'; | |
} else { | |
// If the user is not logged in, show the "Register/Login" link | |
$login_page_url = wc_get_page_permalink('myaccount'); | |
return '<a href="' . esc_url($login_page_url) . '">' . $icon_html . 'Register/Login</a>'; | |
} | |
} | |
// Register the shortcode with the name 'de_login_register_link' | |
add_shortcode('de_login_register_link', 'de_login_register_link_shortcode'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment