Support » Fixing WordPress » How to run my own PHP script from a WP page?

  • DavisDaddy


    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:


    mine are called:


    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


    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′>
    <th>Person Name</th>

    while($row = mysql_fetch_array($result)) {
    echo “<tr>”;
    echo “<td>” . $row[‘name’] . “</td>”;
    echo “<td>” . $row[‘firstName’] . ” ” .
    $row[‘lastName’] .”</td>”;
    echo “</tr>”;
    echo “</table>”;


Viewing 3 replies - 1 through 3 (of 3 total)
  • MarkRH


    While I haven’t tried it myself, looks like this plug-in might do the job:

    I use the Executable PHP Widget to grab a random image from my separate gallery site’s database to display in my side-bar.

    Pioneer Web Design


    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).



    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();

    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

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to run my own PHP script from a WP page?’ is closed to new replies.