WordPress.org

Ready to get started?Download WordPress

Forums

Putting PHP on a WordPress "Page" (17 posts)

  1. ajs
    Member
    Posted 7 years ago #

    What would be the best (easiest) way to put PHP code on a WordPress "Page" feature (or even a Post, for that matter).

    Using the WordPress interface to write a page or post doesn't accept PHP code, as it tries to HTML-ize it or mangle it in other ways. Is this possible?

  2. moshu
    Member
    Posted 7 years ago #

    You need a plugin:
    http://codex.wordpress.org/Plugins/Posts_Formatting
    Look for php-exec, exec-php, run-php and similar stuff.

  3. baszer
    Member
    Posted 7 years ago #

    yes this is what i need, but now i get another problem...

    when i use runPHP

    i get the following problem

    if fill in

    <?php
    $curDate = date('D., F jS Y H:i:s');

    echo "\n<p>";
    echo 'Right now the server thinks it is ' . $curDate;
    echo "\n</p>";
    ?>

    then i check the checkbox 'use runphp'
    i save the page (publish)

    when i watch the page i see this
    http://geenstress.ruunerwold.nl/?page_id=31

    wp deleted the upperpart of the php script,
    so what do i do wrong now?! :(

  4. ajs
    Member
    Posted 7 years ago #

    if you're having troubles, it is mentioned in a couple places that php_exec might work better for some folks than runPHP. even the author of runPHP says this....

  5. baszer
    Member
    Posted 7 years ago #

    ok,

    now i get this error :(

    Parse error: parse error, unexpected T_VARIABLE in /var/www/vhosts/ruunerwold.nl/subdomains/geenstress/httpdocs/wp-content/plugins/exec-php.php(75) : eval()’d code on line 52

    it works fine normally...

  6. baszer
    Member
    Posted 7 years ago #

    anybody idea?

  7. chrishes
    Member
    Posted 7 years ago #

    If you're using WP2.0, you'll need exec-php as php-exec will cause you errors.

    http://bluesome.net/post/2005/08/18/50/

  8. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    An even *better* way than using a plugin is to make a Page Template with the PHP code you want to use and then use that Page template for that page only. This requires no plugins and it is more secure than trying to stick PHP in a place where you don't really want it (the database).

    http://codex.wordpress.org/Pages#Page_Templates

    You'll have to use a plugin to put code into a Post, however if you have to put code into a Post, then there's probably a better way to do whatever it is that you're trying to do, and you should reconsider and ask about that instead.

  9. baszer
    Member
    Posted 7 years ago #

    ok, 2 more questions, how can i get a link in the left menu to this new template

    and why do i get this error

    Fatal error: Call to undefined function: get_header() in /var/www/vhosts/ruunerwold.nl/subdomains/geenstress/httpdocs/wp-content/themes/fluidityls-10/snarf.php on line 6

    when i just use the example!?

    <?php get_header(); ?>

    something wrong with this?

  10. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    baszer: What "left menu" are you talking about? Page templates show up on the right hand side of the Write Page screen, under the "Page Template:" box. And you get them to show up there by simply creating them with the header as it shows in the example:
    <?php
    /*
    Template Name: Snarfer
    */
    ?>

    As for the error, this is not a file you run *directly*. It's a file that WordPress will use. The reason you get the error with get_header is because you are trying to call it directly. Don't do that. Go to your Page and edit it, then choose your template from the drop down and save. Then that page will use your new template. And it won't get that error.

  11. chinatour
    Member
    Posted 7 years ago #

    check out "www.dynamicdrive.com" where you can find some script to make a sliding menu on the left side.

  12. oncidium
    Member
    Posted 7 years ago #

    I'm trying to do something similar...include a gallery within a page. I got the php to run on the page, see here: http://www.divaknitting.com/blog/?page_id=334 but the problem comes when you try to click on a category. I got the page to show up by putting view.php in the blog directory but now I am at a loss as to how to get the blog to appear around it as you drill down to single pics. Any ideas?

  13. ljmblue
    Member
    Posted 6 years ago #

    I've got a page where I want a frequently-changing version number to appear. I've created a page template and I can get at the version number in the template using require_once, but I'm stumped re: how to get this version number from the page template into the page content.

    If using a php plugin is the right way to do it then fine, just thought I'd ask if there was a better way.

  14. moshu
    Member
    Posted 6 years ago #

    Anything from a template should get into the Page if you are using that template when creating the Page.

  15. ljmblue
    Member
    Posted 6 years ago #

    Hm, a code sample might better illustrate my question.

    Here's an abbreviated bit of the template:

    <?php require_once 'fooversion.inc.php'; ?>
    <?php the_content(); ?>
    <?php edit_post_link('Edit this entry.' . get_foo_version(), '<p>', '</p>'); ?>

    and then the page appears (ignoring the sidebar):
    header
    content part
    content part
    Edit this entry 1.2.3

    So I can make the version number appear in the post link. But I want the version number to appear inside the page content like this:
    header
    content part
    Version 1.2.3
    content part
    Edit this entry

    In other words, I want the version number to appear within the page content and this is where I get stuck.

    I am getting the feeling that the only way to get this version number into the page content is with a php plugin or at least something like the Enzymes plugin.

  16. moshu
    Member
    Posted 6 years ago #

    I am not a coder, so excuse my ignorance... shouldn't the required filename be in ()?
    <?php require_once ('fooversion.inc.php'); ?>

  17. ljmblue
    Member
    Posted 6 years ago #

    Using parentheses with require_once is optional.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.