Skip to content

Instantly share code, notes, and snippets.

@digitalchild
Created October 13, 2014 05:34
Show Gist options
  • Save digitalchild/a174794637203c85b94f to your computer and use it in GitHub Desktop.
Save digitalchild/a174794637203c85b94f to your computer and use it in GitHub Desktop.
Force vendor to accept new terms
<?php
add_action('wp_login','check_terms_accepted');
//check if user has accepted the terms when logging in
function check_terms_accepted($login) {
$user = get_user_by('login', $login);
$terms_accepted = get_user_meta( $user->ID, 'new_terms');
$msg = 'Please Accept our new Terms';
if($user->has_cap('vendor') && !$terms_accepted) {
wp_redirect(get_option('siteurl') . '/somepage-template/?msg=$msg');
exit();
}
}
//run this function when a user tries to load a page
add_action('pre_get_posts','check_terms_accepted_page');
//check if user has accepted the terms before loading a page
function check_terms_accepted_page() {
if(is_user_logged_in()) {
if(is_page() && !is_page('somepage-template') && !is_admin()) {
$user_id = get_current_user_id();
$user = new WP_User($user_id);
$terms_accepted = get_user_meta( $user_id, 'new_terms' );
$msg = 'Please Accept our new Terms';
if($user->has_cap('vendor') && !$terms_accepted) {
wp_redirect(get_option('siteurl') . '/somepage-template/?msg='.$msg);
exit();
}
}
}
}
// You will then need a page called somepage-template that loads a template that has the terms and a bit of code to set the user meta 'new_terms' to true.
// It'll have a form with 'accept the terms' submit button. On submit, save the new_terms user meta.
?>
<?php
/**
* Template Name: Terms
*
* @package WordPress
*/
global $current_user;
$user_id = $current_user->ID;
if(isset($_POST['submitted'])) {
// The form has been submitted so they have agreed.
update_user_meta( $user_id, 'new_terms', true );
}
get_header(); ?>
<?php while ( have_posts() ) : the_post(); ?>
<!-- Page Title -->
<?php the_title(); ?>
<?php if (!is_user_logged_in()) {
// Send them to their account if they haven't logged in.
wp_redirect(get_option('siteurl').'/my-account/');
} else {
// Get their role to make sure they are a vendor
$current_user_role = strtolower(get_user_role($user_id));
// if they are a vendor and the form hasn't been submitted.
if ($current_user_role == 'vendor' && !isset($_POST['submitted'])) { ?>
<!-- The content would be the terms -->
<?php the_content(); ?>
<!-- Form with accept button. You could put tick box and check -->
<form action="<?php the_permalink(); ?>" method="post">
<input type="submit" name="submit" value="Accept Terms" />
</form>
<?php } ?>
<?php } ?>
<?php } ?>
<?php endwhile; // end of the loop. ?>
<?php get_footer(); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment