Skip to content

Using dependencies in code snippets

Once you have installed a package through the Loopress plugin, it is available anywhere on your WordPress site, including in code snippets managed by plugins like Code Snippets.

Loopress stores its packages under wp-content/lps/vendor/. You need to require the Composer autoloader once before using any installed package:

require_once WP_CONTENT_DIR . '/lps/vendor/autoload.php';

Place this at the top of your snippet, before any use statements.

This example installs Guzzle and uses it to fetch data from an external API. The result is cached in a WordPress transient to avoid redundant HTTP calls.

1. Install the package

In the Loopress admin page, search for guzzlehttp/guzzle and install it.

2. Use it in a snippet

require_once WP_CONTENT_DIR . '/lps/vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
add_action('wp_footer', function () {
$client = new Client(['timeout' => 5]);
try {
$response = $client->get('https://jsonplaceholder.typicode.com/posts', [
'query' => ['_limit' => 5],
]);
$posts = json_decode($response->getBody()->getContents(), true);
echo '<ul>';
foreach ($posts as $post) {
echo '<li>' . esc_html($post['title']) . '</li>';
}
echo '</ul>';
} catch (GuzzleException $e) {
echo '<p>Error: ' . esc_html($e->getMessage()) . '</p>';
}
});
  • The autoloader path (WP_CONTENT_DIR . '/lps/vendor/autoload.php') is the same regardless of which package you install.
  • If the autoloader is missing (e.g. after a fresh WordPress install), run Repair from the Loopress admin page to regenerate it.
  • Always escape output with esc_html() or esc_url() before echoing data from external sources.