Custom PHP code won't show up in Wordpress theme? - php

This has me absolute stumped!
I have a small snippet of PHP code, <?php include('/includes/stuff.php'); ?>. All it does is output an image.
If I put this in the WP themes header.php or index.php files, it will never show up.
Here is the actual code in my the themes index.php file:
<img src="<?php bloginfo('stylesheet_directory');?>/images/content-top.gif" alt="content top" class="content-wrap" />
<?php include('/includes/stuff.php'); ?> TEST TEST TEST //this is the custom code
<div id="content">
But when it actually runs, it looks like:
<img src="http://www.bavarianblue.com/wp-content/themes/Polished/images/content-top.gif" alt="content top" class="content-wrap" />
<div id="content">
It just ignores my code entirely. No errors, no nothing.
The only time I could get it to work is if I included it in an already included file. For instance, in header.php there is an include for a features.php. If I included it there, it worked fine. But I don't want it there, I need it in index.php.
Is there some kind of configuration in wordpress where is needs to know all includes or something? I confirmed that my files are being modified (on the FTP).
EDIT
I just tried get_template_part(), as described on the wordpress codex, to no avail. Here is the snippet I tried:
<?php get_template_part( 'includes/stuff' ); ?>
Jared

Try the full path to stuff.php. Also, your host might be running php in safe mode, so check your file permissions.
Another way to include php functions in WordPress is via the functions.php file in your theme:
http://codex.wordpress.org/Theme_Development#Functions_File

Related

Where's the HTML in PHP template files?

So I am new to web development, and want to begin developing themes for Wordpress.
I am confident in my HTML and CSS skills but I am somewhat stuck on understanding how PHP works specifically for Wordpress.
To get straight to the point, when I download a basic theme from wordpress.org and look inside all of the template files, I don't see any HTML code.
I am familiar with the get function in php and so on, but watching videos/tutorials on theme development has confused me so much.
For example, most of all the tutorials I have watched shows someone copy and pasting HTML code from their static web templates directly into the PHP files. (index.php and so on). It works and I am told that is a correct method of doing it, but I just don't understand why I don't can't see HTML code in wordpress themes I download.
Is there a way of not showing the HTML?
Thanks you in advance...
The html code is inside the PHP file. there are various way to write HTML inside a PHP file. For example
<?php
echo "<html><h1>header</h1></html>";
?>
Save the above code as PHP and run it on the server You will get html output from PHP file.
You can also run it in the following way
<?php
//your first php code here//
?>
<html>
my html here
</htm>
<?php
//your second php code here//
?>
It is the right way to write html code inside a php file. you can't run php code on a html file so the html code should be written on the PHP file.
========question answer==========
this is the theme you have mentioned : https://github.com/WordPress/twentyseventeen
check the index.php file
https://github.com/WordPress/twentyseventeen/blob/master/index.php
you will see
get_header(); ?>
<div class="wrap">
<?php if ( is_home() && ! is_front_page() ) : ?>
<header class="page-header">
<h1 class="page-title"><?php single_post_title(); ?></h1>
</header>
this type of coding there.
<h1 class="page-title"><?php single_post_title(); ?></h1>
Look carefully the line. <h1 class="page-title"> it is a html tag ( it is html code )
You can download the theme on your pc and open the index.php file and others file. you will see html code but it is mixed with PHP.
I would recommend you install WAMP on your localhost.
There are PHP files for Wordpress that alter how it functions but I would not recommend you edit these unless you know what you are doing.
If you install Wordpress on WAMP you will have access to all this and can also set up specific projects on WAMP to develop and test your themes.
There is a good walk through here on setting it up https://premium.wpmudev.org/blog/how-to-set-up-wordpress-locally-for-pcwindows-with-wamp/?utm_expid=3606929-106.UePdqd0XSL687behGg-9FA.0&utm_referrer=https%3A%2F%2Fwww.google.co.uk%2F
Basically what you'll do with Wordpress templates is use HTML to hold the info you get from PHP, such as the page/post title, content, tags, categories, etc. You could do something like
<h1 class="title-class"><?php get_the_title();?></h1>
No there is no way to hide html and there is no point to do that either.
But if you look carefully in wp-content -> themes folder you will see all themes directories.
there you can find html "code". In some case you won't find any .html files, may be.
Reason for it because wordpress theme's all pages contains atleast one dynamic part like header. so to make html page dynamic you need to set .php extension for files instead of .html or .htm.
You will find less html and more php sometimes because mostly wordpress themes coded with reusable snippets and functions who generates some code blocks.
But there is html code blended with php code inside .php files:
for example:
<html>
<head>
<title><?php get_the_title(); ?></title>
<?php get_custom_css_files(); ?>
</head>
<body class="container">
<div class="col-md-8 text-center">
<?php foreach($posts as $post) {
<div class="title"><?php echo post['title']; ?></div>
<div class="desc"><?php echo post['text']; ?></div>
<?php } ?>
</body>
</html>

What is the best and most reliable way to call other templates into page.php in Wordpress?

My Wordpress website uses two push menus, both of which I initially had situated in the header.php file. It wasn't until I started needing to use a lot of PHP code in one of these menus that I decided to have them in separate files and call them into the page structure via PHP commands. Ive been wondering what would be the best way of going about this and would appreciate any advice on whether I'm doing it correctly. Below is the code from the simpler of the two menus – I've saved this as mobile-menu.php.
<div class="mobile-menu">
<nav class="main-menu" id="mobile">
<?php wp_nav_menu( array( 'theme_location' => 'header' ) ); ?>
</nav>
</div>
This isn't a particularly long list of code, but the other menu (shopping-basket.php)is a lot longer as it uses PHP from a plugin I've installed, and I don't want masses of code in my header.
I've then called both menus into my page.php using "include" commands, like I have with the header and footer – for example:
<?php get_header(); ?>
<?php include("mobile-menu.php"); ?>
<?php include("shopping-basket.php"); ?>
Is this recommended? It seems to work okay, but I've noticed that despite saving the files as PHP templates with a .php extension, they're showing up as HTML files when I have a look in my FTP account. Why would this be?
Any advice or info would be appreciated as I can't really find any concrete info on this online and have got to the stage that I'm at by searching through forums etc.
You would be best off using get_template_part() instead of include().
Load a template part into a template
https://developer.wordpress.org/reference/functions/get_template_part/
You should put the contents of mobile-menu.php into templates/menu-mobile.php (Note that I've reordered the name for least- to most- specific) and templates/shoppingbasket.php. Call them as so:
get_template_part('templates/menu','mobile');
get_template_part('templates/shoppingbasket');

including php file in thickbox inline view in wordpress

In one of my wordpress plugins, I am using thickbox function to call another php file in the same plugin directory. The thickbox is displaying content as long as the content is from the same file, if I include another php file in the thickbox, it won't work. Please help.
Here is my code.
<?php
add_thickbox();
include plugin_dir_path(__FILE__) . '/my_php_file.php'; ?>
<div id="cnt-id" style="display:none;">
<h1><?php _e('Select an Item', 'txt-domain'); ?></h1>
<?php print_r($tpl_list);?>
</div>
The thickbox anchor link
<a href="#TB_inline?width=600&height=550&inlineId=cnt-id" class="thickbox">
<?php _e('Open thickbox', 'txt-domain'); ?>
</a>
So when the thickbox page is loaded, it only shows the h1 heading. the array which I am trying to print is just static array created in that included php file.
Any help will be highly appreciated.
Thanks
Just for someone in the similar situation. This is what I found as solution.
I used the absolution URL of the file on my the same location for thickbox with TB_iframe parameter. And I called a separate php file which is including another one with the array data.
That file which I didn't want to be directly accessible, I added the http_referer check via PHP to check if the ref url has wp-admin/post.php in there. That is not a rock solid hack proof but still helps somewhat.
Thanks

Including a body file (that is much like a platform MySQL / PHP / JS) into a wordpress template

I’ve built an SEO platform that Works with MySQL database and outputs / Inputs to a PHP table.
I’ve made a template in wordpress that has the header and footer from the original theme, and left the body empty so I can call the application / platform.
<?php
/*
Template name: Keywords
*/
?>
<?php get_header(); ?>
<?php
include('/body_call/index.php');
?>
<?php get_footer(); ?>
I’ve tried to place the folder with all the files (body_call) in several places and called the index.php folder hoping that it fills the center of the template.
The index folder is something like this:
<html>
<div id="container">
<body id="body">
<?php include('body_call/body.php'); ?>
</body>
</div>
</html>
I know the problem is the calling part because the platform works well if it’s by its self…
And it's probably some worpress specific code that I'm missing.
Can anyone help?
Thanks,
Miguel
When you include a file from another file you are actually just including it in the file. That means you are actually in first file folder and not second. So if second file needs to include a file wich is stored somewhere else it has to be included by navigating from first file folder to it's actual path. It sounds a little tricky. So to nvigate to upper level if need you should use ../.
Anyway to avoid any confusion i would suggest to use absolute path so where ever the file is called from it will always be able to charge any file from anywhere.
Have a look at include documentation
an example could be
include(/home/user/body_call/index.php');

php file path require syntax for importing a page

I have a directory called images and about and 3 php file on home directory
and 3 files and have different content
footer.php
This is footer <img src="images/logo.png">
header.php
<h1>Welcome to MyWebsite</h1>
index.php
<?php
require('header.php'); ?>
Enter your name : and some forms and javascript code
<?php
require('footer.php'); ?>
Now i have file in directory about as about.php
and it has some contents and below code is
about.php
<?php require('../header.php'); ?>
This is About page<br>
<?php require('../footer.php'); ?>
And when i open the page about.php, the footer is working fine but the image is not showing up, and image directory has image as logo.png
Even i used realpath to work out with relative paths, but could not display.
Even i tried this one too in footer.php
<?php
define('__ROOT__', images(__FILE__));
?>
This is footer <img src="<?php echo require_once(__ROOT__.'/logo.png); ?>">
I tried out the possibilites of relatives paths, do we have any other thing.
If I'm understanding your question correctly, the easiest solution is probably just to use an absolute path for your image tag in footer.php:
So if images is in your docroot, it would look like this:
This is footer <img src="/images/logo.png">
Including images in the php file makes absolutely no sense.
You have to understand the difference between PHP code and HTML code.
PHP code being executed on the server and have result of HTML code sent to the browser.
Where browser reads that HTML and do additional requests to the server to get images.
Thus, server filesystem root has absolutely nothing to do with browser.
As for the problem - Trott's answer is perfect.
Well if you are using include() to get certain section of the Page with PHP, there will definitely be a problem with links.
I believe the best practice is to call images using CSS. Just get a div with a class or id and place the relative link in the CSS.
It worked for me.
I got the answer for this questions
copy the below code in header.php and it will work every where
<?php
define('ROOT_DIR', dirname(__FILE__));
define('ROOT_URL', substr($_SERVER['PHP_SELF'], 0, - (strlen($_SERVER['SCRIPT_FILENAME']) - strlen(ROOT_DIR))));
?>
And where ever there is a image you src="" just put the below code only with imagename changed, and whatever you want.
<img src="<?php echo ROOT_URL .'/images/logo.jpg'; ?>">

Categories