How to count visitor in wordpress website or php

How to count visitor in wordpress website or php

php:

Firstly create a database name like 'mydb' and create a table 'Counter'. Then table stacture here in example below:

CREATE TABLE `Counter` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`visits` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

You have to create 'id' is not null primary key and create 'visits' in just not null. then insert value like 1 number.

Better understand see screenshot.




Then connect database look like code below:

    $servername = "localhost";
$username = "root";
$password = "";
$dbname = "mydb";
$conn = new mysqli($servername, $username, $password, $dbname);  

Then check db connect is right or wrong.

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}


Then update table of visitor column on row number 1.

$sql = "UPDATE Counter SET visits = visits+1 WHERE id = 1";
$conn->query($sql);

Show the table row query:

$sql = "SELECT visits FROM Counter WHERE id = 1";
$result = $conn->query($sql);  

Check Data is found or not:   

if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$visits = $row["visits"];
}
} else {
echo "no results";
}

DB close:

$conn->close(); 

And finally show output on HTML format:

<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>Visit counter</title>
</head>
<body>
Visits: <?php print $visits; ?>

</body>
</html>

Full code for PHP:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "wp";
$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "UPDATE Counter SET visits = visits+1 WHERE id = 1";
$conn->query($sql);

$sql = "SELECT visits FROM Counter WHERE id = 1";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$visits = $row["visits"];
}
} else {
echo "no results";
}
$conn->close();
?>

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>Visit counter</title>
</head>
<body>
Visits: <?php print $visits; ?>

</body>
</html>    

You can create same thing on wordpress website to working on to make wp plugin:    
    
WordPress:  

Firsly you have to follow same thing on database connect same as above i describe on php.

Then you have to create a folder on wp plugins folder and create a index.php or something name as like to create.

Then paste the full code:

<?php
/*
Plugin Name: Counter Visitor
Plugin URI:
Description:
Version: 1.0
Author: Md. Shahinur Islam
Author URI:
*/
function counterr_shortcode_wrapper($atts) {
ob_start();
?>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "wp";
$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "UPDATE Counter SET visits = visits+1 WHERE id = 1";
$conn->query($sql);

$sql = "SELECT visits FROM Counter WHERE id = 1";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$visits = $row["visits"];
}
} else {
echo "no results";
}
$conn->close();
?>

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>Visit counter</title>
</head>
<body>
Visits: <?php print $visits; ?>

</body>
</html>
<?php
return ob_get_clean();
}
add_shortcode('counter_visitor_view','counterr_shortcode_wrapper');

Finaly you have to show this counter on pages. Copy the shortcode you have to create [counter_visitor_view] of any page to paste and then visit website to see reflection.

Note: If you have to face any difficulties, please comment first or contact to me. I will help to this make. Don't be hesitation.


Full Source code on github

How to count visitor in wordpress website or php How to count visitor in wordpress website or php Reviewed by Md Shahinur Islam on August 29, 2022 Rating: 5

No comments:

Powered by Blogger.