Running PHP inside Wordpress Page

Discussion in 'PHP' started by cesarcesar, Aug 10, 2008.

  1. #1
    I need to run PHP inside a Word Press Page/Post WITHOUT use of an additional plugin or editing the WP code. Is it possible?
     
    cesarcesar, Aug 10, 2008 IP
  2. Rory M

    Rory M Peon

    Messages:
    1,020
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Display the code or actually execute it?
     
    Rory M, Aug 10, 2008 IP
  3. Pos1tron

    Pos1tron Peon

    Messages:
    95
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Well if you have access to php.ini (aka if you've got a VPS, or a dedicated server, or even just normal accounts for some hosts with custom control panels), you could use auto_prepend_file or auto_append_file (see http://www.php.net/ini.core at about half way down) to include a php file at the beginning or end of a page execution.

    If you're on a typical host's shared hosting account with cpanel though, I'm pretty sure you won't be able to make a custom php.ini - in this case your best chance could be to put an include()/require() statement in a file that is always included (perhaps config.php, assuming you don't need any info about the page content from wordpress), requiring minimal change of the wordpress code. You could make a script to be run when a new wordpress version is installed, that adds an include statement to the bottom of config.php or any file like it. The script should look something like this:

    <?php
    
    $fileIncludePath = 'PATH TO FILE TO REQUIRE() IN MODIFIED FILE HERE';
    $fileEditPath = 'PATH TO FILE TO CHANGE HERE';
    
    file_put_contents($fileEditPath, '<?php require('.$fileIncludePath.'); ?>', FILE_APPEND) or die("Addition Failed!");
    
    ?>
    PHP:
     
    Pos1tron, Aug 10, 2008 IP
  4. Jasber

    Jasber Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You shouldn't need access to php.ini. You should be able to do something like this in .htaccess:
    <FilesMatch "\.(php)$"> 
    php_value auto_append_file "/your/script.php"
    </FilesMatch>
    PHP:
    This will only work if your host allows your .htaccess to overwrite the httpd.conf file.
     
    Jasber, Aug 10, 2008 IP
  5. nfd2005

    nfd2005 Well-Known Member

    Messages:
    295
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    130
    #5
    Create a page template with your php code. (You can probably copy page.php or single.php and create a template from that.) Next on your post/page editing page, associate your template with that post/page.
     
    nfd2005, Aug 10, 2008 IP