Wordpress: Counter for page views to be inserted in a different page - wordpress

I have been looking for a plugin in wordpress that allows me to insert a page view counter in a different page: I´ll explain;
In page A I have buttom with a contact form to vote a video (it is a video contest). This contact form sends the voter an email with a link to page B so that they can validate their email this way. Every visit to page B will be counted as a vote.
The thing is that I want to count the number times that page B is accessed to show it in page A (to show the number of votes next to the video).
It seems pretty straightforward but I can´t find an easy way to do this. Do you think you could help me?
Thanks!
Guzmán

If a very small number of people will access the page you can just store a value in WordPress and add to it each time. If you expect a lot of users on the page at once you may need a more complex method.
For a lightly trafficed page, with the risk that some visits might not be counted you could leverage the post meta and simply store a counter variable. If two people viewed the page at the same time they could get the same value for the counter variable and so they would not properly increment the variable.
You could add this to the template for the page in question:
$vcount = get_post_meta($post->ID, "view_counter");
add_post_meta($post->ID, "view_counter", $vcount+1, true);
So two people accessing the page would end up with equal $vcount variables. To display the count you'd just need the post id for the post you saved the meta to: print get_post_meta(<POSTIDHERE>, "view_counter", true);
If you need to have a more robust solution, you'd want to store a unique value for each visitor and then to report the total you would count the values and display it. So you could store the visitor's IP address as a multi-value meta:
add_post_meta($post->ID, "view_counter", $_SERVER['REMOTE_ADDR'], false);
Then to display the count you'd need to count the number of post meta values stored with the view_counter key:
$ips = get_post_meta(<POSTIDHERE>, "view_counter", false);
$views = count($ips);
print "Total views: $views";
This is just a sample solution, you should probably check to see if an IP already exists and consider throttling individual IPs. You could also risk filling your database if you have a small shared server unless you write some additional code to keep this from being abused since it adds data to your database on every page view. Finally this will not work correctly if you have any type of caching enabled.
Depending on the plugin that you are using for your contact form you may also be able to report the number of times that the contact form was submitted by querying the database directly.
For more information see the documentation for add_post_meta and get_post_meta
(disclaimer, code written from memory and documentation, not tested)

Related

Inserting a custom handler anywhere in a wordpress URL

I have some custom posts that I need to be able to list and link to at various points around a site.
They are case studies and I want to make them contextually relevant to services, which are currently just pages.
Ideally I want to be able to insert 'case-studies' into a URL, have it recognised and then either list relevant posts if there is nothing following it, or display the post based on the slug that follows case-studies.
EG:
/service1/case-studies - lists all case study posts relevant to service1
/service1/case-studies/blah displays the 'blah' case study post
In the above service1 is just a page. The URL might even be
/service1/subservice/case-studies
/service1/subservice/case-studies/blah
I'm thinking that relationships will handle the relevancy - so a case-studies listing will look up all case studies related to the slug preceding 'case-studies' in the url (so in these cases subservice or service1)
I've looked at wp-router (https://github.com/jbrinley/WP-Router) but I'm not sure how I can use the slugs preceding the case-studies marker to query for relevance.
Anyone had any luck with useful URL routing in Wordpress?

Generate and display html table with user data from wordpress

Update: With the help of the answerer below I figured out how to do this. Basically I used a WP Query to get users and their user metadata and I sorted and placed the data in a for each loop in a table.
I'm new to PHP and I need some help. Basically I want to create a leaderboard with different user data. I want to display it in an html table.
This is kinda what i want:
Username GamiPress Points Time since last login
And I want the table to be populated with these data amongst others automatically. I want two versions one that is sorted with the 15 users who have the most points and one that sorts on the 15 users that logged in last.
Can someone point me to the right place on how I can best implement this?
I basically want to create the GamiPress Leaderboard add-on that I, unfortunately, can't afford, but with some extra fields.
There are multiple steps to do if you want to achieve this:
1) Add meta field to your users, so you can store the points. For example you can use "Advanced Custom Fields" plugin for this.
2) Write a function for adding points to this field. Define when this function will be fired.
3) Query the users ordered by that meta value and display it (get_users($args) might be useful).
4) For the Last-Login value you can use a plugin (google Wordpress Last Login) and write another Query and order results by that meta field. You can also write this by your own, here is a link I found: https://www.wpbeginner.com/plugins/how-to-show-users-last-login-date-in-wordpress/
I don't know if this is what you were looking for.
Or did you want to see an example code how you use a wp query and display data in html table?

Wordpress | generate unique link

I am turning to Stackoverflow community for a need of critical advice.
It's simple, I want a person to generate a link... and that link will have a button where whoever it is sent to, that person can click yes/no - then return the value.
user 1 > generate link > user 2 > options > return option to user 1
Please can anyone point me in the right direction?
Thanks very much!
All the code you need is in wp-login.php
Generate unique URL (for password reset)
Email URL (or you could just display it)
Set user profile metadata (so you can track it)
If you want to track individual users, then you can either generate a unique url per user, per authored page or perhaps simpler is generate one unique url per page, but check that the user is logged when the unique link is clicked.
Either way you can use that to prevent multiple votes etc

Pagination in application which use firebase.com as database

Front end application will use firebase.com as database.
Application should work like blog:
Admin add / remove posts.
There are "home" page which show several articles per page with "Next" and "Prev" buttons and page which show single article.
Also I need deep linking.
I.e. when user enter URL http://mysite.com/page/2/ I need that application show last articles from 10 to 20, when user enter URL http://mysite.com/page/20/ application show last articles from 200 to 210. Usual pagination.
Main question - is it possible to achieve this if use firebase.com.
I read docs at firebase.com, I read posts here. "offset()" and "count()" will be in the future, use priority, in order to know count of items and not load all of them use additional counter.
Based on this I suppose this:
ADD POST action:
get value of posts counter
set priority for new post data equals to value of posts counter
increase value of posts counter
DELETE POST action
get value of priority for post data
query posts data which have value of priority more than priority for post data which will be deleted and decrease their value
decrease value for posts counter
GET POSTS FROM x TO y
postsRef.startAt(null, x).endAt(null, y).on(... and so on)
Thing which I not like in this way are actions for DELETE POST. Because index of posts will be from 0 to infinity and post with less priority is considered as post which was created earlier so if I have 100000000 posts and if I need to delete 1st post then I need to load data for 99999999 next posts and update their priority (index).
Is there any another way?
Thanks in advance, Konstantin
As you mentioned, offset() will be coming, which makes your job easier.
In the meantime, rather than using startAt and endAt in combination, you could use startAt and limit in combination to ensure you always get N results. That way, deleted items will be skipped. You then need to check the last id of each page before moving to the next to find the proper id to start on.

How to create a form with variable no. of field set

I have to design a page for user information, for some background verification purpose, at my work. I need a set of fields for address, total count of which will be selected by user to update the form with that many fields. So, if user selects 3, form will have 3 set of address fields. Similar concept for work and education details.
Right now, I am passing the count to a handler page, which checks total count, and return it along with querystring, back to the main page. I am able to update the no. of fields, this way, but, all values are lost, once I return to the form. There are a lot of fields to even use session object for every value. Also, it resets the count of other such field set to 0. So, if I select 4 in address field, it renders four set of address fields, but fields for other details are gone.
I need to know, if it is possible to update the fields, using just one page, instead of creating a handler file to handle the redirect, so that I don't lose other data.
Sorry, for sounding a bit confusing. Will update the question, if needed.
Edit:
Similar blocks are there for education and work details. I want the update button to update the block, with that many fields, while retaining the values already entered by the user.
I have finally shifted the update code to one page. And the total count of blocks, is calculated by this way.
if request.form("addresscount") <> "" then
varaddresscount = request.form("addresscount")
else
varaddresscount = 1
end if
varaddresscount is used to loop through the html code which renders address fields. Even with this method, if I click on update button to change the total field count, every value entered by user is reset to default. Is there a way to retain the no. of fields without using session object, as there are way too many fields for which I have to store the value in session.
Why not have just a "add address" button that, whenever clicked, adds a extra set of input boxes using Javascript? That solves a lot of your problems regarding retaining the data on already filled in fields AND it makes it easier for the user.

Resources