Limit WordPress Revisions

This little method right here is something I employ on all my WordPress websites. There are plug-ins that can do this for you, but it is such a simple code edit that there’s no real reason to have an extra plug-in for it.

If you want to see how many revisions you have for your posts/pages, go to the editor for one post/page. If revisions are not displayed under the text editor, then select the appropriate check box from the “Screen Options” drop-down in the upper-right of the WordPress Dashboard.

By default there is no limit on the amount of revisions so any limit you impose is going to help keep your WordPress database more efficient. We’ll be using the WP_POST_REVISIONS setting in our wp-config.php file to accomplish this.

In this example we’re going to set our max amount of revisions per page or post to 3. So in total there will be 4 entries in the database per page or post, 1 for the original itself, and then the 3 latest revisions.

Step 1. Login to your cPanel.
Step 2. Access the cPanel File Manager Code Editor and navigate to your wp-config.php file.
Step 3. Enter in the following code towards the top of the wp-config.php file:

/*
 @package WordPress
 */

define('WP_POST_REVISIONS', 3);

After that click on Save Changes at the top-right.

I use FTP to get, edit, and put the wp-config.php file. Use whatever works for you. That block of code is the important bit.

Excerpts from: Limit or disable WordPress revisions – InMotion Hosting

More on Dates in PHP

I ran into quite a few blips when I tried to post some dates to the MySQL database. Very often,  I got all zeros input into the database. Then, it showed the wrong dates (1969 or something). The following code is what I arrived at after a lot a research, a call to my Mom for more research, and a bit of testing. I’m thrilled to finally have this working!

To start off with, your date must be in YYYY-MM-DD format. For me, that was simple. Just change the initial session variable.

 $_SESSION['tdate'] = Date("Y-m-d");
 //or for testing
 $_SESSION['tdate'] = "2012-09-12";
 $fv = array();

 //$fv (form values) array filled from $_POST data and validated //then //execute INSERT INTO with date
 $insert_vars = "Fname, Lname, `date-regis`";
 $insert_vals = "'" . $fv['fname'] . "', '" . $fv['lname'] . 
 ", '" . $fv['regdate'] . "'";

 $isql = "INSERT INTO " . $mydatabase .
 "(" . $insert_vars . ") " .
 "VALUES (" . $insert_vals . ")";

 $iresult = mysql_query($isql, $con);

 if(! $iresult )
  {
    die('Could not enter data: ' . mysql_error());
  }

 //lastly, I wanted to display the date a bit differently
 $datetodisplay = date("m/d/Y", strtotime($row['date-regis']));

A few things to note about the above code.

  1. I’ve learned to never use hyphens in MySQL column names a little too late. Thankfully, the back tick (`) helps keep me from having to reinvent the wheel (see the column `date-regis`).
  2. The date in YYYY-MM-DD format is insert into the database enclosed in single quotes (‘) just like a string.
  3. The last line of code there can be used to initialize your dates into the proper format for the databases, if need be.

As the title suggests, I’ve written on dates in PHP before. That post is all about comparing dates.

Redirecting in PHP

I mistakenly thought redirecting the user would be a simple and well documented process in php. NOT! I’ll get right to what actually works.

function redirect($url) {
 if (strlen($url) > 0) {
 if (headers_sent()) {
 echo "<script>document.location.href='" . $url . "';</script>";
 echo "<noscript><meta http-equiv="refresh" content="0;'" . $url . "'" /></noscript>";
 }
 else {
 header("Location: " . $url);
 }
 exit();
 }
 }

The above block of code is placed in the header, and it contains 3 methods of redirection. I doubt the php “header()” option will work at all, but I left it in for completeness. header() has to run before ANYTHING else on the page does, and no matter how I put that in, it did not work in a situation where I needed to pass it $url.

The preferred method is to use that tiny bit of JavaScript, and I put META refresh in there as a backup for the odd situation when JavaScript is disabled.

Credit where credit is due:

Comparing Dates, Learning PHP

I’m learning a lot of PHP here recently. I’ve never programmed in it before, but it is a fun little language. The nuances of the syntax are interesting sometimes – very typo prone! I’ve found several sites invaluable in getting the hang of the syntax, but I only recently thought to put them in here. If I find more, I’ll add them in later!

#1: I’ve referenced w3schools a lot over the years for a lot of different langues. It is a fantastic site for basic reference.

The useful site that spawned this post is actually where I found a simple method for comparing dates utilizing the strtotime function. I love how he did it “the hard way” first. That is totally how I was planning on doing it, and why I was searching for an easier way!
http://www.highlystructured.com/comparing_dates_php.html

Reading the post is worthwhile, but sometimes I just want to skip to the code. The following is a sample of the code I wrote based on Mike D’Agostino’s helpful writing.

$regopen = 0;
//convert dates to comparable formats
$date1 = strtotime($startdate);
$date2 = strtotime($earlydate);
$date3 = strtotime($enddate);
$today = strtotime($todaydate);
//now to compare - it's so easy its beautiful
If (($today >= $date1) AND ($today <= $date3)) {
$regopen = 1;
   //early pricing or regular?
$cost = $costreg;
If ($today <= $date2) $cost = $costearly;
}