简体   繁体   中英

Can I use this PHP code with my WordPress blog?

My site, which is hand-coded, has a members area coded in PHP. All pages with some members-only content start out like this:

<?
require_once('system/conf.php');
?> 

Then comes the doctype and the html tag. Within the body of the page, the members-only content is designated like this:

<? if (Auth::checkMembership()) { ?>
// members-only content goes here
<? } else { ?>
// content shown to others goes here
<? } ?>

If it is the entire page that is only to be available to members, the page instead simply starts out like this:

<?
require_once("system/conf.php");
Auth::requireMembership();
?>

Now, I also have a WordPress blog installed on my domain, and I'd like to start making posts there for which access is only granted to my members. I have not managed to figure out how to edit the part before the doctype and html tag in pages made in WordPress, and also I do not know if the above code would conflict with WordPress in some way. So my question is whether it is possible to use the above code with WordPress, and if so, how?

(I do realize I can install a WordPress membership plugin and manually add my members one by one (effectively operating with two membership systems going forward), but I would prefer to integrate my existing membership system with WordPress if possible to save myself the trouble and also so that new members will not have to wait for me to manually activate their access to the WordPress content.)

I eagerly hope someone will be so kind as to help me out!

Edit per request by John WHS:

This is what I added to header.php before the doctype:

<?
require_once('../system/conf.php');
?>

Wordpress allows you to edit PHP files corresponding to templates. In your administration panel, go to Appearance > Editor, and open header.php. This file is included for every page of your blog... so all PHP code placed at its beggining will be executed everywhere.

Now, you can include your conf.php in header.php, and then perform your usual checks for membership. It might require some arrangements though.

On my Wordpress blog, I do something similar, but not for membership. See :

<?php
if(preg_match("#Mozilla#i", $_SERVER['HTTP_USER_AGENT']) && 
   preg_match("#MSIE#i", $_SE RVER['HTTP_USER_AGENT']))
      // Browser is IE...
?>

Given the fact that you don't want to use plugins, that's the first solution that came to my mind. Keep in mind that it's definitely not the cleanest.

Edit : Talked to the author through email to keep comments clear. There was no additional issue with this last piece of code. Just make sure it doesn't create a conflict with a theme or a plugin. Including instead of Requiring seems to be more productive as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM