WordPress.org

Ready to get started?Download WordPress

Forums

Running PHP in a post URL? (5 posts)

  1. bang
    Member
    Posted 9 years ago #

    I'm trying to figure out how to run php inside a url in a post. I have sucessfully run PHP in a post with the help of:
    http://mark.scottishclimbs.com/archives/2004/07/02/running-php-in-wordpress-posts/
    But I cannot run PHP inside of a URL.

    For example, if I were to put this in one of my posts:
    <a href="site.com?x=<?php echo $_GET['x']; ?>&pg=1">Link Here</a>
    It malfunctions and messes up both the url and surrounding code.

    I have tried it with the plugin:
    <a href="site.com?x=<phpcode>echo $_GET['x'];</phpcode>&pg=1">Link Here</a>
    And that does not work either.

    It is not the php command that is causing the trouble (I have used the exact code outside of the posts fine), it is because WP is doing something to the posts and I can't figure out how to remedy it. Both of the popular PHP plugins for WP did not work for this.
    Otherwise the plugins work fine, but when you throw a URL in there something happens.

    I would appreciate any insight on this issue.
    Thank you.

  2. masquerade
    Member
    Posted 9 years ago #

    Use the RunPHP or PHPExec plugins.

  3. bang
    Member
    Posted 9 years ago #

    I have tried both. They both work in regular post text but neither seem to work inside hrefs.

  4. bang
    Member
    Posted 9 years ago #

    I paid a PHP guru look at it and he came up with this solution. Here's How you do it:

    1. Install and activate the PHPExec plugin: http://priyadi.net/archives/2005/03/02/wordpress-php-exec-plugin/

    2. Using PHP echo the entire <a>, and enclose that <a> in the PHPExec tags (<phpcode>, </phpcode>)

    So for example, if I wanted to make a link to site.com?x=<?php echo $_GET['x']; ?>&pg=1 using the link text Link Text then I would put this in my post:

    <phpcode><? echo "<a href=site.com?x={$_GET['x']}&pg=1>Link Text</a>" ?></phpcode>

    I could also use regular HTML for the link, as well as an <img> as long as there are no quotes. Use single quotes. For example, to link to that same page using the image pic.jpg I would put this in my post:

    <phpcode><? echo "<a href=site.com?x={$_GET['x']}&pg=1><img src=pic.jpg></a>" ?></phpcode>

    Hopefully this helps those of you that were having a hard time with this as well. Cheers.

  5. richschmidt
    Member
    Posted 9 years ago #

    Makes sense. Thanks for sharing your hard-earned wisdom with all the rest of us! :)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.