Forums

how to add a functional PHP script within WP so that it stays functional? (7 posts)

  1. marcnyc
    Member
    Posted 2 years ago #

    Hello, I have a PHP script that connects to a database and generates a list of clickable items.
    I need to include the output of this script within my WordPress website and I am getting bold scratching my head trying to figure out how. My WP installation dir is 'blog'.

    I tried the Exec-PHP plugin, the WP Include File plugin and a few other plug ins that basically all let you include an external PHP file.
    What I did is create a page (called "artists") and then add the include code for those plugins in that page. The page's URL (I have pretty permalinks activated) is /blog/artists

    I do see the output of my PHP script, and although there are some 'cannot modify header' errors due to the fact that the script uses sessions, the bigger problem I have is that the clickable items of the list break because if a link points to "index.php?artistid=43" instead I am sent to the URL /blog/?artistid=43, which of course doesn't resolve because WordPress doesn't know what that variable means.

    Is there a way around this? Is it possible to let a fully functional third party standalone script that works, still work within WP so that it looks like it is part of the rest of the site?

    Is the only way around this an iFrame that will look crappy and will have scrollbars?

    Thanks a lot for your help.

  2. stvwlf
    Member
    Posted 2 years ago #

    use a custom page template and add your PHP in the template, not in the post area
    http://codex.wordpress.org/Pages#Creating_Your_Own_Page_Templates

    http://www.expand2web.com/blog/custom-page-template-wordpress/

  3. marcnyc
    Member
    Posted 2 years ago #

    Thanks for the links. So it is not possible to do it without creating new template pages? I just wanted to create it in a way (if possible) that would allow me to switch templates easily, without having to move around/create custom template files. Is that out of the question?

  4. stvwlf
    Member
    Posted 2 years ago #

    If you set parameters in the code you can have multiple PHP scripts within one custom page template. Call the URL with GET parameters that determine what code to execute.

    don't point your link to index.php?artistid=43. You point it to the URL of the custom page you have set up. If you are displaying artist profiles for example make a page that has a url of /artist amd pass it
    /artist?artistid=43

    Then you have to tell WP about the query vars on the URL. There are two ways you can do that
    1) http://www.simonwheatley.co.uk/2009/02/13/adding-get-params-to-a-url-in-wordpress/

    2) http://www.webopius.com/content/137/using-custom-url-parameters-in-wordpress

    I have one site with 2000 author profiles. They are all stored in the database and a single custom page template is called with an ID # as a query var to display that page as the template for these profiles. The template retrieves the query var and does the db lookup of the content to display on that profile. It works well.

    To me using a PHP exec plugin to add code to a post is not a very reliable approach unless you have only a small amount of code to add. The custom templates allow you to program in PHP just as in any standalone PHP program, and have that code integrated within the WP framework.

  5. marcnyc
    Member
    Posted 2 years ago #

    I would love to look at your single custom page template and see how your website integrates your author profiles with the WP installation... could you send me a link? would you be willing to share that template code so I can see? I am not quite clear on what you are describing even though I read the two links you sent.
    Thanks for your reply, time and support.

  6. stvwlf
    Member
    Posted 2 years ago #

    Hi

    unfortunately the site's still in development and not publicly acessible yet.

    The author info is stored in a custom db table within the WP database

    As an example, there is a page of listing of books in one category that shows a thumbnail of the cover, the title, the author name, the isbn, etc. When a profile exists for an author the name is presented as a clickable link.

    The gist of the code for the link is
    <a href="<?php bloginfo('url'); ?>/authors-illustrators/profile?profileID=<?php echo $author_profile_id ?>" ><?php echo $author_name; ?></a>

    That links it to the author row in the table. I am using WP posts for reviews, so chose to have the author info in a separate table but I could just as easily have created a post for each author and used the post content to hold their profile, and create WP custom fields for each field of author info.

    /authors-illustrators/profile is the URL of the WP page I have linked to the custom page template. It is a child page of authors-illustrators, which is why the two levels of URL path there. As you can see, I am passing a query var on that URL. These vars need to be defined to WP or WP strips them off and ignores them.

    To define the query var I have this in the theme's functions.php file

    add_filter('query_vars', 'my_queryvars' ); //  query vars passed on URL
    function my_queryvars( $qvars ) {
      $qvars[] = 'profileID';
      return $qvars;
    }

    This tells WP to add the var I am passing, profileID, to an array of query vars that WP keeps for its own use.

    Then I need to test for and if existing retrieve that value within the custom template (the WP API does it this way instead of passing through $_GET). You can still use $_GET with a different WP function, but this method is cleaner and more aligned with the WP API).

    In the custom page template, near the top, I have this code:
    $profileID = $wp_query->query_vars['profileID'];
    This retrieves the query var from the global WP data structure. If it was found I use that variable to do a lookup of that profile and fill in the variables in the form with the data from that record. So the one custom page template displays thousands of individual profiles.

    If the variable is not found, I am using the same template for a different purpose, to display a listing of authors. The code tests for the existence of the var and processes accordingly.

    Hopefully you can follow the gist from this example. You can think of your posts as straight data if you want to - WP has default ways of displaying the posts but you are not limited to that. You could enter 1,000 artist posts and display their contents in a different template altogether by retrieving the post content and custom fields as variables in a single template. Then you can arrange it as you like. When looked at this way, a post is just a bunch of fields in a row in some database tables.

  7. marcnyc
    Member
    Posted 2 years ago #

    is your site public yet?

Topic Closed

This topic has been closed to new replies.

About this Topic