简体   繁体   中英

How to directly access php file in WordPress

I have a php file that generate js code. I need to acces this file like host/nameoffile?args

For now I settled this issue this way - a have named my php like page-{slug}.php, made a page in Wordpress with same slug and placed this file in my current theme folder.

our server works on nginx with only one entry point that is index.php, so it's block any other php queries.

Is there any other realization, not like my current?

how about coping all the code from the php file and adding them to a function with parameters (in functions.php) that would return the js code. That way you can just call the function from the header/footer and it will output the relevant js code.

Solution by @parham is that, what i was looking for)

add_action( 'template_redirect', 'wpse131387_template_redirect' );
function wpse131387_template_redirect( ){
    if ($_SERVER['REQUEST_URI'] == '/some-template') {
        global $wp_query;
        $wp_query->is_404 = false;
        status_header(200);
        include(dirname(__FILE__) . '/some-template.php');
        exit();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM