if (isset($_REQUEST['unfollow'])) {
global $wpdb;
$table_name = $wpdb->base_prefix . 'bp_follow';
$current_user = $_REQUEST['current_user'];
$author_user = $_REQUEST['author_user'];
//$wpdb->query($wpdb->prepare("DELETE * FROM ".$table_name." where leader_id=".$author_user." and follower_id=".$current_user));
$wpdb->delete($table_name, array('leader_id' => $author_user, 'follower_id' => $current_user,));
echo "<script type='text/javascript'>
window.location=document.location.href;
</script>";
}
if (isset($_REQUEST['follow'])) {
$current_user = $_REQUEST['current_user'];
$author_user = $_REQUEST['author_user'];
$table_name = $wpdb->base_prefix . 'bp_follow';
global $wpdb;
$wpdb->insert($table_name, array(
"follower_id" => $current_user,
"leader_id" => $author_user,
));
echo "<script type='text/javascript'>
window.location=document.location.href;
</script>";
}
function follow_btn($current_user, $author_id)
{
global $wpdb;
$table_name = $wpdb->base_prefix . 'bp_follow';
$results = $wpdb->get_results("SELECT follower_id FROM " . $table_name . " where leader_id=" . $author_id . " and follower_id=" . $current_user);
//var_dump($results);
if ($results) {
return "<div class='follow_div'>
<form method='post' action=''>
<button type='submit' name='unfollow'>Unfollow me</button>
<input type='hidden' name='current_user' value='" . $current_user . "' />
<input type='hidden' name='author_user' value='" . $author_id . "' />
</form>
</div>";
} else {
return "<div class='follow_div'>
<form method='post' action=''>
<input type='hidden' name='current_user' value='" . $current_user . "' />
<input type='hidden' name='author_user' value='" . $author_id . "' />
<button type='submit' name='follow'>Follow me</button>
</form>
</div>";
}
}
front page
$t_id = get_queried_object_id();;
$author_meta = get_option("taxonomy_term_$t_id");
$current_user = get_current_user_id();
echo follow_btn($current_user, $author_meta['presenter_id']);