run php application within wordpress

2019-05-30 15:51发布

So I own a computer repair store. We have a point of sale software that we really like. There is a "check repair status" folder you can install this app on another website, I love the way this works, but want to incorporate it (deps.php, index.php, headerstatus.php, footerstatus.php, common.php) all into one file so I can execute that php in the post section of a WordPress page remove the <? php {php _code here} ?> tags and replace them with [insert_php] {php code here} [/insert_php] with the use of a wordpress plugin "insert php" only the code between those two tags will be executed inside that WordPress page (inphp) it has been posted on.

The problem is it cannot reach out to another file from there it all has to be contained. Since wordpress pages aren't physical locations rather information from a DB. It would be nice to run that php from the page so I could easily just create a couple small custom class sizes for boxes and divs then delete all previous custom classes and styles in the php and allow the default class/styles from our theme take their place and custom styles/classes (likely inline)by me for those that aren't defined by the default css of our theme.

So is there command we could use to tell the index where to reference those functions, parameters, or commands from within the same file, as opposed to referencing separate files?

require("deps.php"); require("headerstatus.php"); require("common.php");
require_once("footerstatus.php");
function showstatus() { require("deps.php"); require("headerstatus.php"); require("common.php");

So if it's not possible to remove require, or use the require or another command to point at a localized function, line, or path in its place. either a (note I said possible, this code doesn't need to be perfect it needs to run from a single script and work. I understand people hate to recommend non standard answers to programming.)

How much work would it be to rewrite each individual item needed from the common, deps and php files to reflect the type of data its looking for? Or would it make more sense to rewrite the code in the index to work better with localized functions?

The problem is that I need this to work, and can find no examples online which is not usually a good sign. I'm open to any and all suggestions! Just throwing out ideas here, also if you make a suggestion please give an example.

1条回答
做个烂人
2楼-- · 2019-05-30 16:33

The best way to do this is to preserve the folder structure of the application you want to insert. This way paths should still work, they are relative to the file so should still work.

so 1. create a new folder in your theme called "repairstatus"

  1. upload all the files into that -- index.php etc

  2. Place the following code in your themes functions.php (or create a child theme

    //1. add a wp query variable to redirect to
    add_action('query_vars','plugin_set_query_var');
    function plugin_set_query_var($vars) {
        array_push($vars, 'is_repair_page'); // ref url redirected to in add rewrite rule
    
        return $vars;
    }
    
    
    //2. Create a redirect
    add_action('init', 'plugin_add_rewrite_rule');
    function plugin_add_rewrite_rule(){
        add_rewrite_rule('^repairstatus$','index.php?is_repair_page=1','top');
    
        //flush the rewrite rules, should be in a plugin activation hook, i.e only run once...
    
        //flush_rewrite_rules();  
    }
    
    //3.return the file we want...
    add_filter('template_include', 'plugin_include_template');
    function plugin_include_template($template){
    
    
        // see above 2 functions..
        if(get_query_var('is_repair_page')){
            //path to your template file
            $new_template = get_stylesheet_directory().'/repairstatus/index.php';
            if(file_exists($new_template)){
                $template = $new_template;
            } 
            // else needed? up to you maybe 404?
        }    
    
        return $template;    
    
    }
    

Now navigate to settings on the admin dashboard and click on permalinks. Just hit the save button. test at this stage, http://accuratepctech.com/repairstatus/ should open index.php (it will look funny for the moment)..

now mod the index.php file...all we need to do is comment out the headerstatus and footerstatus requires, they will break the html output...

We can use get_header() to load the header of the wp site which will load css, navigation etc. and get_footer() for the footer element.

    if (array_key_exists('func',$_REQUEST)) {

    $func = $_REQUEST['func'];

    } else {

    $func = "";

    }





    //loads header and related 
    get_header();


    function repairlookup() {

    require("deps.php");

    //require("headerstatus.php"); -->not needed, its only html

    require("common.php");

    //etc////////////


    //find require("footerstatus.php") and remove

    }

    get_footer();

now you should have a wp looking page just the index.php code will be unstyled. You can add the css in by using

<link rel="stylesheet" type="text/css" href="style.css">

(note this is html, close php tags to use it) but id recommend you open this file and copy into your style.css in your theme, you can then change the css to suit.

Note this will probably work as is, but there may be errors so check the operation throughly. I'm going to recommend you google wpdb and update to use WP database functions (you can create seperate tables if you want) otherwise you have 2 db connections on the go. This will not be good for the performance of the website, it will load ok with 1 user but 3/4 concurrent users will start to slow everything down...

查看更多
登录 后发表回答