1

I've been building out a website within a WordPress 'wrapper', and it's been going reasonably well. I've got a Plugin that holds some of my code, and I've got a Theme that holds other bits of my code, and overall things seem to be working the way I'd like for them to.

My site is not a blog and will never contain blog-style content, has very few of the kinds of 'Page' elements that typify WordPress installations, and overall doesn't use most of the rest of what people think of when they think 'WordPress'. I'm mainly using WordPress because I'd like for user account management and monetization to be handled by WordPress and 3rd-party plugins that I can use with WordPress.

So in order to build my site within WordPress, I had to create a bunch of page Templates, and then use the WordPress Admin's Create Page function to create a (blank) page with a particular slug (or at least a unique slug) for each of my Templates. This all works.

However, it's not very portable.

What I mean by that is that I've developed all of these 'Templates' (which are really fully-functional pages) and they exist in my Theme, but then when I want to move my code from my dev box to the test environment, I've got to go into WP's admin interface and laboriously create all of those Pages again. And pray that I don't screw anything up with all that mind-numbing manual effort.

And then, when it's time to promote to Production, I've got to do all that again. This, needless to say, is undesirable.

My question, then, is 'how can I use my custom-coded PHP pages in WordPress in a way that makes them easily portable between environments?' From the formulation of the Title of this post, you can see that I assume that this would entail not using Templates, but I don't know what I don't know, so I'd appreciate guidance from those of you with more WordPress experience than I have.

9
  • you can create a shortcode with a plugin. then when there is something new, you juste update the plugin and the page will show the new result through the shortcode already in place. look here : codex.wordpress.org/Function_Reference/add_shortcode
    – mmm
    Nov 14, 2017 at 21:30
  • The WordPress way would include keeping content within the Editor itself. But to answer your real question - how to make it easier to copy content from site to site - you can programmatically create pages, posts, and other post types. Create them through code on your dev site until you get things tweaked right, and then you can do the same thing on your prod site. Or, use a database migration / site cloning plugin to copy everything for you, including not only the FTP files but also the data in the database.
    – WebElaine
    Nov 14, 2017 at 21:31
  • Seems everything you're currently doing by hand you could encode in a plugin or theme or child theme to install somewhere else.
    – CK MacLeod
    Nov 14, 2017 at 21:32
  • @CKMacLeod I've currently got a bunch of PHP and JS code in my plugin and theme... what else would I 'encode' into my theme and/or plugin? The commands to create the Pages I'd like to not have to do manually? Nov 15, 2017 at 19:00
  • 1
    See answer below: It's very general, but you've asked a general question.
    – CK MacLeod
    Nov 16, 2017 at 0:14

1 Answer 1

1

I think what you'll want to do - or something like what I'd do in your place - is create a simple WordPress plugin. (I'm bold-facing things that you can search up - lots of material on all of this will be right at your fingertips on the internet, often in detail in the WordPress Codex.)

On activation it would use wp_insert_post to create the pages you need. The pages would in turn all use the same page template - call it page-php.php or something. By some mixture of automation and hard coding, the page template would "include" whichever of your php sub-pages (or whatever you want to call them) at the appropriate points, along with whatever appropriate basic WP formatting and function tags. A typical method is to copy over the theme's page.php, and use it is your template's template.

For a general release plug-in, you might have to look into the more difficult tasks of programmatically producing page templates, or of utilizing custom post types. Either might actually be worth looking into, but I don't think you really need to do so. On the WordPress All Pages screen, you'd just select and bulk edit your new pages to use the template file, which you'd add to your child theme directory.

For a general release plug-in you'd also probably want a way to add new pages and associated sub-pages to the group, but you could do that by hand if you weren't able to arrive at a programmatic solution right off the bat.

As for whatever other necessary and unique JS and PHP files, all could be included as needed with the plugin.

I'll just add that it's possible to do this all with a child theme instead using very similar methods, and you will want to be working with a child theme anyway.

If and when you hit some roadblocks, you can come back here with the actual code you're using and probably get prompt attention, and useful corrections and alternatives, very quickly.

2
  • Ah, interesting! I'd created a separate Template for each of my pages, and had to create a Page for each one, using its Template. Your described approach differs from that by using one Template and then including different PHP files (for instance, the ones that currently serve as my Templates), right? If so, how can I specify which of the PHP file(s) to include? Would I key off the page's slug or something like that? Thanks very much for this information, it's very useful and a lot to think about. Nov 16, 2017 at 1:09
  • Right, I think you're on the right track. You could key off the page slug or name in the new template file. There are built-in functions for that, and you'd be able to set, say, the name once, and re-use the same variable, or create or extract an array of them from your php files, and do the page creation and inclusion iteratively. Also, look up register_activation_hook, as it's commonly used in plugins in this kind of thing.
    – CK MacLeod
    Nov 16, 2017 at 2:36

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.