How to Create A Standalone WordPress Custom PHP Page

Posted in Wordpress Development on 5 August 2011 5 comments

The term “Standalone” here refers to a page that run using WordPress engine but does not display using your WordPress theme. It means that your page can access all PHP functions in WordPress but you get to decide how the page looks like. The good news is, to do this using WordPress is amazingly easy.

Loading WordPress Engine

First thing to do is to load the WordPress core engine file, wp-load.php. Depends on where you want to put your custom page, you just need to use require_once(‘wp-load.php’), of course point it to correct folder if your custom page is in different folder.

In this example, i put my custom page in my WordPress root folder. Enter this in your custom page:

<?php
 
require_once('wp-load.php');
 
//if you put in "your-wordpress-root/wp-content/" folder , prepend with "../" to go "1 folder up"
//require_once('../wp-load.php');
 
?>

Congratulations, you just created a standalone WordPress custom PHP page. Isn’t that easy?

Making the page Admin-Only

Very often you need limit access to your page to your blog admins only. You can check if the user is an admin after the require_once() statement:

<?php
 
require_once('wp-load.php');
 
if ( !is_user_logged_in() && !current_user_can('manage_options')) {
die('Please login as admin to use this page.');
}
 
//your page content here

As we said previously, after we loaded wp-load.php, we then have access to all WordPress functions, that is why we can use is_user_logged_in() and current_user_can() to check the user.

“manage_options” is the capability we used to check if the user is admin because all admins can “manage_options”. For more capabilities other than just “manage_options”, please refer to the WordPress Codex on Roles & Capabilities.

Hope this helps :)

 

Posted by Zen on 5 August 2011 • 38,450 visits 5 comments
Tags :


or Subscribe to specific category only :




  - 5 Comments


Rastavarian says:

took me 2 hours until i find this solution :D
for me : require_once(‘../../../wp-load.php’);

thank you !

Matt says:

Hi,
I’ve been using this method, however when i go to my player (http://www.worldmusicradio.com/player.php) the title is “Page not found”, do you know how to fix this?
Cheers,
Matt

parapharmacie en ligne says:

Great, works like a charm. I use it to make a special landing page with a custom form! Thanks a lot!

Trackbacks

  1. How to Create A Standalone Wordpress Custom PHP Page | ZENVERSE
  2. Permalink da script esterno - AlterVista

Leave a Reply

You must be logged in to post a comment.

Previous Post
«
Next Post
»