How can I add a PHP page to WordPress?

execute php in wordpress pages without plugin
how to call php file in wordpress page
wordpress blank page template
how to make php website in wordpress
how to add php code to wordpress post or page
insert php wordpress plugin
wordpress php
custom page template wordpress plugin

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 third-party APIs (so I need to include other PHP files).

How do I accomplish this?

N.B.: I do not have a specific need to interact with the WordPress API - apart from including certain other PHP libraries, I need I have no other dependencies in the PHP code I want to include in a WordPress page. So obviously any solution that didn't require learning the WordPress 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

Source: Creating Custom Page Templates for Global Use

How to Add PHP Code to A WordPress Post or Page, You can name your file "newpage. php" - put it in your theme directory in wp-content . You can make it a page template (see http://codex.wordpress.org/Pages) or you can include it in one of the PHP files in your theme, such as header. php or single. How to Add PHP Code to WordPress Post or Page Using a Plugin. From your WordPress dashboard, navigate to XYZ PHP Code -> PHPCode Snippets . In the PHP Code Snippets page, click Add New PHP Code Snippet . Add your desired PHP function and its tracking name to the appointed boxes. In If the

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 one 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();
?>

Then 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, except, it causes 404 headers, which is weird that they would want you to use this approach. 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!


Update: 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 installation:

<?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 one you use.

How can I add a PHP page to WordPress?, In this video tutorial we look at how to add PHP to any page within your Wordpress website Duration: 6:08 Posted: Jul 17, 2015 The recommended way to add PHP functions is to modify the child theme or create custom page templates. Despite so, there are occasions where you might want to add specific PHP functions to an individual post or page. In these cases, you can override the rule using WordPress code widget plugins. The following tutorial will guide you through the

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 Open Graph (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 WordPress 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 WordPress installations from 2016+ please see How can I add a PHP page to WordPress? for extra parameters to include before outputting your page data to the browser.

How to add PHP to a Wordpress page, Your custom page template is like any other theme file in WordPress. You can add any HTML Duration: 3:45 Posted: Mar 1, 2019 Go to Posts and click, “Add New.” In the visual editor for WordPress, you’ll see a new button. Click the “PHP” from the editor tool bar. Your list of currently saved PHP code snippets will be visible from this button.

Creating the template page is the correct answer. For this, just add this into the page you created inside the theme folder:

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

For running this code, you need to select "mytemplate" as the template of the page from the back end.

Please see this link for getting the correct details https://developer.wordpress.org/themes/template-files-section/page-template-files/.

How to Create a Custom Page in WordPress, What you can do is this: Put up.php in your active theme's folder, and put this line at the top of your up.php file: <?php /* Template Name: Up */ ?> Create a page  Since this is a my page, you NEED TO CREATE A PAGE from WordPress admin panel. Go to Admin => Pages => Add New. Add a page title, depending upon how you have coded the custom page, you might add page body (description) as well. You can fully skip the description if it’s written in the custom php page. At right hand side, select Template.

Any answer did not cover if you need to add a PHP page outside of the WordPress Theme. This is the way.

You need to include wp-load.php.

<?php require_once('wp-load.php'); ?>

Then you can use any WordPress function on that page.

How to add a .php file to WordPress, If you want to add custom page in WordPress, you need to add your custom PHP file into currently activated theme ( /wp-content/themes/ ) folder. Suppose you  To do this, simply click on Allow PHP in Posts menu option. Click on the Code Snippets Tab. Add your PHP query there. Then in your post, simply use the shortcode with the Snippet ID:

How to create custom php script page, But if you're looking to include a post or page-specific element with PHP, you can do so with snippets and shortcodes. Today, I'm going to go over  I read you need to add php code to a page and insert the shortcode of that page in an elementor widget but I couldnt find out what the shortcode is. You cannot add PHP code to Elementor, since this would pose a security risk for websites. You can create shortcodes that run PHP and add the shortcode to Elementor.

How to Insert PHP Code into WordPress Posts and Pages , php - put it in your theme directory in wp-content. You can make it a page template (see http://codex.wordpress.org/Pages#​Creating_Your_Own_Page_Templates)  In this video tutorial we look at how to add PHP to any page within your Wordpress website. To read the full article go to: http://www.atinyrhino.com/blog/wo

How to create a custom PHP page in WordPress, How To Safely Add PHP Code To WordPress in plugin pages (although you can also do this with MU plugins if you add this header); Shared across all themes. Click the “Upload/Insert” button to launch the Add Media dialog. Drag and drop the desired image into the dialog or click “Select Files” and double-click the image from its location on your computer to upload it into your WordPress page. Repeat this step for each image or other multimedia file you want to migrate into this WordPress page.

Comments
  • you can use those plugins : wordpress.org/extend/plugins/exec-php or wordpress.org/extend/plugins/php-code-widget Hope it will help!
  • Props to @adam -- this is the simpliest, easiest, and fastest way to do it.
  • The template option wasn't available until I created the file with the comment in it. Then the template option appeared. Nice - I was going to use shortcodes but this is way easier.
  • Is there any downside of using a plugin for publishing php ?
  • @DamianGreen the question doesn't ask for a fully automated way, and in fact shows a preference for avoiding the WP API
  • This answer feels wrong because it is not re-using the existing theme elements, but copy-pasting them (paragraph 2). If the theme is provided by a third party then it may receive theme updates that will not cascade through to a custom template. Further it adds complexity to changing the look and feel of your site, changing theme requires a rebuild of the custom page. Would a plugin not be better for these two reasons? The way I read the question, the OP wishes to add additional functionality, not additional theme elements "use of 3rd party APIs"
  • Yet 404 Headers occur, and even Wordpress multisite file in wp-activate.php has to disable 404 header by setting $wp_query->is_404 = false when activating new multisites using require( dirname( __FILE__ ) . '/wp-blog-header.php' ); at the top of this page.
  • i don't understand how you can not recreate the problem. It's very simple to recreate. Just install Wordpress, than create a .php file, use the wp-blog-header.php approach as defined on codex.wordpress.org/Integrating_WordPress_with_Your_Website than see the 404 header in your browser inspector tools.
  • Here's a webpage that documents this problem also, in case you missed it during your intensive research on this problem: cooltrainer.org/…
  • The header issue is an issue that happens in the latest versions of WordPress, which is exactly what I suggested in my initial comment. Careful using words like 'never fails'. :)
  • This is a very improper approach to a problem. By going this route, you are creating a solution that will only work so long as WP doesn't change its bootstrap. You should be leveraging hooks available to accomplish (easily) what this is trying to do.
  • This idea was very useful to me. However check out this post about using require_once('wp-load.php'); correctly