Select Page

Create a Custom API Endpoint in WordPress

Creating a custom endpoint in WordPress to check for plugin updates involves several steps. This process includes setting up a custom REST API endpoint, creating a function to handle requests to this endpoint, and ensuring that the response contains the necessary information about the plugin updates. Here’s a step-by-step guide:

1. Register a Custom REST API Endpoint

You need to register a custom endpoint in WordPress's REST API. This can be done by using the register_rest_route function.

Add the following code to your plugin or theme's functions.php file:

add_action('rest_api_init', function () {
    register_rest_route('custom/v1', '/plugin-updates/', array(
        'methods' => 'GET',
        'callback' => 'check_for_plugin_updates',
    ));
});

2. Create the Callback Function

Next, you need to create the callback function check_for_plugin_updates that will handle the request to your custom endpoint. This function will check for updates and return the relevant information.

Here’s an example of what the callback function might look like:

function check_for_plugin_updates() {
    // Define the plugin slug and the current version
    $plugin_slug = 'your-plugin-slug';
    $current_version = '1.0.0';

    // Simulate checking for updates (you would normally fetch this data from your server)
    $response = array(
        'new_version' => '1.1.0',
        'package' => 'https://example.com/path/to/your-plugin.zip',
        'slug' => $plugin_slug,
        'requires' => '5.0',
        'tested' => '5.7',
        'last_updated' => '2024-05-30',
        'sections' => array(
            'description' => 'This is the update description.',
            'installation' => 'Installation instructions here.',
            'changelog' => 'Changelog content here.',
        ),
    );

    // Check if there is an update
    if (version_compare($current_version, $response['new_version'], '<')) {
        return new WP_REST_Response($response, 200);
    } else {
        return new WP_REST_Response(array('message' => 'No updates available'), 200);
    }
}

3. Secure Your Endpoint

To ensure that only authorized requests can access your update information, you might want to secure your endpoint. You can do this by adding a permission callback function:

add_action('rest_api_init', function () {
    register_rest_route('custom/v1', '/plugin-updates/', array(
        'methods' => 'GET',
        'callback' => 'check_for_plugin_updates',
        'permission_callback' => function () {
            return current_user_can('manage_options'); // Adjust capability as needed
        },
    ));
});

4. Use the Custom Endpoint in Your Plugin

In your plugin's main file, you need to set up the plugin update mechanism to use the custom endpoint. Here’s an example of how you might do this:

add_filter('site_transient_update_plugins', 'check_custom_plugin_updates');

function check_custom_plugin_updates($transient) {
    if (empty($transient->checked)) {
        return $transient;
    }

    $plugin_slug = 'your-plugin-slug/your-plugin-file.php';
    $api_url = 'https://your-site.com/wp-json/custom/v1/plugin-updates/';

    $response = wp_remote_get($api_url);
    if (is_wp_error($response) || wp_remote_retrieve_response_code($response) !== 200) {
        return $transient;
    }

    $data = json_decode(wp_remote_retrieve_body($response));
    if (version_compare($transient->checked[$plugin_slug], $data->new_version, '<')) {
        $transient->response[$plugin_slug] = (object) array(
            'slug' => $data->slug,
            'new_version' => $data->new_version,
            'package' => $data->package,
            'url' => '',
        );
    }

    return $transient;
}

5. Testing the Endpoint

Ensure your custom endpoint is working by accessing it directly via a browser or a tool like curl or Postman:

https://your-site.com/wp-json/custom/v1/plugin-updates/

You should receive a JSON response with the plugin update information.

By following these steps, you can create a custom endpoint for WordPress to check for plugin updates, ensuring that your plugins can check for and receive updates from your custom source.

Check out more articles

Elevate Your Digital Presence with a Top Tampa Web Design Company

If you’re a business owner in Tampa, partnering with a professional web design company can make all the difference. In this article, we’ll explore the benefits of working with a Tampa web design company and how it can elevate your digital presence.

Deploy WordPress Updates from a Private Repository

To push code to a private repository and trigger a pending WordPress update badge, you need to follow a systematic approach involving version control, continuous integration/deployment, and the appropriate use of WordPress hooks and filters. Here’s a step-by-step...

How to Generate a Ranger Config File

ranger --copy-config=all This will copy all the default config files from /usr/local/lib so you can make edits without messing anything up. Huge shout out to https://dquinton.github.io/debian-install/config/ranger.html for explaining this and so much more. Took me a...

First Open Source Contribution

Surprise! I genuinely did not plan on making my first ever open source contribution today. In fact, I didn't plan on a lot of things happening this week at all. I recently got an awesome opportunity to work on a site that is being hosted on a platform called Pantheon,...

How to Exclude Specific Posts and Pages from WP_QUERY

For the sake of having something to work with, let's suppose we've got the following query:In this query, inside the $args array, there's a special parameter that allows you to exclude specific posts and pages from the query. This parameter is called post__not_in and...

Configure SSH For Password-less Connections

Preamble I'm slightly embarrassed to admit this, but it was a long time before I figured out how amazing this little file is and how exactly to put it to good use. The file I'm talking about is the SSH config file. By default, when you first connect to a server via...

WordPress Site Stuck in Maintenance Mode

Plugin Error Code 500 During Update While I was updating a WordPress plugin locally, something happened on my local server to trigger a 500 error code as the plugin was updating. On the frontend, I refreshed the page and was greeted with "Briefly Unavailable for...

Creating a WordPress Plugin Downloader

How it Started I was recently tasked with the challenge of creating a WordPress theme generator. The idea being, instead of writing your style.css file from scratch, you'd be able to just answer a few simple questions about how you'd like your theme set up, and those...

Fix Audio on XFCE Chromebook

The Problem After moving from GalliumOS to Xubuntu, I noticed that playing YouTube videos on both Firefox and Chromium would result in decent playback for a few minutes, but suddenly the audio would turn into a solid beeping tone while the YouTube video displayed the...

Adjust Trackpad Sensitivity XFCE

xinput set-prop "Elan Touchpad" "Synaptics Finger" 1 1 1 What are the values 1 1 1? This sets the sensitivity as close to the original ChromeOS as possible. Larger numbers will decrease sensitivity of various aspects. I never looked into which individual value...

Looking for a place to host your next project or design?

Use this link to get your first 4 months of WP Engine for free: WP Engine – Plans (With 4 months free)