0

I have an existing site that has 5 or 6 php pages, with HTML mixed in for some tables and forms. The biggest purpose of the site is to upload CSV files into a database table and then on other pages select certain records from the database and display them in HTML tables.

The site works perfectly on my local server with a MySQL workbench interface for the database. However, I was just told it will have to be a wordpress site. Basically, it will link from an existing WP site, but same pages and themes so I'm basically building it within this existing site and pages.

I've been told it might be best to turn each of my PHP pages into page templates for the WP install. I'm curious the best way to go about turning these pages into wordpress page templates. I've looked all over but can't find great tutorials. Does anyone have some helpful info?

2 Answers 2

1

It really is rather simple. Check out the Codex on Page Templates for more info.

  1. Make a copy the existing page.php file located inside your WordPress theme's folder.
  2. Rename your page.php file to page-mypage.php
  3. At the very top of your new page-mypage.php file, right after the opening <?php tag add the following code to define your custom page template.

/* Template Name: My Page Template */

  1. Now inside your page-mypage.php file locate the WordPress loop. It usually looks like this:

    <?php
    // Start the loop.
    while ( have_posts() ) : the_post();
    
        // Include the page content template.
        get_template_part( 'template-parts/content', 'page' );
    
        // If comments are open or we have at least one comment, load up the comment template.
        if ( comments_open() || get_comments_number() ) {
            comments_template();
        }
    
        // End of the loop.
    endwhile;
    ?>
    
  2. Replace the entire loop with the relevant PHP code of your project

  3. Now in WordPress you will have to add a new page named mypage and select your custom template (My Page Template) in the "Page Attributs" sidebar block. There will be a dropdown menu called "Template".
  4. Done!
2
  • Wow, that is pretty simple! Thank you, I couldn't find a good guide on that this morning. Really quick: IF all of my pages have a connection and communication with my local database and table, would I keep the connection code in my PHP as long as I've created the db and table in my wordpress CPanel and the user/pass are correct?
    – H.Norman
    Apr 17, 2017 at 14:28
  • It would be best to use WordPress's database for your entire site. This way, you avoid having to connect to your database manually, as WordPress does this for you. Check out Class Reference/wpdb for a full overview of how too work with WP's database functions and Creating Tables with Plugins for a guide on how to create a database table for WordPress.
    – Swen
    Apr 17, 2017 at 15:56
0

You need there current theme to either develop a child-theme for it, or integrate your code into their existing theme.

This tutorial pretty much covers the basics for creating wordpress templates from scratch: https://www.taniarascia.com/developing-a-wordpress-theme-from-scratch/ So you should be able to find all the bits needed to convert your code into wordpress templates.

If you want a dirty quick fix, as long as you follow the formats from wordpress (using comment blocks above the page template so wordpress can identify it), you can just convert your .php files to a page template easily. To also enable the user to insert his own content into the page template, you do need to use some wordpress hooks tho. But they are really easy, see this part in the codex: https://developer.wordpress.org/themes/template-files-section/page-template-files/ for creating page templates & see https://codex.wordpress.org/The_Loop for using the loop:

    <?php 
if ( have_posts() ) {
    while ( have_posts() ) {
        the_post(); 
        //
        // Post Content here
        //
    } // end while
} // end if
?>

And then you can use stuff like the_content(); for displaying the content from the WYISWYG editor.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.