Per-Post User Level Restrictions

WordPress 1.2 Per-Post User Level restrictions

(c) 2004 Scott Merrill

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

INTRODUCTION This modification allows blog authors to assign a minimum user level required to view each post. The default value is zero (retroactively applied to all existing posts), which means anyone can see the post. By choosing the post level on the admin post or edit screen, the author can limit who sees what.

INSTALLATION PART ZERO Extract the contents of the file postlevel.tgz into your WordPress directory. No files will be overwritten. The contents of the download file are thus:

PART ONE This modification requires the addition of a new row to the wpposts table, called "postlevel". There are sever al ways to accomplish this: 1) Make sure the included postlevelinstall.php is in your wp-admin directory. Call this page from your browser:

2) Edit the included postlevel.mysql file to correctly identify the name of your wpposts table (only necessary if you're using a custom table prefix for your database tables), then execute the following:    mysql -uusername -p wordpress < post_level.mysql Replace "username" with your database username, as defined in wp-config.php. Replace "wordpress" with the name of your WordPress database, as defined in wp-config.php

3) Use a tool like phpMyAdmin to manually alter the table. The post_level row must be UNSIGNED INT(2), default zero, not null.

PART TWO Next, you need to modify five files:

Again, there are several ways to do this: 1) From the command line, run the patch command in your WordPress directory to automatically modify all the files for you using the included postlevel.diff file:    patch -p0 < postlevel.diff

2) If you haven't modified any of the listed files yourself (to apply other modifications, for example), you can simply copy the files from the modified/ directory to their respective locations:    cd modified    cp -r ./* ..

3) You can look through either the postlevel.diff file, or the files in the modified/ directory to see what chan ges I've made. I've prefaced all of my modifications with this line:    // SDM 23-05-2004 postlevel modification to help you identify what has been changed.

USING THE MODIFICATION Once installed, you'll note that the post and edit screens now have a drop-down select box labelled "Post level". This is the minimum user level required to view the post.


Update 03-06-2004 By default, WordPress grants the ability to create new posts to any user with a user level greater than 0. So restricting posts with this mod and granting visitors user levels high enough to read your restricted posts means that your readers can become writers and blog on your blog. The fix is easy enogh: In wp-admin/post.php, find the line "default:" (this regex -- ^default: -- will take you right there). It's line 721 in my copy. Below that is a line that reads:

if ($user_level > 0) {
Change that zero to any higher number, depending on the number of restricted levels you want to have. If you only want a single level of restricted posts, change the zero to 1. Then raise your privileged readers up to user level 1. They'll see level 1 posts, but not be able to edit.

My sincere apologies for any trouble this oversight has caused.


home / about / flickr / github / keybase / linkedin