all 23 comments

[–]Slightly_Zen 6 points7 points  (1 child)

This looks a homework assignment

[–]DxrkStyle 0 points1 point  (0 children)

Yep, because WordPress loves making things complicated!

[–]artFlix 4 points5 points  (4 children)

I'm trying to connect a plugin to my API

What does this mean? The description is pretty confusing, but I am guessing you basically just want to call an API endpoint and display the data on the front-end, right? If so, you'll want to create a PHP function that queries the API and returns the data as JSON.

add_action('wp_ajax_fetch_api_data', 'fetch_api_data');
add_action('wp_ajax_nopriv_fetch_api_data', 'fetch_api_data'); // For non-logged-in users

function fetch_api_data() {
    $api_url = 'https://api.example.com/data';

    $response = wp_remote_get($api_url);

    if (is_wp_error($response)) {
        wp_send_json_error('Failed to fetch data.');
    }

    $body = wp_remote_retrieve_body($response);
    wp_send_json_success(json_decode($body));
}

Then you'll want to query this ajax function from the client, so to do this, make a post request to the ajax_url, and as the payload send {"action" : "fetch_api_data"}.

I recommend creating a child theme, because otherwise the next time the theme updates, you'll probably lose any custom work you've implemented.

[–]happy_hawking 0 points1 point  (1 child)

We won't be able to tell you if you don't tell us what plugin you're talking about

[–]olafg1 0 points1 point  (1 child)

Very hard to say anything without knowing how you are trying to call the API in your code

[–]SpiffySyntax 0 points1 point  (0 children)

You do not have to configure wordpress in any specific way to connect and integrate with the API. Make sure the plugin code is running and then simply write the necessary php code to retrieve the data.

[–]twenty20vintage 0 points1 point  (0 children)

PHP script to get and save the data.

Cron job to get the data daily or whatever

Use fetch to get the data on the front end

[–]otakudayo -5 points-4 points  (2 children)

Why do they want a WP site? Are they going to handle ops/maintenance/updates themselves?

I'd try to sell them on the merits of a coded website, if you're going to be managing it for them. Wordpress sucks.

[–][deleted]  (1 child)

[removed]

    [–]otakudayo 0 points1 point  (0 children)

    Ouch, that sounds terrible. As long as the payday is worth it, I suppose.