Would need some more help with the

Did ask a question how to import some code with php, in this case it's a sidebar that will be used on some of the pages on a website, It's wordpress and it can execute php from post / page's and have also tested directly in the main code

When i did ask the question the best and most easy thing would be to create a file and use include

Did create thefile.php and did put it in the theme dir where all other .php file's used is, In the file i did just put some basic html like

<div id=the-id"> 
      <p> a test text </p>

Then i did put the include code writen as below in my template

<?php include 'thefile.php'; ?>

Doing this i see my text "a test text" but also get several warning's

 Warning: include(/wp-content/themes/thetheme/thefile.php) [function.include]:       failed to open stream: No such file or directory in /home/cplac/public_html/wp-content/plugins/exec-php/includes/runtime.php(42) : eval()’d code on line 22

And also this

Warning: include() [function.include]: Failed opening ‘/wp-content/themes/thetheme/thefile.php’ for inclusion (include_path=’.:/usr/lib/php:/usr/local/lib/php:/home/cplac/public_html/wp-content/plugins/wpsyndicator:/home/cplac/public_html/wp-content/plugins/wp-external-links/includes/phpQuery/:/home/cplac/public_html/wp-content/plugins/wp-external-links/includes/phpQuery/plugins/’) in /home/cplac/public_html/wp-content/plugins/exec-php/includes/runtime.php(42) : eval()’d code on line 22

Someone that can help me out whit this ? Have tested both in pages but also in the theme files and so on !

migrated from stackoverflow.com Nov 20 '11 at 15:47

This question came from our site for professional and enthusiast programmers.


The Wordpress method to include php files is to use this function:

<?php get_template_part('myfile'); ?>

which will include the file myfile.php that is in the same template directory.

See http://codex.wordpress.org/Function_Reference/get_template_part for other parameters.

  • Given that the OP (@Mathias) is using WordPress as a framework, this would actually be the best method. – Johannes Pille Nov 20 '11 at 6:32

You need to add /home/cplac/public_html/ to your include path.

  • Thx, that did solve it :D – Mathias Åsberg Nov 20 '11 at 4:24
  • @MathiasÅsberg, You should mark this answer as the accepted answer, if this solved your problem. Please read the FAQ: stackoverflow.com/faq – Brad Nov 20 '11 at 4:45

include is like

include = 'thefile.php';

on another note you need to also specify the correct path to the file from the root level or adjacent level of which the file resides.

Say I put a file in my "images" folder on my server for sake of example. And the path to the file would be /images/thefile.php

so with that the include would look like

include = '/images/thefile.php';

edit on a side note, working with WP can sometimes be a pain.. but generally WP is PHP so whatever works in PHP will work with WP overall.

for you based on the error it may be something like..

include = '/wp-content/themes/thetheme/thefile.php';

But you would need to from your ftp or equivalent navigate to that file path and upload the file into there.

  • 2
    1) The include syntax is fine without parenthesies. 2) What is "/images" relative to? – deceze Nov 20 '11 at 4:20
  • You are correct.. Im just very used to writing in a different syntax due to the MVC's I use as of late. And images was sake of example.. nothing more – chris Nov 20 '11 at 4:23
  • As far as I know, this is totally wrong! This triggers a syntax error in PHP. include is a keyword, you cannot assign anything to it. – Mike Oct 5 '18 at 6:06

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.