I'm trying to make a plugin for wordpress visitor counter and limit counts of visitor. I wrote this code, but counter variable increments 4 times or 5 times every time I refresh the page. Basically plugin is for free visits after free visits user should be logged in to visit website. Please check the code.
<?php
$count=0;
function ip_get(){
if(!empty($_SERVER['HTTP_CLIENT_IP'])){
//ip from share internet
$new_ip = $_SERVER['HTTP_CLIENT_IP'];
}elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
//ip pass from proxy
$new_ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}else{
$new_ip = $_SERVER['REMOTE_ADDR'];
}
global $wpdb;
$table_name=$wpdb->prefix.'wbs_user_ips';
$results = $wpdb->get_results( "SELECT * FROM $table_name"); // Query to fetch data from database table and storing in $results
if(empty($results)){
$wpdb->insert($table_name,
array(
'user_ip'=>$new_ip,
'user_count'=>'1',
'user_status'=>'0',
)
);
}
if(!empty($results)) // Checking if $results have some values or not
{
$count=0;
foreach($results as $row){
$ip= $row->user_ip;
$count=$row->user_count;
}
if($ip==$new_ip){
echo $count++;
$wpdb->query($wpdb->prepare("UPDATE $table_name
SET user_count=".$count."
WHERE user_ip = %s",$ip));
}
}
}
?>
I think there is an error in your foreach loop.
And please make sure you use the correct hook
<?php
$count=0;
function ip_get(){
if(!empty($_SERVER['HTTP_CLIENT_IP'])){
//ip from share internet
$new_ip = $_SERVER['HTTP_CLIENT_IP'];
}elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
//ip pass from proxy
$new_ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}else{
$new_ip = $_SERVER['REMOTE_ADDR'];
}
global $wpdb;
$table_name=$wpdb->prefix.'wbs_user_ips';
$results = $wpdb->get_results( "SELECT * FROM $table_name"); // Query to fetch data from database table and storing in $results
if(empty($results)){
$wpdb->insert($table_name,
array(
'user_ip'=>$new_ip,
'user_count'=>'1',
'user_status'=>'0',
)
);
}
if(!empty($results)) // Checking if $results have some values or not
{
$count=0;
foreach($results as $row){
$ip= $row->user_ip;
$count=$row->user_count;
if($ip==$new_ip){
echo $count++;
$wpdb->query($wpdb->prepare("UPDATE $table_name
SET user_count=".$count."
WHERE user_ip = %s",$ip));
}
}
}
}
?>
Related
I'm trying to send emails notification using wp cron but my code doesn't works.
I'm using a plugin to run my wp cron job every day and the function called is named EVENT NOTIFICATION.
In functions.php I put this code:
function EVENT_NOTIFICATION_function() {
global $wpbd;
$test_data = '2025-05-04';
$results=$wpdb->get_results ("SELECT * FROM wp_postmeta WHERE meta_key = 'data_concert' AND meta_value = '$test_data'" );
foreach ($results as $row) {
$post_id = $row->post_id;
// Send email to;
$email_to = 'tester1#gmail.com, tester2#gmail.com';
// Set the email subject
$email_subject = 'Event Alert';
// Set the email content
$content = 'Hey dude remember that you have to play!';
// Set the email headers
$headers = array('Content-Type: text/html; charset=UTF-8');
wp_mail( $email_to, $email_subject, $content , $headers );
}
}
add_action( 'EVENT_NOTIFICATION', 'EVENT_NOTIFICATION_function' );
There's something wrong on this part:
$results=$wpdb->get_results ("SELECT * FROM wp_postmeta WHERE meta_key = 'data_concert' AND meta_value = '$test_data'" );
foreach ($results as $row) {
$post_id = $row->post_id;
cause if I delete it everything works well!
I don't find how to fix it.
I have created a new table within the same database that contains the Wordpress tables. How can I query this specific table from a custom php page? The page can't seem to connect to the db. It's been a while since I have had to do any php/mysql coding, so forgive my ignorance.
<?php
global $wpdb;
$sql = "SELECT * FROM table_name";
$result = mysqli_query($wpdb, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["ID"]. " " . $row["content"]. "<br>";
}
} else {
echo "0 results";
}
?>
$wpdb is an instance of the wpdb class. You make calls to it like the following:
global $wpdb;
//Prepare a SQL Query
$query = $wpdb->prepare("SELECT * FROM my_custom_table WHERE column_foo = %s", $someVariable);
$results = $wpdb->get_results($query);
foreach($results as $result) {
//do a thing
}
I am building a new login page for a user login page, but I am using the wp_user table. When I am trying to user md5() or wp_hash_password() to check user password, it is not working.
My code is as follows:
<?php if(isset($_POST['submit'])){
//print_r($_POST); die();
$user_email=$_POST['email'];
$user_pass=$_POST['pass'];
$encuser_pass = wp_hash_password( $user_pass );
echo $check_user="select * from wp_users WHERE user_login='$user_email'AND user_pass='$encuser_pass)";
//echo $check_user; die;
$run=$wpdb->get_results($check_user, ARRAY_A);
if(count($run))
{
$_SESSION['email']= $user_email;//here session is used and value of $user_email store in $_SESSION.
wp_redirect( get_site_url().'/dashboard/');
exit;
}
else
{
echo "<script>alert('Email or Password is wrong')</script>";
}
}?>
It's not working. What way I need to implement that?
<?php require_once(ABSPATH . 'wp-includes/class-phpass.php');
<?php if(isset($_POST['submit'])){
$user_email=$_POST['email'];
$user_pass=$_POST['pass'];
global $wpdb;
$check_user="select user_pass from wp_users WHERE user_login='$user_email'";
$wp_hasher = new PasswordHash(8, TRUE);
$password_hashed = $wpdb->get_var($check_user);
if($wp_hasher->CheckPassword($user_pass, $password_hashed)) {
echo "YES, Matched";
} else {
echo "No, Wrong Password";
}
}
I'm using HTML custom email templates for Wordpress notifications.
Every template works fine. For some reason, though - the password reset template (which works fine otherwise) will not append the user_login variable at the end of the password reset link - which is vital for the key to be valid. The link without the $user_login renders an "invalid key" error on the WP password reset page.
An example of the string in the link is below - note the missing login=username at the very end.
url/wp-login.php?redirect_to=url?action=rp&key=12345678910&login=http://url.com/wp-login.php?redirect_to=url?action=rp&key=12345678910&login=
Here is the code I'm using to modify the template. Does anyone know why this is happening - and if so, how I can fix it?
add_filter ('retrieve_password_message', 'custom_retrieve_password_message', 10, 2);
function custom_retrieve_password_message($content, $key) {
global $wpdb;
$user_login = $wpdb->get_var("SELECT user_login FROM $wpdb->users WHERE user_activation_key = '$key'");
ob_start();
$email_subject = custom_retrieve_password_title();
include('email_header.php');
?>
<p>It looks like you need to reset your password for your account!</p>
<p>To reset your password, visit the following address, otherwise just ignore this email and nothing will happen.<p>
Reset password
<?php
include('email_footer.php');
$message = ob_get_contents();
ob_end_clean();
return $message;
}
I think the problem is that wordpress changed the way the user activation key is saved in the database. The key is hashed before it's saved in the wp_users table and $key contains the unhashed plain-text activation key. So the following line of your code won't get a result and $user_login will be empty.
$user_login = $wpdb->get_var("SELECT user_login FROM $wpdb->users WHERE user_activation_key = '$key'");
Try this instead:
add_filter ('retrieve_password_message', 'custom_retrieve_password_message', 10, 2);
function custom_retrieve_password_message($content, $key) {
global $wpdb;
$user_data = get_user_by_email(trim($_POST['user_login']));
$user_login = $user_data->user_login;
ob_start();
$email_subject = custom_retrieve_password_title();
include('email_header.php');
?>
<p>It looks like you need to reset your password for your account!</p>
<p>To reset your password, visit the following address, otherwise just ignore this email and nothing will happen.<p>
Reset password
<?php
include('email_footer.php');
$message = ob_get_contents();
ob_end_clean();
return $message;
}
I am not totally sure about all the rest . - but for one thing , you need to get your quotes right . try
echo wp_login_url('url')
not
echo wp_login_url("url")
e.g. :
Reset password
or try
echo '<a href=' . wp_login_url("url") . '?action=rp&key='.$key.'&login='.$user_login.'>Reset password</a> ';
Also - ( and if that is no help ) can you elaborate more where you use it , how and with what templates so we can try and dig deeper ?
For anyone interested, this is another solution:
add_filter ('retrieve_password_message', 'custom_retrieve_password_message', 10, 2);
function custom_retrieve_password_message($content, $key) {
global $wpdb;
if ( empty( $_POST['user_login'] ) ) {
wp_die('<strong>ERROR</strong>: Enter a username or e-mail address.');
} else if ( strpos( $_POST['user_login'], '#' ) ) {
$user_data = get_user_by( 'email', trim( $_POST['user_login'] ) );
}else if(!empty( $_POST['user_login'] )){
$user_data = get_user_by('login', trim( $_POST['user_login']));
}elseif ( empty( $user_data ) ){
wp_die('invalid_email', __('<strong>ERROR</strong>: There is no user registered with that email address.'));
}
$user_login_name=$user_data->user_login;
ob_start();
$email_subject = 'Your password has been changed';
include('email_header.php');
?>
<p>It looks like you need to reset your password. <br/>To reset your password, click here, otherwise just ignore this email and nothing will happen.<p>
<?php
include('email_footer.php');
$message = ob_get_contents();
ob_end_clean();
return $message;
}
Additionally, you can use the POST data to check if the username or the email was submitted:
add_filter ('retrieve_password_message', 'custom_retrieve_password_message', 10, 2);
function custom_retrieve_password_message($content, $key) {
if ( username_exists($_POST['user_login']) ){
$user_login = $_POST['user_login'];
} else {
$user_data = get_user_by_email(trim($_POST['user_login']));
$user_login = $user_data->user_login;
}
ob_start();
...
I'm working on a wordpress, and using a plugin wordpress popular posts, and facing this issue.
How can I show a count of how many times a page has been viewed by visitors on category listing page or index page of the blog. This information is already there which is being used by plugin to display same in sidebar, not sure how can I use the same plugin data to show page view count on blog page.
I tried to find this, but didn't get exactly what I want.
Pls advise how to do this?
Plugin I use is http://wordpress.org/extend/plugins/wordpress-popular-posts/
You can simply do it without a plugin.
To count the post views, the first thing you have to do is to add the following code to your WordPress Theme functions.php
<?php
/*
* Set post views count using post meta//functions.php
*/
function customSetPostViews($postID) {
$countKey = 'post_views_count';
$count = get_post_meta($postID, $countKey, true);
if($count==''){
$count = 0;
delete_post_meta($postID, $countKey);
add_post_meta($postID, $countKey, '1');
}else{
$count++;
update_post_meta($postID, $countKey, $count);
}
}
?>
And now we will call this function in the single.php to update the count value in the Database.
<?php
customSetPostViews(get_the_ID());//single.php
?>
Now in the same single.php file if we want to show the post view count, we can use this code:
<?php
$post_views_count = get_post_meta( get_the_ID(), 'post_views_count', true );
// Check if the custom field has a value.
if ( ! empty( $post_views_count ) ) {
echo $post_views_count;
}
?>
Now to show all the popular post in the descending order by post view count. use this code:
<?php//popular post query
query_posts('meta_key=post_views_count&posts_per_page=5&orderby=meta_value_num&
order=DESC');
if (have_posts()) : while (have_posts()) : the_post();
?>
<li><?php the_title(); ?></li>
<?php
endwhile; endif;
wp_reset_query();
?>
Happy Coding
There is a function called wpp_get_views provided by the plugin.
If you know the ID of the post/page for which you want to show the view count, all you need to do is to call the function with that ID as the parameter. For example:
$count = wpp_get_views($post->ID);
add_action('init','cr_tbl',55);
function cr_tbl(){
global $wpdb;
$table_add_two = $wpdb->prefix."post_view";
$cat_rating_ddl="
CREATE TABLE IF NOT EXISTS `".$table_add_two."` (
`pvid` INT(11) UNSIGNED AUTO_INCREMENT,
`ip` varchar(255) NOT NULL,
`device` varchar(255) NOT NULL,
`postid` int(11) NOT NULL,
`create_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY(pvid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
";
$all_tables = array();
$mytables=$wpdb->get_results("SHOW TABLES");
foreach ($mytables as $mytable){
foreach ($mytable as $t){
$all_tables[]=$t;
}
}
$sql_one='';
if(!in_array($table_add_two,$all_tables)){
$sql_one.=$cat_rating_ddl;
if(!empty($sql_one)){
if(!function_exists('wp_should_upgrade_global_tables')){
require(ABSPATH . 'wp-admin/includes/upgrade.php');
}
dbDelta($cat_rating_ddl);
}
}
}
function get_the_user_ip() {
if(!empty($_SERVER['HTTP_CLIENT_IP'])){
$ip = $_SERVER['HTTP_CLIENT_IP'];
}elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}else{
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;}
function add_view_record($pid){
global $wpdb;
$ip = get_the_user_ip();
$device = (wp_is_mobile())?'mobile':'desktop';
$table_name = $wpdb->prefix.'post_view';
$list = $wpdb->get_results("SELECT * FROM $table_name WHERE postid = $pid and ip = '".$ip."'");
if(empty($list)){
$wpdb->insert($table_name,
array(
'ip' => $ip,
'device' => $device,
'postid' =>$pid,
)
);
}}
function get_view_count($pid){
$total=0;
global $wpdb;
$table_name = $wpdb->prefix.'post_view';
$list = $wpdb->get_results("SELECT count(pvid) as total FROM $table_name WHERE postid = ".$pid);
if(!empty($list)){
$total=$list[0]->total;
}
return $total;}