• Fox_Black

    (@fox_black)


    Hello everyone!

    I want to programm an plugin for my website, which should be actually an api which returns for every user his own specific data (json files in the filesystem). For user access I want to use built-in functions and functions delivered from other plugins has_user_access(user, area) for example from an other plugin. So if I want to use those wordpress functions I cant write only a .php file and place it somewhere, because the wordpress functions are not included here.

    So I found 3 solutions around at the moment:

    1. Somewhere I found, that you can write a .php file and include wp_load.php. But that method seems not to be recommended.

    2. Next I thought about writing a custom page template, which includes only my php code and does not use page content functions like header, footer, the_content and bind that page to a subpage. So when that subpage is called for example website/myapi the php code inside the page template handles the request and gives only back the json file and I can use the wordpress functions I need inside it.

    3. Use the official Wordpres Rest API Plugin. But it seems a little bit overkill for the purpose to give json files from the filesystem back since I can do the user control with existing functions and not need the authentication methods of the Rest API?

    1 seems to be really bad so I will choose 2 or 3. What do you think about solution 2. Are there any disadvantages? Should I better use solution 3?

    Best regards,

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    Your assessment is very accurate and correct, well done! I personally would go with custom page templates myself. Mainly because I can custom tailor the code to my specific needs. Generic, all encompassing interfaces are very useful is some cases, but carry a certain amount of overhead.

    There are a couple other ways to invoke the WP environment that will work for any installation (requiring wp-load.php cannot do this). One is by using AJAX requests, the other is by channeling requests through admin-post.php. This last one is not well documented, but it’s not too hard to figure out. It’s sort of like AJAX, except no javascript is required.

    tex0gen

    (@tex0gen)

    You could also use the built in WordPress AJAX url. There you would write your own functions and call them via ‘wp-admin/admin-ajax.php’.

    Create your function and follow the docs on this page:
    https://codex.wordpress.org/AJAX_in_Plugins
    Don’t forget to echo out your data using json_encode();

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Programming an php api with wordpress functions’ is closed to new replies.