Skip to content

plugins_api Hook for Dynamic Plugin Updates 🚀

Hello again, You’ve already mastered the art of showing your plugin’s changelog statically using the plugins_api hook (or at least you will soon!). But now, we’re going to take things up a notch and do it dynamically! ⚡️

Imagine this: you’re sitting back with a cup of coffee, relaxing after deploying a fresh new version of your plugin, and WordPress automatically shows the latest changelog fetched from your custom update server. Yup, no more hardcoding version numbers or changelog entries every time an update happens! Automation at its finest.

Ready to make this magic happen? Let’s dive into the dynamic world of plugin updates! 🛠️

Step 1: Hook into plugins_api (Again, but Dynamic This Time)

Just like before, we’ll hook into the plugins_api filter, but this time, instead of showing static information, we’re going to fetch data from your server every time an update check is performed.

In your plugin’s main.php file, add this code:

php
add_filter('plugins_api', 'custom_dynamic_noob_plugin_info', 20, 3);

This tells WordPress: “Hey, listen to me! I’ve got the latest update info waiting on my custom server, just let me fetch it for you!” 🏃‍♂️💨

Step 2: Writing the Dynamic Fetching Function 🔮

Let’s modify the function to fetch the latest plugin information (like the changelog) from your custom server.

php
function custom_dynamic_noob_plugin_info($res, $action, $args) {
    // Ensure this is for YOUR plugin
    if ($action !== 'plugin_information' || $args->slug !== 'my-noob-plugin') {
        return $res;
    }

    // Define the response object if it's empty
    if (empty($res)) {
        $res = new stdClass();
    }

    // Fetch the latest plugin info from your update server (example URL)
    $response = wp_remote_get('https://example.com/api/plugin-info/my-noob-plugin');

    // Check for a valid response
    if (is_wp_error($response) || wp_remote_retrieve_response_code($response) != 200) {
        return $res; // If there's an error, return the default response
    }

    // Decode the server response
    $plugin_data = json_decode(wp_remote_retrieve_body($response));

    if (!$plugin_data) {
        return $res; // Return default if no data is received
    }

    // Now fill in the dynamic info fetched from the server
    $res->name = $plugin_data->name;
    $res->slug = $plugin_data->slug;
    $res->version = $plugin_data->version;
    $res->author = $plugin_data->author;
    $res->homepage = $plugin_data->homepage;

    // Use the dynamically fetched changelog
    $res->sections = array(
        'description' => $plugin_data->description,
        'changelog'   => $plugin_data->changelog,
        'faq'         => $plugin_data->faq
    );

    // Use the download link from the update server
    $res->download_link = $plugin_data->download_link;

    return $res;
}

What’s happening here? 🤔

  • We're using wp_remote_get to fetch information from your custom update server. The URL could be an API endpoint on your server that returns the plugin data in JSON format.
  • We check for errors (because nobody likes failed requests 😕), and if the data is successfully fetched, we decode it and assign the details to the $res object.
  • The plugin’s name, version, changelog, author, and even the download link are now dynamically pulled from your server. No more hardcoding! 🎉

Step 3: Create the Plugin Info on Your Server 🌐

Now, you need a way for your server to provide this information. Let’s assume you’ve created an API endpoint at:

https://example.com/api/plugin-info/my-noob-plugin

This endpoint should return JSON data that looks like this:

json
{
    "name": "My Noob Plugin",
    "slug": "my-noob-plugin",
    "version": "1.3.0",
    "author": "<a href='https://example.com'>Your Name</a>",
    "homepage": "https://example.com/my-noob-plugin",
    "description": "<p>This is my custom plugin with the latest update fetched dynamically!</p>",
    "changelog": "<h4>Version 1.3.0</h4><ul><li>🚀 Dynamic changelog is here! No more manual updates.</li><li>🐛 Fixed that annoying bug where the plugin refused to show its true potential.</li></ul>",
    "faq": "<h4>FAQs</h4><p>Q: Does this plugin solve world hunger? A: Not yet, but we're working on it!</p>",
    "download_link": "https://example.com/downloads/my-noob-plugin-v1.3.zip"
}

Make sure your server returns valid JSON with the correct structure, and you’re good to go! 🎉

Step 4: Testing Time! 🧪

  • After setting up your server API to return the latest plugin information, head over to your WordPress admin dashboard.
  • Go to the Plugins page and click View details for your plugin.
  • BOOM! Your dynamic changelog and plugin details should appear like magic. ✨

Step 5: Bragging Rights... Again! 🏆

Congratulations! You’ve now taken full control of your plugin’s update system and made it dynamic. No more manual edits to changelog data—everything’s fetched from your custom server on the fly! You’re now an update master 🧙‍♂️.

Step 6: Bonus Tips 🎯

  • Security tip: You might want to secure your API endpoint with authentication to prevent unauthorized access to your plugin data.
  • Caching tip: WordPress may cache the update info, so consider clearing transients to see instant changes while testing.
  • Adventure tip: Extend this by showing custom update notices, adding custom FAQs, or even pulling in screenshots from your server.

And that’s how you use the plugins_api hook to dynamically fetch changelog and update info from your own server! Now go forth, update responsibly, and have a blast creating more amazing features for your users! 🚀

Built by noobs, for noobs, with love 💻❤️