-1

I have written a Wordpress plugin in PHP and I want to output JSON without any wordpress theme html and other boilerplate. But I do need to interact with other plugins and the WP-Settings information. What is the best way to do this? I currently have a theme file called JSON outputter that just calls a method of a class that wants to output the JSON, is there a better way?

EDIT: Let me give an example:

I have the following index.php file in my plugin named jsonOutput:

<?php
// I need wp-config information here
$info = DB_NAME;
new otherPlugin();

header('Content-Type: application/json');
echo json_encode(array("test"=>"data"));

if I just access the following file via myserver.com/wp-content/plugins/jsonOutput/index.php then I don't have access to the wp-config info, all the other plugin or the other parts of Wordpress.

What is the best practise to output just JSON and not the header and footer and title and other things that wordpress and/or the theme adds. I want to know that the preferred way to do this is.

My current way works but I don't feel that it is the best way to do this.

P.S. I find the downvotes pretty harsh, I thought that I gave enough context but apparently not. If you need any more information just ask but a downvote only hurts my chances to find people that can actually help me.

  • AJAX, custom REST endpoint, or URL rewrites can all be events that trigger the output of JSON if you let your plugin handle them. See wordpress.stackexchange.com/a/218912/84219 – jgraup Mar 18 '16 at 10:34
  • 1
    The use of the words "best" and "better" is a sign of a bad questions, that is either too broad, opinion based or suffers from lack of research. Please edit your question to explain what is your exact situation and what alternatives are open for you, so people can have a chance to judge what is "better" and "best" – Mark Kaplun Mar 18 '16 at 10:45
  • There is just no context to go on here. Downvoted as non-constructive – Pieter Goosen Mar 18 '16 at 10:46
  • Complaining about downvotes is pointless. downvotes are an indication that you question is less likely to get an answer, so in theory would you prefer being ignored? Tip for next time, notifications are not being sent to people that commented on you question when you edit it, so better to be explicit about them – Mark Kaplun Mar 18 '16 at 14:59
1

You will want to have your own url structure to output your "non html" output. Actually there are several ways, and it might be up to coding style preference to select one

  1. Add a Page and a template for the page that does what you need. You don't have to output any html even in the theme hierarchy execution path, so you can have a page template which does not call get_header/get_sidebar/get_footer and just output the json from the url parameters. This approach is probably more appropriate for a theme then a plugin

  2. Use the admin-ajax.php end point. If there is no reason to have a fancy end point for your requests it will just work and you can follow all the examples about how to write ajax for wordpress on the web

  3. use the new json endpoint introduced in 4.4. It is new and I could not find the actual API for that, but it does exist and probably the best future looking solution

  4. use add_rewrite_rule to create your own end point and write the appropriate handler.

The technical differences between all the options in the end, is mainly how you set them up and the format of the url. Otherwise "best" and "better" are a matter of taste or other requirements (core json handling might be disabled, if you want to make sure your plugin will work even then, then the json endpoint path is not for you).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.