Check if Wordpress User exists by email - php

For the life of me I cant get this to work (default example from WP Codex). I created a php file with this code and dropped it in my theme folder, when I access the file on the web I get a blank page, nada -- am I missing something, do i have to put this someplace else? any help is greatly appreciated.
<?php
$email = 'myemail#example.com';
$exists = email_exists($email);
if ( $exists )
echo "That E-mail is registered to user number " . $exists;
else
echo "That E-mail doesn't belong to any registered users on this site";
?>

simple answer,
If that is the template page, than use this:
<?php
$email = 'myemail#example.com';
$exists = email_exists($email);
if ( $exists )
echo "That E-mail is registered to user number ";
else
echo "That E-mail doesn't belong to any registered users on this site";
?>
and ensure you have correct opening and closing php tags.
But if are from other than tempalte page then use this:
<?php
require_once("../../../../wp-load.php"); //ADD THIS
$email = 'myemail#example.com';
$exists = email_exists($email);
if ( $exists )
echo "That E-mail is registered to user number ";
else
echo "That E-mail doesn't belong to any registered users on this site";
?>
Add or Remove ../ in the require_once("../../../../wp-load.php") as per the page location.
This will surely help you.

Try to add wp-load.php in your file with right path.
<?php
require_once("../../../wp-load.php"); //ADD THIS
$email = 'myemail#example.com';
$exists = email_exists($email);
if ( $exists )
echo "That E-mail is registered to user number " . $exists;
else
echo "That E-mail doesn't belong to any registered users on this site";
?>

Related

WPJOBMANAGER, send email to application email on Job listing approval

Hello I am using WP Job manager and I want to send an email to on the application email once the job listing has been approved by the admin. I have found this code https://wpjobmanager.com/document/tutorial-send-email-employer-job-listing-approved/ but this only works to send a notification email to a user that already has an account. I want to send an email to the email provided in the job listing as often those users do not have an account on our website. I am sorry if this question has already been answered, I have looked around and couldn't find one. Also I tried to tag this with WPJOBMANAGER but I don't have enough rep to create a tag.
Thanks for all your help.
Okay after doing a bunch of research I have managed to find a way to do this.
function listing_published_send_email($post_id) {
if( 'job_listing' != get_post_type( $post_id ) ) {
return;
}
$post = get_post($post_id);
$author = get_userdata($post->post_author);
$job = get_the_job_application_method($post);
// Message to be sent to users.
$message = "Put the message you want to send here";
// Checks to see if the person who submitted the job listing is a user or a guest.
if ($author == 0 OR $author == NULL){
// Checks to see if the email is valid.
if ($job->type == 'email') {
$email = $job->email;
wp_mail($email, "Your job listing is online", $message);
}
else { // The email is not valid so return.
return;
}
}
else {
wp_mail($author->user_email, "Your job listing is online", $message);
}
}
add_action('pending_to_publish', 'listing_published_send_email');
add_action('pending_payment_to_publish', 'listing_published_send_email');
Should just be able to put this code in functions.php file and it will work.

sign up form with auto generated password and user hash_key for email confirmation

Hi friends am trying to create a sign up form that will generate an auto password for users and a hash key that will be needed for email verification before one can log in to their account but am having a problem, the values cannot be entered into the database. please help. here is my code.
if(isset($_GET['usertype'])){
$user=$_GET['usertype'];
if($user==1){
$acc="Starter Account";
}
else if($user==2){
$acc="Basic Account";
}
else{
$acc="Premium Account";
}
$hash_key=md5(rand(0,1000));
$pass=md5(rand(1000,5000));
if(isset($_POST['add'])){
$first_name=mysqli_real_escape_string($con, $_POST['first_name']);
$surname=mysqli_real_escape_string($con, $_POST['surname']);
$username=mysqli_real_escape_string($con, $_POST['username']);
$email=mysqli_real_escape_string($con, $_POST['email']);
$timestamp=strtotime("+21 Days");
$o_date=date('Y-m-d H:i:s', $timestamp);
$j_date=date('Y-m-d H:i:s');
if (( !preg_match ("/^[a-zA-Z\s]+$/",$first_name))||(strlen($first_name) < 3)) {
$first_name_error= "<p class='text-danger'>Your first name should contain letters only and must not be less than 3 characters</p>";
}
if (( !preg_match ("/^[a-zA-Z\s]+$/",$surname))||(strlen($surname) < 3)) {
$surname_error= "<p class='text-danger'>Your surname should contain letters only and must not be less than 3 characters</p>";
}
if (( !preg_match ("/^[a-zA-Z\s]+$/",$username))||(strlen($username) < 3)) {
$username_error= "<p class='text-danger'>Your username should contain letters only and must not be less than 3 characters</p>";
}
if(!filter_var($email,FILTER_VALIDATE_EMAIL)){
$email_error= "<p class='text-danger'>Please use a valid email</p>";
}
if(mysqli_num_rows(mysqli_query($con,"SELECT * FROM users WHERE username='$username'"))>0){
$username_rep_error="That username is already taken";
}
if(mysqli_num_rows(mysqli_query($con,"SELECT * FROM users WHERE email='$email'"))>0){
$email_rep_error="That email is already taken";
}
if((empty($first_name_error))&&(empty($surname_error))&&(empty($username_error))&&(empty($username_rep_error))&&(empty($email_rep_error))&&(empty($email_error))){
if(mysqli_query($con,"INSERT INTO users (first_name,surname,username,email,password,account,join_date,offer_expirely,hash_key) VALUES('$first_name','$surname','$username','$email','$pass','$acc','$j_date','$o_date','$hash_key')")){
$to = $email; // Send email to our user
$subject = 'Signup | Verification'; // Give the email a subject
$message = '
Thanks for signing up!
Your account has been created, you can login with the following credentials after you have activated your account by pressing the url below.
Username: '.$username.'
Password: '.$password.'
Please click this link to activate your account:
http://www.mysite.co.ke/verify.php?email='.$email.'&hash='.$hash.'
'; // Our message above including the link
$headers = 'From:noreply#mysite.co.ke' . "\r\n"; // Set from headers
mail($to, $subject, $message, $headers); // Send our email
$valid_msg="Your account has been made, please verify it by clicking the activation link that has been sent to your email.";
}
else{
$msg="There was a problem registering. Please try again.";
}
}
}
}
remove the single quotes
try this
if(mysqli_query($con,"INSERT INTO users (first_name,surname,username,email,password,account,join_date,offer_expirely,hash_key) VALUES($first_name,$surname,$username,$email,$pass,$acc,$j_date,$o_date,$hash_key)")){

PHP: get_user_by(id) not returning anything, user_id exists! Wordpress

Here is my code
$user_id = $_GET["user_id"];
$user = get_user_by('id',$user_id);
$balance = mycred_get_users_balance($user_id);
$user_info = get_userdata($user_id);
$first_name = $user_info->first_name;
$last_name = $user_info->last_name;
echo $first_name;
if ($balance > "0") {
mycred_subtract( 'Check-in',$user_id, -1, 'Checked in.' );
echo "Welcome " .$first_name." ".$last_name. "You are checked in.";
}
else{ echo "Welcome " .$first_name." ".$last_name;
echo "<br/>";
echo "You have a balance of " . $balance . ".";
echo "<br/>";
echo "Please purchase more credits at our website
I am trying to display the User first name and last name, this was working previously on my wordpress website, now the $user object is empty I'm quite sure because nothing is being echoed.
I am able to parse the $user_id from the URL and echo it, but when I try to echo the $first_name, get nothing.
I do get
Welcome You are checked in
Hence I know that the get_user_by is not working, the $user_id DOES exist in the database, I'm testing #1 which is my ID.
I feel like it is something to do with a mobile optimization because we are scanning a QR code to redirect to this page and passing the user_id through the URL.
EDIT* I switched off JetPack smartphone optimization and got the functions working, except still no $first_name
Any ideas???
So it turns out I did not have a field stored as $first_name as I updated the registration form. I changed the $first_name to call for $user_nicename instead and it shows the users account display name.

PHP unaccepted text form [duplicate]

This question already has answers here:
Checking if string contains "HTTP://"
(7 answers)
Closed 7 years ago.
recently I started getting bots from one website that keeps posting their website links in my "Customer Feedback" form. I want to make my form deny any text that contains "http://" or any other words/phrases I will add (they will surely find a way to bypass the "http://"), but the thing is I don't know how to do so. Here is the code (the forms that need to be checked for "http://" are $name and $comment):
if(isset($_POST['add'])){
$name = $_POST['name'];
$email = $_POST['email'];
$comment = $_POST['comment'];
$ip = $_SERVER['REMOTE_ADDR'];
$datetime = date('Y-m-d H:i');
$checkIp = mysql_query("SELECT ip from comments WHERE ip = '$ip'");
if (mysql_num_rows($checkIp) > 0) {
echo "Only 1 feedback per IP allowed!";
$IP = mysql_fetch_array($checkIp);
print_r($IP);
}
if($name){
if($email){
if($comment){
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
mysql_query("INSERT INTO comments (id, name, email, comment, ip, datetime) VALUES ('','$name','$email','$comment','$ip','$datetime')");
}
else
echo "The email address is invalid!<br><br>";
}
else
echo "You haven't entered any comment!<br><br>";
}
else
echo "You haven't entered an email address!<br><br>";
}
else
echo "You haven't entered your name!<br><br>";
}
Thank you!
You need to check and see if a substring is contained in the $name or $comment variables like so:
if (strpos($comment,'http://') === false and strpos($name, 'http://') === false) {
echo 'continue executing your code here';
}else{
echo 'Its a bot!';
}

Simple check of email address in Constant Contact

I can't find a damned bit of documentation for using the Constant Contact REST API to check if an email address is in a list or not.
The following seems to be completely useless:
include_once('cc_class.php');
$ccContactOBJ = new CC_Contact("basic", $cckey, $ccuser, $ccpass);
if(($_SERVER['REQUEST_METHOD']=="POST") && !empty($_REQUEST['member-submit'])) {
$contact = $ccContactOBJ->getSubscribers(urlencode($_POST['MemberEmail']));
if (empty($contact['items'])) {
$message = 'You are not listed in our database.';
}
else {
$message = 'You are already listed in our database';
}
echo $message;
}
Anyone have ANY idea how to return a true or false value?

Categories