Using WordPress ‘plugins_api_result’ PHP filter

plugins_api_result is a WordPress PHP filter that allows you to modify the Plugin Installation API response results.

Usage

function my_plugins_api_result( $res, $action, $args ) {
    return $res;
}
add_filter( 'plugins_api_result', 'my_plugins_api_result', 10, 3 );

Parameters

  • $res (object|WP_Error): The response object or WP_Error to be modified.
  • $action (string): The type of information being requested from the Plugin Installation API.
  • $args (object): Plugin API arguments.

Examples

Change plugin details

function modify_plugin_details( $res, $action, $args ) {
    if ( $action == 'plugin_information' && isset( $res->slug ) && $res->slug == 'your-plugin-slug' ) {
        $res->name = 'New Plugin Name';
        $res->author = 'New Author Name';
    }
    return $res;
}
add_filter( 'plugins_api_result', 'modify_plugin_details', 10, 3 );

In this example, we modify the name and author of a specific plugin using its slug. If the action is ‘plugin_information’ and the slug matches our target plugin, we change the name and author.

Add custom data to the response

function add_custom_data( $res, $action, $args ) {
    $res->custom_data = 'This is custom data added to the response';
    return $res;
}
add_filter( 'plugins_api_result', 'add_custom_data', 10, 3 );

Here, we add custom data to the response object for all plugins by adding a new property custom_data with the desired value.

Filter plugins by a specific author

function filter_plugins_by_author( $res, $action, $args ) {
    if ( $action == 'query_plugins' ) {
        $filtered_plugins = array();
        foreach ( $res->plugins as $plugin ) {
            if ( $plugin->author == 'Target Author' ) {
                $filtered_plugins[] = $plugin;
            }
        }
        $res->plugins = $filtered_plugins;
    }
    return $res;
}
add_filter( 'plugins_api_result', 'filter_plugins_by_author', 10, 3 );

In this example, we filter the plugins list to show only plugins by a specific author when the action is ‘query_plugins’.

Remove a plugin from the results

function remove_plugin_from_results( $res, $action, $args ) {
    if ( $action == 'query_plugins' ) {
        $filtered_plugins = array();
        foreach ( $res->plugins as $plugin ) {
            if ( $plugin->slug != 'plugin-to-remove' ) {
                $filtered_plugins[] = $plugin;
            }
        }
        $res->plugins = $filtered_plugins;
    }
    return $res;
}
add_filter( 'plugins_api_result', 'remove_plugin_from_results', 10, 3 );

This example shows how to remove a specific plugin from the results by checking its slug and filtering it out from the plugins array.

Modify plugin rating

function modify_plugin_rating( $res, $action, $args ) {
    if ( $action == 'plugin_information' && isset( $res->slug ) && $res->slug == 'your-plugin-slug' ) {
        $res->rating = 100;
    }
    return $res;
}
add_filter( 'plugins_api_result', 'modify_plugin_rating', 10, 3 ); 

In this example, we modify the rating of a specific plugin using its slug. If the action is ‘plugin_information’ and the slug matches our target plugin, we change the rating to 100.

Change plugin icons

function change_plugin_icons( $res, $action, $args ) {
    if ( $action == 'plugin_information' && isset( $res->slug ) && $res->slug == 'your-plugin-slug' ) {
        $res->icons = array(
            '1x' => 'https://example.com/your-icon-128x128.png',
            '2x' => 'https://example.com/your-icon-256x256.png',
            'svg' => 'https://example.com/your-icon.svg'
        );
    }
    return $res;
}
add_filter( 'plugins_api_result', 'change_plugin_icons', 10, 3 );

In this example, we change the plugin icons using custom image URLs. If the action is ‘plugin_information’ and the slug matches our target plugin, we modify the icons array with the new URLs.

Hide a specific plugin version

function hide_specific_plugin_version( $res, $action, $args ) {
    if ( $action == 'plugin_information' && isset( $res->slug ) && $res->slug == 'your-plugin-slug' ) {
        if ( $res->version == '1.0.0' ) {
            $res->version = '1.0.1';
        }
    }
    return $res;
}
add_filter( 'plugins_api_result', 'hide_specific_plugin_version', 10, 3 );

In this example, we hide a specific version of a plugin. If the action is ‘plugin_information’, the slug matches our target plugin, and the version is ‘1.0.0’, we change the version to ‘1.0.1’.

Add custom tags to plugins

function add_custom_tags_to_plugins( $res, $action, $args ) {
    if ( $action == 'query_plugins' ) {
        foreach ( $res->plugins as $plugin ) {
            if ( $plugin->slug == 'your-plugin-slug' ) {
                $plugin->tags['custom_tag'] = 'Custom Tag';
            }
        }
    }
    return $res;
}
add_filter( 'plugins_api_result', 'add_custom_tags_to_plugins', 10, 3 );

This example demonstrates how to add custom tags to specific plugins. If the action is ‘query_plugins’ and the plugin’s slug matches our target plugin, we add a new tag to the tags array.