Consuming an external API in WordPress?

I have a client who has a WordPress website, and he want to extend it to add new functionalities. These functionalities are included in a website I have already done (a kind of search engine over a database, with some filters), and which is under new enhancements.

What I’m planning to do is to create a json api of my website (with all search funtion over my database), and make the wordpress website consuming this api, then I’ll only have to create views to show search results.

Read More

The problem is that I don’t know WordPress at all (for me it is something where you can add articles via admin panel…) and I don’t know how we can write our own server side code in it. I have searched on Google and found something called “plugin”, but I’m wondering if it is possible to do so, and if it is the simplest way to do as I don’t need to have the code used by any other third party. If it is possible to hack a WordPress install to add my PHP files, code what I need (my views with the same WordPress css) it would be simpler, but I don’t know if it possible ?

Related posts

Leave a Reply

4 comments

  1. A short primer on WordPress development that might help or might confuse you:

    The main thing to keep in mind: it’s all just PHP. And also: don’t edit the core files of WordPress, because you will get in trouble when you update to a newer version later. The only code you should edit is that of plugins and of themes.

    If you only need to add something to an existing page, it might work to just edit the right theme file. However, if you need to do something advanced (and adding an extra “view”, like at a specific URL, is more advanced in WordPress), you need to add the code at a place that will be loaded earlier in the WordPress boot process.

    The reason for this is that WordPress always does a post query for you, based on the URL. It you go to /category/banana/, it will query for posts of category banana and load the correct template file where you only have to loop over and display them. However, if you go to /custom-view/, WordPress will probably not find any post and load the 404 template – giving you no easy way to recover!

    Some people solve this by creating “stub pages”: they create a Page in WordPress where the content is not important, only the template is: there they do whatever fancy thing they want to do. This is hackish, but it works. The clean way would be to define extra rewrite rules, that let WordPress know that /custom-view/ is a valid URL, but that something else should happen there.

    If you want to do this, you need to hook into the core WordPress system. The important part here is hook into, not modify. WordPress has actions and filters. An action is just an announcement (“Hey, we’re currently writing the <head> tag. Do you want to add anything too?”). A filter allows you to modify a variable (“This is the post title. If you want to modify it, return a new value”). Of course, the trick now is to know which actions and filters you should use. This depends on the way you choose (stub pages or new rewrite rules), so I won’t go into details here.

    You define the actions and filters you want to use in a plugin file or in the functions.php file of your theme. They are special in that they are loaded while WordPress is booting, so before the main query is executed and so on. A plugin file is just a PHP file, placed in the wp-content/plugins/ directory, with a special header so WordPress can read its name and display it in the administration area where you can enable and disable it (so you can leave the plugin in the directory but temporarily disable it).

  2. Basics

    You have to get around the basics for plugins and themes. In short: They are additions to wordpress core functionality. Normally “Plugins” are more about functionality and “Themes” are more about the “view”. You might also want to read a little about the “Plugin API”.

    JSON

    You might want to use the json2 script that’s built into core to provide a “safe” API.

    JSON example

    The rest would be something like this:

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

    XML Rpc

    The Codex also has something about the “XML Rpc”.

    Http API

    Another possibility might be http requests. Be sure that you read the references at the bottom of the link.


    Webhooks

    This is additional, as it’s more about giving a consumeable API. Explanation here. And there’s also a plugin named HookPress that does this job for you. Some slides from the author as well.

  3. I assume you do know PHP? Or that is barrier as well?

    Essentially plugins in WordPress are convention how to package PHP code so that it is recognized and handled by WordPress core. It is format, rather than functionality.

    As for actual functionality you can pretty much run arbitrary PHP, but it is good practice to make use of WP’s APIs for compatibility and reliability.

    Think of your code in context of WP as include (which it will be technically). From there it is scope of your needs and resources that will determine how much of it will be rewritten to be specific to WordPress.

  4. Yes, it is possible.

    most plugins are created through what are called hooks:

    for example:

    add_action("hook", "function")
    

    it’s pretty easy to build yourself a plugin once you know what how that framework, well… works… I would advise to farmiliarlize yourself with it, the most extensive place for knowledge (albeit, not the most organized – like php.net) is the wordpress codex.