Skip to content

Instantly share code, notes, and snippets.

@dhaupin
Last active November 15, 2020 06:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhaupin/7a5d429de5be0d623add58edad51b479 to your computer and use it in GitHub Desktop.
Save dhaupin/7a5d429de5be0d623add58edad51b479 to your computer and use it in GitHub Desktop.
Wordpress shortcode to display WP or Woocommerce login forms
<?php
/**
* Shortcode - Render Wordpress login form
**/
add_shortcode('wp_login_form', function() {
if (is_user_logged_in()) {
} else {
ob_start();
wp_login_form();
return ob_get_clean();
}
});
/**
* Shortcode - Render Woocommerce login/register form
**/
add_shortcode('woocommerce_login', function() {
if (is_user_logged_in()) {
} else {
ob_start();
echo '<div class="woocommerce">';
wc_get_template('myaccount/form-login.php');
echo '</div>';
return ob_get_clean();
}
});
/**
* Mitigate theme before render
**/
add_action('template_redirect', function() {
global $post;
if (has_shortcode($post->post_content, 'woocommerce_login')) {
// Add Woocommerce body classes
add_filter('body_class', function($body_classes) {
$body_classes[] = 'woocommerce-account';
$body_classes[] = 'woocommerce-page';
return $body_classes;
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment