0

I have a .php file which uses some WP functions e.g. get_stylesheet_directory_uri(), query_posts, etc... and $wpdb of course.

File it self return JSON data so it is not intended for viewing on its own.

I'm not looking for template, or clicking around in WP dashboard, I just want to know where is the good place in WP file structure to put the .php file and expect WP functions and object to be available.

And also what do I need to include at the top?

toni rmc
  • 848
  • 2
  • 10
  • 25
  • 1
    Take a look for [this](http://stackoverflow.com/questions/5306612/using-wpdb-in-standalone-script) – Ivnhal Aug 07 '16 at 05:48
  • I have tried and followed instructions there but still fails. WP can't find globals which are used in functions.php. – toni rmc Aug 09 '16 at 04:34
  • Why not to put the code you need in functions.php of your theme? – Ivnhal Aug 09 '16 at 06:59
  • Because it is a lot of code. Not just some code snippet but code that will generate a lot of HTML data and pass that data as part of the JSON response. Besides, cramming code in one file seems really bad design. What if I have need for something similar but different page? Put that as well in functions.php? – toni rmc Aug 19 '16 at 19:32
  • If you need a lot of functionality why not to add new plugin and put all that inside it? Since you've created simple plugin skeleton you are available to use any WordPress API inside it. – Ivnhal Aug 19 '16 at 19:41

2 Answers2

1

You can create custom template in theme root directory or in child theme (child theme is important when you are creating custom template).

template should contain template name, header and footer function.

eg. custom-tpl.php

<?php

Template Name: My Custom Temlate

get_header();

//your other functions and content goes here

get_footer();

after creating template it will appear in your edit page template section under drop down. select custom template for your page and view page you will get your output

Dnyanesh
  • 51
  • 6
0

You can create a custom page template. Inside the page template:

<?php

global $wpdb;

[here you will write your code]


?>

On the front pages where you want the json output, use this url http://domainname.com/wp-content/themes/themename/custompagetemplatename.php

Use your domain name (domainname), theme name (themename) and theme template file name (custompagetemplatename) in place of example names in the url. This url will return you the output for your code.

About this comment in your template:

<?php
/* Template Name: Full Width Page */
?>

You do not need a template name in the header of your file unless you want to use this as a template for pages in wordpress.