Php include template path

How to fix PHP include not working

When you add a PHP file to another PHP file using the include function, you may find that the include call doesn’t work.

For example, suppose you include a file as follows:

PHP may return the following warning on your screen:

This tutorial will help you to debug and solve the problem above.

Here are some things you can do to get the include statement working:

Make sure the file exists

This should go without saying, but sometimes you may have a typo in the file name you specify.

For example, you may write functions.php when you actually have a function.php file.

Before you check anything else, make sure that the file you are referencing exists.

Make sure you use the correct path

The include function will try to import your PHP file by navigating the file system path.

This means that you need to make sure that the included script can be accessed from the script that needs it using the specified path.

For example, suppose you have a project structured as follows:

Say you need to include the functions.php file in the index.php file.

You need to specify the path to the lib/ folder from index.php as shown below:

 The same applies when you have a script located above the script where you include it in the file system.

Suppose you need to include the template/ folder script in index.php . Here’s how you do it:

Because the template/ folder is on the same level as the main/ folder, you need to add ../ to move one level up in the include statement.

Let’s see another example. Now you need to include the header.php in the functions.php file.

But note that if you also include functions.php inside index.php , then PHP will try to get to header.php from index.php location.

To handle this kind of pathing error, you need to add $_SERVER[«DOCUMENT_ROOT»] to the include path.

This way, you will reference the included file from the root of your PHP project

  The include path is not dynamic and won’t adjust based on where you call the files from.

To avoid pathing errors, add $_SERVER[«DOCUMENT_ROOT»] and specify the absolute path instead of the dynamic path.

Make sure you use the system file path and not URL path

When you define the file path in the include statement, you need to use the file path instead of the URL path.

You can’t do the following:

As it will cause the warning below:

If you want to include a URL path as shown above, you need to activate the allow_url_include option in your php.ini file:

 But this is not recommended because your PHP application will be open to exploitation by malicious hackers.

Remote URL for include statement introduces vulnerability to your application.

Change include with require to find the cause

When there’s an issue with the include function, PHP will continue running your script and only emits a warning.

This makes it hard to debug and find why the include function doesn’t work.

To find the cause of the issue, you need to change include with require .

Unlike include , the require function will stop the script execution and emits a compile error.

 By using a require statement, PHP will stop running your script when the file import encounters any issue.

And those are the four things you can do to solve the PHP include not working issue.

I hope this tutorial has been useful for you. 🙏

Take your skills to the next level ⚡️

I’m sending out an occasional email with the latest tutorials on programming, web development, and statistics. Drop your email in the box below and I’ll send new stuff straight into your inbox!

About

Hello! This website is dedicated to help you learn tech and data science skills with its step-by-step, beginner-friendly tutorials.
Learn statistics, JavaScript and other programming languages using clear examples written for people.

Type the keyword below and hit enter

Tags

Click to see all tutorials tagged with:

Источник

Php include svg to template wordpress

Solution 2: I think it might be that get_template_directory_uri() returns an http direction, and some versions of php do not support remote file access, I would try using it only like this Question: I have SVG icons in PHP files, for example : I need to create variable associated with this file to include it and display the SVG code easily everywhere in my template (I’m using WordPress). Solution: Instead of placing in file put the code in PHP global variable and declare this in so you can use it anywhere in theme.

How to set template path within php include for wordpress API

I have the following to fill out, but for the life of me I can’t get this to read the get_template_directory_uri(); in order for the file to be properly included. This results in the page loading up to the running of this script, but then breaks everything afterwards (white space).

I’ve included the loop end/restart points in case this is a wordpress issue.

 
have_posts()) : $first_query->the_post(); ?>

This is a snippet for the forecast.io api if anyone’s wondering.

include( get_template_directory_uri().'/lib/forecast.io.php'); 

I think it might be that get_template_directory_uri() returns an http direction, and some versions of php do not support remote file access, I would try using it only like this

Php — WordPress Getting post data for Post Grid of, There are a number of values you can get by post data template variable function and one of it not the name of the author but their ID (that’s sad but at least you can use this value to get the author name). The value is ‘post_author’ => ID of the author (for example ‘1’) Here is the function where I get the post author …

How To Fix WordPress Fatal Error Failed to Open Stream

Have a lot of WordPress sites that were either damaged infected or corrupted by an upgrade or virus. Anyways here’s a quick way to fix that issue. This vid Anyways here’s a quick way to fix

Set variables for PHP files, so I could include these variables everywhere

I have SVG icons in PHP files, for example google-plus.php :

I need to create variable $icon_google_plus associated with this file to include it and display the SVG code easily everywhere in my template (I’m using WordPress). If you can, I would like examples, how to include that variable in these situations ( TheVariableHere = included $icon_google_plus ):

Situation 1 — PHP

function download_link($atts, $download_title) < $download_link = $atts[link]; $download_description = $atts[description]; return '
'.$download_title. '
'.$download_description. '
'; > add_shortcode('download', 'download_link');

Situation 2 — HTML

I’ll be very grateful!

Sorry, I’m very dumb in PHP, it’s very hard to learn it even with tutorials.

Instead of placing svg in PHP file put the code in PHP global variable and declare this in function.php so you can use it anywhere in theme.

global $googlePlusIcon; $googlePlusIcon = ''; 

“enable svg on wordpress” Code Answer, // Enable SVG support function add_mime_types($mimes) < $mimes['svg'] = 'image/svg+xml'; return $mimes; >add_filter(‘upload_mimes’, ‘add_mime_types’);

WordPress plugin template override

I am using this Worpress plugin: http://bestwebsoft.com/plugin/portfolio-plugin/

I already changed the portfolio itself by including the requests into my own template, but if i click onto one of the items, it directs me to the /portfolio/ permasite, which is then displaying the requested portfolio item in detail.

Now here is my question: I do know where to find the template for this single display (portfolio-post.php) but I’d like to change it myself, without risking to lose the template when I update the plugin. So editing within the portfolio plugin folder is not an option for me.

Is there any method to change this template by placing a «portfolio-post.php» whatever in my own theme folder?

If you create the portfolio-post.php file in your template folder and edit it there, you don’t have the risk that an update will overwrite it.

As long as you position the file inside the same folder basis as in the plugin you should be fine

HTML SVG — W3Schools, HTML SVG. SVG stands for Scalable Vector Graphics. SVG is used to define graphics for the Web. SVG is a W3C recommendation. Element. . 4.0.

PHP Include Errors

After migrating a WordPress site from a development server to the live site, I’m getting the following errors:

Warning: include() [function.include]: Failed opening ‘/home/content/83/11353583/html/wp-content/themes/mmaevents/includes/widgets/category-list.php’ for inclusion (include_path=’.:/usr/local/php5/lib/php’) in /home/content/83/11353583/html/wp-content/themes/mmaevents/includes/theme-widgets.php on line 13

There’s a number of those errors.

The code for theme-widgets.php is below:

include( get_template_directory() . '/includes/widgets/1-column-post-list.php' ); include( get_template_directory() . '/includes/widgets/2-column-box-post-list.php' ); include( get_template_directory() . '/includes/widgets/carousel-post-list.php' ); include( get_template_directory() . '/includes/widgets/tab-post-list.php' ); include( get_template_directory() . '/includes/widgets/tags-list.php' ); include( get_template_directory() . '/includes/widgets/category-list.php' ); include( get_template_directory() . '/includes/widgets/popular-post-list.php' ); include( get_template_directory() . '/includes/widgets/embedded-video.php' ); include( get_template_directory() . '/includes/widgets/flickr-shots.php' ); include( get_template_directory() . '/includes/widgets/social-profiles.php' ); include( get_template_directory() . '/includes/widgets/ad-leaderboard.php' ); include( get_template_directory() . '/includes/widgets/ad-skyscraper.php' ); include( get_template_directory() . '/includes/widgets/ad-square-popup.php' ); 

Any ideas why I’m getting these errors?

This mean u dont have the files in the correct location . Where the php file is located you should also have a directory /includes/widgets/1-column-post-list.php . You could also spell out the entire url to these files but probably not necessary

It means it can’t find those files, possibly they didn’t get migrated with everything else?

It’s also possible your php search path is different on the dev server from the production server. So those files aren’t in a directory directly under the place where the file that’s including them lives. So there might be a difference between the php.ini files on each server. (See more here).

Ideally you’d fix the php.ini, but if you can’t you can use the set_include_path() function somewhere before the plugins are included (more details here).

Twig: Include block of another template, Using partials will be a better solution for that. I don’t think that is possible to access block of another templates in twig, always i need to reuse parts of template i create a partial for them. Partials could follow a different path, like _partials/Header.twig.html And you can include this in template also with …

Источник

Читайте также:  Sparse matrix python index
Оцените статью