Support » Fixing WordPress » Can't use this PHP code in Posts and Pages

  • Resolved Pasta78



    I’d like to add a specific .php code into my posts and pages, for that I’ve installed the plug-in of “Allow PHP in posts and pages” from Hit Reach.

    (Didn’t get an answer on the Plug-ins author’s page).

    Here is what I’m trying to do:

    (1) Under my Theme’s folder – created file called config.php
    Config.php contains the following:

    $test=’This is Test Text’;

    (2) Then, I’ve updated my Theme’s ‘Single Post’ page (single.php) to be:

    include “config.php”;
    get_header(); ?>

    (Note: config.php and single.php are hosted in the same folder)

    (2) Then, I opened a sample post -> Clicked “HTML” tab and entered:

    echo $test;

    Nothing happens…it won’t call that data from config.php…

    If I went to single.php and manually added the following:

    <?php echo $test?>

    Just to see if that works, so it works that way, but the problem is I want it to work from within post’s content.

    What am I doing wrong and is there a simple way for me to add PHP codes into WP posts and pages? (I’m not a php expert…).


Viewing 12 replies - 1 through 12 (of 12 total)
  • The most obvious answer is that when yo uare using include() to incldue the other file into your page, it’s not getting the right location for it.

    I’ve seen this before, and it’s a PHP issue, not a WordPress one. When you use include() or require() with a file name like ‘file.php’ instead of a full file path like ‘/home/user/files/file.php’ it will take that relative to the script that’s currently running. The problem occurs in things like the template pages that are also included from within other pages to get their output, so the file path is taken a sthe main calling file, and not the template file.

    One thing that you can try is to add this littel bit of code in:

    echo getcwd();

    That will output the folder that you’re currently working in, and I’ll bet that is not the directory that your files are in. When youcfind out where you are actually working you can figure out what you need to do to get to the directory that you want to be in.


    did as what you say, add this code into a post:

    echo getcwd($test);

    Didn’t work…
    Got a php error…

    What do I need to do?

    What exactly are you trying to do that you need to include another file? I know you said you aren’t a PHP expert but there a ways to stick the output of PHP onto any/all pages and posts if you know what you’re doing or have the right guidance.

    That’s exactly what I need to do – have the ability to stick the same html code(s) for example in a specific spot(s) on all posts.

    For example: I’d like to display a photo with a link after the first paragraph of each post.

    So if one day I decide to replace that photo with another photo or with another html code that does something else, I can easily do that from one place.

    So I want to have a config.php page where I keep all my special html codes in one place.

    When I edit those codes as per my needs, it’ll affect all places where they appear.

    I’m not a .php expert, so one way that I know how to do that is as explained above. I’m sure there are other ways to do that…

    Hi, Can anyone help me with this please?
    I believe I’m doing things right, just that there’s a problem with WP.

    You would want to look into either using the functions.php file inside of your theme or creating a plugin that would insert the data into the post content for you.

    If you go the functions.php route you will lose the functionality if you switch to a different theme later on meaning you’ll have to copy over the old stuff onto your new theme. If you decide to go the plugin route you only have to activate the plugin and you’ll still have the stuff working whether you switch your theme or not.

    In regards to what you actually want to do, I would go the plugin route just because it is safer overall. As far as the actual code goes, well… either have to figure that out yourself, find a plugin that performs a similar action and tweak it or hire a developer to code something for you.

    Thanks for the reply JarretC.

    I don’t understand – there’s a plug-in that supports ‘php inside posts’, all php codes seem to be working, but this simple code won’t work…

    Again, I’m not a php expert but the php code that I’m trying to use seems to me like a very basic php code.

    There must be a way for me to run that simple code on posts. I’m sure what I’m trying to achieve by this has been in mind of many other WP users so there must be a ready solution for that.

    Go to and paste your code into there and submit it. Then on the page with your code copy the URL and post it here so I can look at it to see if there is an issue.

    Thanks Jarret,

    At the top of this page I’ve decribed in details the exact php codes I was using and the way I’ve been using them with “Allow PHP in posts and pages” Plug-in from Hit Reach.

    Try that out with the same plug-in and you’ll see that it won’t work for you…(it’ll take you no more than 2-3 minutes).


    Can anyone help with this please?

    What is the actual PHP error that you are getting? Most times the error message will help lead you to the problem if you take the time to read i tand understand it.

    I highly doubt that the problem is anything to do with WordPress as these things normally end up being user error (I have had a lot of that myself).

    Thanks, found the problem.
    Just contacted the creator of the plug-in “Allow PHP in posts and pages” (Hit Reach).

    Here is what you need to do:

    include(“wp-content/themes/(your theme’s name)/config.php”);
    echo $test;

    Now it works.

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Can't use this PHP code in Posts and Pages’ is closed to new replies.