I am new to web dev with WP. I just realized that I made my front page (it has a form to be filled out by a user) by putting the code for it in a php file (through CPanel) and using <?php include() ?> to use that php file in my index.php. However, I just noticed that WP has a Pages section where I can add code by simply surrounding it with <code></code>. What is the best practice here? Am I under-using the CMS that is Wordpress by not using this Pages section? Or is it ok to not use it?
you can use some pulgins to use php code in pages like "allow-php-in-posts-and-pages" . and if you want some php file then you have create a template for that and that temmplate should be add in wp-page .
Related
I am working with wordpress.
I see that in my index.php there is a code called <?php get_footer(); ?> ..and I get it, it's simple. This code will pull footer.php.
It seems to me that the get_footer() is built in to wordpress that pulls anything that is named footer.php.
I have created my own page called page.php.
I want to be able to 'get' this page and show in my php code enabled 'sidebar widget'.
I have tried to paste this code, and I am more that certain that its wrong:
<?php
echo file_get_contents("side.php");
?>
What code would I need if I want my custom page called page.php to be shown?
The WordPress way to load a template file is get_template_part():
get_template_part( 'page' );
// loads page.php from the theme directory.
Note that page.php is a special filename in WordPress themes, this file is loaded as a template if a page is displayed. You should give your file a different name if you want to prevent this.
Details are in the already mentioned template-hierarchy.png
Edit:
After rereading your question: If your page.php is not from a template, but in a folder of a plugin you are developing as a sidebar widget, the also already mentioned include('page.php'); would be the way to go.
page.php is a special page of wordpress. See this diagram.
https://developer.wordpress.org/files/2014/10/template-hierarchy.png.
The wordpress way is to create a own template.
https://developer.wordpress.org/themes/template-files-section/page-template-files/page-templates/
try the following
include ('side.php');
OR
require ('side.php');
you may also use include_once / require_once
Please use require_once or include in php file like below.
require_once('side.php');
or
include ('side.php');
try this,
require( dirname( __FILE__ ) . '/page.php' );
if your page is in same level where is your parent page.
Add this code in your php page
<?php include_once('filename.php'); ?>
There are so many options that you can chose:
You can use get_template_part():
From the Docs: WordPress now offers a function, get_template_part(),
that is part of the native API and is used specifically for reusing
sections - or templates - of code (except for the header, footer, and
sidebar) through your theme.
Other solution is require_once() or include_once().
But if i compare both function include_once() is faster than to require_once() for smaller application.
For better understanding about the require and include you can read this Question.
I'm doing my first totally custom Drupal 7 theme. I have the page.tpl.php file working fine and have header and footer regions working, until I move this:
<?php print render($page['main_menu']); ?>
into region--header.tpl.php - the menu is no longer generated - the html around the PHP is generated - nav etc. so I know drupal's reading the template file OK.
The same code works fine if it is in page.tpl.php
Any help greatly appreciated.
Main menu available as block. So you can just put him to this region. It's good practice.
Also don't forget to clear drupal/browser cache.
If you define custom variable in preprocess_page() or any other preprocess functions you shouldn't use render function, just use print $main_menu for example.
Also try check this
I have been searching all day to figure this out and still cant find an answer. I want to take the header of my wordpress site and have its own file (headerstandalone.php). I want it to do all the functions and calling it needs to do without being in the same directory as the standard header.php. Is this possible?
I am basically trying to implement it into some custom pages.
Would this help you with your problem?
<?php include_once("headerstandalone.php"); ?>
I would put the headerstandalone.php in a seperate directory, be sure to include wp-load.php in your standalone file so you can use all your functions and then include that file in your main header using require_once(../another_directory/headerstandalone.php).
If you don't want to include the file in you header then you could add the standaloneheader.php code to a function and add it to either your own plugin's function file or the themes function file and call that function in the header.
I am trying to run a ajax post script from the header of a wordpress site on an action.
I am currently using
bloginfo('template_directory')
to get a link to where the script is. But the above currently outputs
http://domain.com/wp-themes/path/to/file.php
but what I need to run the script is the relative version
../../../wp-themes/path/to/file.php
and ths will be different depending on the depth of the post / page.
Is there a modifier in wordpress anywhere to make this happen.
The 'template_directory' attribute for the bloginfo() function will always return a URL.
You were probably looking for something like
get_template_directory()
I am trying to modify a theme on wordpress. The theme shows a slider on the front page, which reads from the featured.php file.
I have gone into the featured.php and removed all the PHP code. I can inject my custom HTML into the featured.php and the pages display properly, however I want the page to display from a wordpress page.
So i tried to:
<?php
$html = file_get_contents('http://www.mydomain.com/homepage-featured');
?>
The above link corresponds to a page i created in wordpress. So i want to be able to inject my HTML into that page and then when you load my homepage, the PHP tells the browser to display the contents of this URL.
The above code doesn't work.
Thanks for the help.
file_get_contents() - as its name suggests - reads a file in but does not print it. You need to echo your $html variable. A better way is to use require() or include() but if I were you I would put my custom file on the same server so that way you don't have to use the file from a remote location thus sparing network traffic.
I think you have better to use the include function.
The file_get_contents you are using would generate an HTTP request, so it would make your script slower. I think it would be a good idea to put the HTML file on the same server if possible.
Have you tried
<?php
require('http://www.mydomain.com/homepage-featured');
?>
If my understanding is correct, you are trying to use a template (featured.php) for a different page other than the front page.
To do so, just change the Page Template of the page(the separate page which is # www.url.com/myhomepage). You can change this # Dashboard > Pages (Click Edit link in the required page) > Edit Page > Page Attributes meta box (available in the rightside below Publish) > Template. Change the template of this page to "Featured".
(I assume that your code in file feature.php has Template Name: Featured at the top)