How to Add a PHP Page to Wordpress

How can I add a PHP page to WordPress?

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 create a custom PHP hello world page in wordpress?

There are several ways to create custom page templates in WordPress with the documentation found under Theme Development. If you are developing your own theme, you can just use the naming conventions found under the Template Files List, or if you are using a theme developed by someone else you should first create a Child Theme and then add your custom templates under it. You can also make any PHP file under your theme a Custom Page Template by adding a comment at the top of the page. WordPress will automatically detect this file and the template will become accessible via the Page Edit screen in the WordPress Admin.

The short version - add a file called page-custom-template.php to your theme directory (typically /wp-content/themes/YOUR_THEME_DIR/ and add the following comment at the top:

<?php
/*
Template Name: My Custom Page Template
*/

// your code goes here
echo 'hello world';

?>

How to add custom php page in wordpress?

1/ Create custom template

    <?php
/*
Template Name: Your_Template_Name
*/

// YOUR PHP

2/ Create PAGE in Back Office with YOUR_TEMPLATE

OR

https://codex.wordpress.org/Creating_Options_Pages

Insert PHP code In WordPress Page and Post

WordPress does not execute PHP in post/page content by default unless it has a shortcode.

The quickest and easiest way to do this is to use a plugin that allows you to run PHP embedded in post content.

There are two other "quick and easy" ways to accomplish it without a plugin:

  • Make it a shortcode (put it in functions.php and have it echo the country name) which is very easy - see here: Shortcode API at WP Codex

  • Put it in a template file - make a custom template for that page based on your default page template and add the PHP into the template file rather than the post content: Custom Page Templates

How to upload totally custom PHP/web files into WordPress?

I have tested this answer end to end and it works. However, it now transpires that this question relates to Wordpress.com which provides modified/restricted versions of Wordpress - see edit at bottom of answer.

It can all be done with 2 miniscule scripts.

i: "mydummy.php" contains just a single comment on its own line (necessary to "fool" WP into uploading your actual script and archive file).

<?php
/* Plugin Name: mydummy */
?>

ii: "myinstaller.php" e.g.

<?php 
$destinationDir = $_SERVER['DOCUMENT_ROOT'] . '/' . 'myparentdir';
mkdir( $destinationDir, 0775); // 0755 whatever
$zip = new ZipArchive;
$zip->open('myarchive.zip');
$zip->extractTo($destinationDir); // creates dirs and files below as per archive structure
$zip->close();
?>

Tested and works. Add your own error handling & info/progress msgs as required (you are familiar with php).

To upload and run:

  1. Create a zip (e.g. myarchive.zip) of the directories and contained files you want to place on your server.

  2. Place this zip and the 2 script files above in a folder called "mydummy".

  3. Zip the "mydummy" folder" (the result should be "mydummy.zip" containing folder "mydummy" with your archive, myinstaller.php & dummy.php in it.

  4. upload mydummy.zip via Wordpress admin: Plugins->Add New->Upload File. Dont bother activating. Your script and archive are now uploaded to (/maybe-path-to) /wp-content/plugins/mydummy.

  5. run your script http://example.com/wp-content/plugins/mydummy/myinstaller.php JOB DONE

  6. delete plugin, scripts and archive: via Wordpress admin: Plugins find "mydummy" and click its delete link

File perms shouldn't be a problem (I assume server will ensure files are 644 by default etc) the owner will obviously be the same as Wordpress - this should not be a problem for browser access.

Edit:

A common response to Wordpress.com questions is to advise contacting their support; and on a paid plan I would hope you get better answers from them than here. My knowledge of this host is near zero - I vaguely recollect its sites use a restricted variant of Wordpress and that on some(?) plans only specified plugins can be used.

You will need to build it as a WordPress plugin. Otherwise, it won't
work inside WordPress.com's structure.

If they mean the upload will only take place if there is a functioning plugin then try replacing the empty dummy plugin in my first answer with this:

<?php
/* Plugin Name: mydummy
Plugin URI: http://example.com
Description: do nothing plugin
Author: me
Version: 1.0.0
Author URI: http://example.com/
*/
$donothing = 0;
?>:

However; if they mean scripts will only execute if called by "Wordpress" you need to add extract code to plugin but ensure extract only occurs once not every time someone visits the site. In plugin, replace $donothing = 0 with:

function site_perm_function() {
// your code to create html string of dirs (e.g. root and current) + their permissions
// return permission string
}
add_shortcode( 'display_my_site_perms', 'site_perm_function' );

function install_function() {
// your code from myinstaller.php above with code to build message string from statement errors/results
// return message string
}
add_shortcode( 'myinstaller', 'install_function' );

Create a new zip and try installing and ACTIVATING plugin and. If OK:

Open the post editor to create a dummy post.
In the post editor insert the shortcode [display_my_site_perms] then preview the post - this should display permissions.

If permissions look OK for unarchiving then add shortcode [myinstaller] to the post and preview again - this should do the install.

Hopefully it will work, but not tested and I have zero knowledge of Wordpress.com.

How to execute a custom php file with a wordpress website

Wordpress templates are located in wp-content/themes/yourthemename/. Easy way to load a custom php file is to put serve.php in that folder and to make that file a page template by putting comment below at the beginning of the file:

<?php
/*
Template Name: Serve
*/

Now go to admin->pages, create a new page and assign that page template from the template dropdown. Open the http://mywordpresswebsite.com/serve/ url in browser ( click on Show Page in admin bar ) and serve.php will be loaded.

How to insert PHP script into a wordpress without using a plugin

There is two way to do this.

If you want to use PHP code in your widget text editor then you can do that by adding below code in your function.php file.

function php_execute($html){
if(strpos($html,"<"."?php")!==false){ ob_start(); eval("?".">".$html);
$html=ob_get_contents();
ob_end_clean();
}
return $html;
}
add_filter('widget_text','php_execute',100);

Clear cache if you are using any cache plugin and then try to add PHP code to your widget area like:

 <?php echo 'Hello!!!' ?>

If you want to add PHP code in your page or post editor then the best way is to create custom shortcode in function.php file something like:

function list_pages_function( $atts ) {
return wp_list_pages('sort_column=menu_order');
}
add_shortcode( 'output_pages', 'list_pages_function' );

then you can use [output_pages] as shortcode in your page or post editor. See the shortcode API.

Hope you will find this helpful for you.

Thanks.



Related Topics



Leave a reply



Submit