How to add a PHP page to WordPress?

I want to create a custom page for my WordPress blog that will execute my PHP code in it, whilst remaining a part of the overall site CSS/theme/design.

The PHP code will make use of 3rd party APIs (so I need to include other PHP files)

How do I accomplish this?

NB I do not have a specific need to interact with the Wordpress API - apart from including certain other PHP libs I need I have no other dependencies in the PHP code I want to include in a WP page. So obviously any solution that didn't require learning the WP API would be the best one.


You don't need to interact with the API or use a plugin.

First, duplicate post.php or page.php in your theme folder (under /wp-content/themes/themename/ ).

Rename the new file as templatename.php (where templatename is what you want to call your new template). To add your new template to the list of available templates, enter the following at the top of the new file:

<?php
/*
Template Name: Name of Template
*/
?>

You can modify this file (using PHP) to include other files or whatever you need.

Then create a new page in your WordPress blog, and in the page editing screen you'll see a Template dropdown in the Attributes widget to the right. Select your new template and publish the page.

Your new page will use the PHP code defined in templatename.php


If you wanted to create your own .php file and interact with Wordpress without 404 Headers and keeping your current permalink structure there is NO Need for a Template file for that 1 page, I found that this approach works best, in your .php file:

<?php
    require_once(dirname(__FILE__) . '/wp-config.php');
    $wp->init();
    $wp->parse_request();
    $wp->query_posts();
    $wp->register_globals();
    $wp->send_headers();

    // Your Wordpress Functions here...
    echo site_url();
?>

Than you can simply perform any wordpress functions after this. Also, this assumes that your .php file is within the root of your wordpress site where your wp-config.php file is located.

This, to me, is a PRICELESS discovery as I was using require_once(dirname(__FILE__) . '/wp-blog-header.php'); for the longest time as Wordpress even tells you that this is the approach that you should use to integrate Wordpress Functions, cept, it causes 404 headers, which is weird that they would want you to use this approach. https://codex.wordpress.org/Integrating_WordPress_with_Your_Website

I know many people have answered this question and it already has an accepted answer, but here is a nice approach for a .php file within the root of your wordpress site (or technically anywhere you want in your site), that you can browse to and load without 404 Headers!

EDIT

Just a quick update here. There is a way to use wp-blog-header.php without 404 headers, but this requires that you add in the headers manually, something like this will work in the root of your wordpress install:

<?php
    require_once(dirname(__FILE__) . '/wp-blog-header.php');
    header("HTTP/1.1 200 OK");
    header("Status: 200 All rosy");

    // Your Wordpress Functions here...
    echo site_url();
?>

Just to update you all on this, a little less code needed for this approach, but it's up to you on which 1 you use.


If you're like me, sometimes you want to be able to reference WordPress functions in a page which does not exist in the CMS. This way, it remains backend specific and cannot be accidentally deleted by the client.

This is actually simple to do just by including the wp-blog-header.php file using a php require() .

Here's an example that uses a query string to generate Facebook OG data for any post.

Take the example of a link like http://example.com/yourfilename.php?1 where 1 is the ID of a post we want to generate OG data for:

Now in the contents of yourfilename.php which, for our convenience, is located in the root WP directory:

<?php
    require( dirname( __FILE__ ) . '/wp-blog-header.php' );

    $uri = $_SERVER['REQUEST_URI'];
    $pieces = explode("?", $uri);
    $post_id = intval( $pieces[1] );

    // og:title
    $title = get_the_title($post_id);

    // og:description
    $post = get_post($post_id);
    $descr = $post->post_excerpt;

    // og:image
    $img_data_array = get_attached_media('image', $post_id);
    $img_src = null;
    $img_count = 0;

    foreach ( $img_data_array as $img_data ) {
        if ( $img_count > 0 ) {
            break;
        } else {
            ++$img_count;
            $img_src = $img_data->guid;
        }
    } // end og:image

?>
<!DOCTYPE HTML>
<html>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=yes" />
<meta property="og:title" content="<?php echo $title; ?>" />
<meta property="og:description" content="<?php echo $descr; ?>" />
<meta property="og:locale" content="en_US" />  
<meta property="og:type" content="website" />
<meta property="og:url" content="<?php echo site_url().'/your_redirect_path'.$post_id; ?>" />
<meta property="og:image" content="<?php echo $img_src; ?>" />
<meta property="og:site_name" content="Your Title" />
</html>

There you have it: generated sharing models for any post using the post's actual image, excerpt and title!

We could have created a special template and edited the permalink structure to do this, but since it's only needed for one page and because we don't want the client to delete it from within the CMS, this seemed like the cleaner option.


EDIT 2017: Please note that this approach is now deprecated

For WP installations from 2016+ please see https://stackoverflow.com/a/39800534/1958998 for extra parameters to include before outputting your page data to the browser.

链接地址: http://www.djcxy.com/p/69350.html

上一篇: 通过零分错误投票

下一篇: 如何添加一个PHP页面到WordPress?