WordPress.org

Ready to get started?Download WordPress

Forums

How to run my own PHP script from a WP page? (4 posts)

  1. DavisDaddy
    Member
    Posted 1 year ago #

    Greetings. I have a little PHP script that pulls item names and the names of
    an associated person from a set of MySQL tables. I've appended a "sanitized"
    version of the script (mostly just omitting the details of the query). The
    script works just fine from the command line or as a stand-alone web page.

    I'd like to make a page on a (self-hosted) WordPress site that basically just
    runs this script, but I haven't been able to figure out how to do that.

    I tried just dropping the PHP code into the HTML view of a new page. The
    resulting page just showed the PHP code.

    I thought I might encapsulate the script as a function and drop it into
    functions.php (in a child theme). I can do that easily enough, but I don't
    know what to do after that. I tried mentioning the function name in brackets
    on the web page, but just echoed the function name.

    BTW, I've added the associated tables to the WordPress database, naming them
    with an 'x' prefix. I.e., where the standard WordPress tables are called:

    wp_<something>

    mine are called:

    xwp_<something>

    I.e., I don't see any possibility of confusion, but my tables ARE in the same
    database as the regular WordPress tables (in case this makes a difference).

    The system in question is running WordPress 3.4.2 on Ubuntu 12.04.1 LTS. The
    version of PHP is 5.3.10, and the version of MySQL is 5.5.24.

    Thanks for any help you can provide.

    -- Mike

    Appendix: standalone PHP script that I want to run from a WP page
    ==================================================================

    <?php

    require_once 'login.php';

    $con = mysql_connect($db_hostname, $db_username, $db_password);
    if (!$con) {
    die('Could not connect: ' . mysql_error());
    }

    mysql_select_db("mymdb", $con);

    $result = mysql_query(
    "SELECT <name of topic> <person firstName> <person lastName>
    FROM ... WHERE ...");

    echo "<table border='1'>
    <tr>
    <th>Topic</th>
    <th>Person Name</th>
    </tr>";

    while($row = mysql_fetch_array($result)) {
    echo "<tr>";
    echo "<td>" . $row['name'] . "</td>";
    echo "<td>" . $row['firstName'] . " " .
    $row['lastName'] ."</td>";
    echo "</tr>";
    }
    echo "</table>";

    mysql_close($con);
    ?>

  2. MarkRH
    Member
    Posted 1 year ago #

    While I haven't tried it myself, looks like this plug-in might do the job: http://wordpress.org/extend/plugins/shortcode-exec-php/

    I use the Executable PHP Widget http://wordpress.org/extend/plugins/php-code-widget/ to grab a random image from my separate gallery site's database to display in my side-bar.

  3. Pioneer Valley Web Design
    Member
    Posted 1 year ago #

    Create a Page Template that includes your personalized PHP and/or HTML and assign it to a page or use one of the default page template names...assign template to the page(s).

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

  4. DavisDaddy
    Member
    Posted 1 year ago #

    Thanks to Mark and Seacoast for their useful suggestions. I think the custom
    Page Template is going to work. I do have (at least) one follow-up question.

    The MySQL connect/select/query functions require me to supply the hostname,
    username, password, and database name. For all of these I just replicated the
    information that's already in wp-config.php. This isn't a big deal, as it's
    not likely that this information will change frequently (maybe never).

    OTOH, replicating data like this is ugly, and it DOES open the door for an
    inconsistency down the road.

    Is there any simple way to pull this information into my PHP script? Ideally,
    this would be something like:

    $dbName = wp_get_db_name();
    etc.

    Failing that, I guess one could do some kind of regular-expression search of
    wp-config.php, looking for DB_NAME, DB_USER, etc.

    Any suggestions? Thanks.

    -- Mike

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.