toni rmc
toni rmc

Reputation: 878

Wordpress add .php file manually

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?

Upvotes: 0

Views: 73

Answers (2)

Anuj Verma
Anuj Verma

Reputation: 1

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.

Upvotes: 0

Dnyanesh
Dnyanesh

Reputation: 51

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

Upvotes: 1

Related Questions