Posted by Simon Goodchild at January 24th, 2012
This is part 4 of the tutorial, moving on from part 3. It uses wps_mail_page_4.php. Remember you should copy this to your themes folder, along with wps_mail_page.css and change your new mail page template (mine is “AA Mail”) on the dashboard’s edit page screen.
Chances are, you will get more than 5 or 10 mail messages in your inbox. We could just show a long list, but that would not be very nice – far better to be able to “page” through the inbox messages, and jump between pages (and jump straight to first and last page).
So we first set up where we are going to start from (which message), and how long our page of mail messages will be:
$start = isset($_GET['start']) ? $_GET['start'] : 0; $page_length = 5;
$start will hold the first message to show (where 0 is the first, 1 the second, and so on). It checks to see if a value has been passed as a parameter in the URL (?start=10 for example … trust me for now, we’ll cover this in a bit), or otherwise sets it to 0 (the first one).
$page_length is the number of messages to show on each page, I’m setting it at 5 for now.
Now, when we get our inbox messages, we pass a couple of parameters (line 92):
$inbox = $wps_mail->get_inbox($page_length, $start);
In other words, how many messages, and starting from which message. Doing just this will show the first 5 messages. So we need to work out which page we’re on, and show the list of pages accordingly.
The first thing we need to do is find out how many messages there are in total, and then calculate how many pages of messages there will be (based on our $page_length):
$inbox_count = $wps_mail->get_inbox_count();
$pages = ($inbox_count % $page_length) == 0 ? $inbox_count / $page_length : floor($inbox_count / $page_length) + 1;
You don’t need to understand the maths, or even the line of code, suffice it to say that $pages will now be the number of pages according to the total count of messages in your inbox.
If there is more than one page (otherwise page numbers are irrelevant), we do the following:
- Get the current page
- If past the first page, show a link to the “First” page
- If past the first page, show a link to the “Previous” page
- Loop through all the pages, either highlighting the current page, or adding a link to the page in the loop
- If before the last page, show a link to the “Next” page
- If before the last page, show a link to the “Last” page
There is a fair bit of number crunching going on, but the links referred to above are the important bits here, for example (from line 156):
echo '<a href="'.$wps->get_mail_url().'?start=0">First</a> ';
The hyperlink is set to the mail page, with start as a parameter set to 0. By effectively re-loading the same page and setting start to 0, we are telling the page to display messages from 0 (ie. the beginning!). Let’s consider something more complicated at line 178:
echo '<a href="'.$wps->get_mail_url().'?start='.(($p*$page_length)-$page_length).'">'.$p.'</a>';
The hyperlink will go to $wps->get_mail_url().’?start=’.(($p*$page_length)-$page_length). The key here is the parameter that start is set to. It works out the first message on the page the loop is up to, that’s what (($p*$page_length)-$page_length) is doing!
For example, if as we loop through the number of pages ($pages), let’s say we get to page 4 (so $p = 4), and we are working with a $page_length of 5 messages. Start will then be set to ((4*5)-5), or 15. The fifteenth message would be the first message shown, and would be the first message on page 4 (remember, page 1 would be 0, page 2 would be 5, page 3 would be 10 and page 4 would be 15).
And we have paging, awesome! :)
Next step, add a compose new mail form and sending the mail.