PHPackages                             snicco/better-wp-api - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. snicco/better-wp-api

ActiveLibrary[API Development](/categories/api)

snicco/better-wp-api
====================

This small package tries to solve the pain point of not being able to solve to scope code that references WordPress core code.

v1.10.1(1y ago)119.7k↓100%13LGPL-3.0-onlyPHPPHP ^7.4|^8.0

Since Apr 17Pushed 1y ago1 watchersCompare

[ Source](https://github.com/snicco/better-wp-api)[ Packagist](https://packagist.org/packages/snicco/better-wp-api)[ RSS](/packages/snicco-better-wp-api/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (3)Versions (35)Used By (13)

BetterWPAPI - Interact with WordPress core while keeping your code testable.
============================================================================

[](#betterwpapi---interact-with-wordpress-core-while-keeping-your-code-testable)

[![codecov](https://camo.githubusercontent.com/a99e6ec528fffd1664e95534f9a09a4a09d2afe62799ff0d8774dc22d8453f6c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f436f7665726167652d3130302532352d73756363657373)](https://codecov.io/gh/snicco/snicco)[![Psalm Type-Coverage](https://camo.githubusercontent.com/c12cfed65c7da16501f7a84e7861b8c4757fc30e9dc00bb2983783dbb3f3f84c/68747470733a2f2f73686570686572642e6465762f6769746875622f736e6963636f2f736e6963636f2f636f7665726167652e7376673f)](https://shepherd.dev/github/snicco/snicco)[![Psalm level](https://camo.githubusercontent.com/c5e90ffcf3a5aa1f78f93bddde5db7627b114329393aa87697df8cedc7f5391a/68747470733a2f2f73686570686572642e6465762f6769746875622f736e6963636f2f736e6963636f2f6c6576656c2e7376673f)](https://psalm.dev/)[![PhpMetrics - Static Analysis](https://camo.githubusercontent.com/364ffb28ea219affd0fed2e99cc046bac0bf41da3f1d3814e0cbe4a4bb54c994/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5068704d6574726963732d5374617469635f416e616c797369732d326561343466)](https://snicco.github.io/snicco/phpmetrics/BetterWPAPI/index.html)[![PHP-Versions](https://camo.githubusercontent.com/241a10d25aa09d5e8a82ebd2b55780a63dd43736d958d4004c3166e650874aca/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d253545372e34253743253545382e30253743253545382e312d626c7565)](https://camo.githubusercontent.com/241a10d25aa09d5e8a82ebd2b55780a63dd43736d958d4004c3166e650874aca/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d253545372e34253743253545382e30253743253545382e312d626c7565)

**BetterWPAPI** is a single class that contains proxy methods for some of the most use **WordPress** core methods.

It's meant to be used in **distributed** **WordPress libraries** and helps you to keep your library testable and [scopable](https://github.com/humbug/php-scoper).

Table of contents
-----------------

[](#table-of-contents)

1. [Motivation](#motivation)
2. [Installation](#installation)
3. [Usage](#usage)
4. [Contributing](#contributing)
5. [Issues and PR's](#reporting-issues-and-sending-pull-requests)
6. [Security](#security)

Motivation
----------

[](#motivation)

We developed this library for the **WordPress** related components of the [**Snicco**](https://github.com/snicco/snicco) project.

Directly interacting with core functions was problematic for us because:

- Code becomes untestable without
    - a) booting the entire **WordPress** codebase or
    - b) using additional mocking frameworks ([mocking sucks](https://twitter.com/icooper/status/1036527957798412288)).
- The code becomes[ really hard to scope](https://github.com/humbug/php-scoper/issues/303) using the de-facto standard tool [PHPScoper](https://github.com/humbug/php-scoper).
- Static analysers like **Psalm** and **PHPStan** go crazy over core functions.

If you can't relate to these issues, you probably don't need this library.

**Edit:** The scoping problem was solved, by us creating a [command-line-programm](https://github.com/snicco/php-scoper-excludes) that can be used to generate a [list of all functions and classes](https://github.com/snicco/php-scoper-wordpress-excludes/blob/master/generated/exclude-wordpress-functions.json)in the entire **WordPress** core codebase. For now, the best example on how to use it is [the `scoper.inc.php` configuration](https://github.com/GoogleForCreators/web-stories-wp/blob/main/scoper.inc.php#L13)of the [Google Web stories plugin](https://github.com/GoogleForCreators/web-stories-wp).

Installation
------------

[](#installation)

```
composer require snicco/better-wp-api
```

Usage
-----

[](#usage)

[This is the public API](https://github.com/snicco/better-wp-api/blob/master/src/BetterWPAPI.php#L36) of **BetterWPAPI**. No public or protected methods will be added until a next major version.

The idea is to limit the interaction with **WordPress** core to this class.

This has worked very well for us in order to keep our code testable and scopable. You have a single class where you can see all interaction that your library has with **core**. During tests, you can then simply swap this class with a test double.

The important thing is that: **All classes that need anything from WordPress must accept this class as a constructor dependency.**

A simple example: (for a real example, check out the [**BetterWPMail**](https://github.com/snicco/snicco/blob/licensing-and-docs/src/Snicco/Component/better-wp-mail/src/WPMailAPI.php)component)

Assuming we have the following class:

```
use Snicco\Component\BetterWPAPI\BetterWPAPI;

class CSVImporter {

    public function __construct(BetterWPAPI $wp = null) {
        $this->wp = $wp ?: new BetterWPAPI();
    }

    public function import(string $file){

        if(!$this->wp->currentUserCan('import-csv')) {
            throw new Exception('Not authorized');
        }
        // import csv.
    }

}
```

This is how we use it in production code.

```
$importer = new CSVImporter();

$importer->import(__DIR__.'/orders.csv');
```

This is how we test the code. No bootstrapping **WordPress** needed.

```
class CSVImporterTest extends TestCase {

   /**
    * @test
    */
    public function that_missing_permissions_throw_an_exception() {

        $this->expectExceptionMessage('Not authorized');

        $wp = new class extends BetterWPAPI {
            public function currentUserCan():bool {
                return false;
            }
        }

        $importer = new CSVImporter($wp);
        $importer->import(__DIR__.'/test-users.csv');
    }

}
```

Contributing
------------

[](#contributing)

This repository is a read-only split of the development repo of the [**Snicco** project](https://github.com/snicco/snicco).

[This is how you can contribute](https://github.com/snicco/snicco/blob/master/CONTRIBUTING.md).

Reporting issues and sending pull requests
------------------------------------------

[](#reporting-issues-and-sending-pull-requests)

Please report issues in the [**Snicco** monorepo](https://github.com/snicco/snicco/blob/master/CONTRIBUTING.md##using-the-issue-tracker).

Security
--------

[](#security)

If you discover a security vulnerability within **BetterWPAPI**, please follow our [disclosure procedure](https://github.com/snicco/snicco/blob/master/SECURITY.md).

###  Health Score

38

—

LowBetter than 84% of packages

Maintenance36

Infrequent updates — may be unmaintained

Popularity26

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity65

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~27 days

Recently: every ~1 days

Total

33

Last Release

609d ago

Major Versions

v1.10.0 → v2.0.0-beta.12024-09-01

v1.10.1 → v2.0.0-beta.72024-09-04

### Community

Maintainers

![](https://www.gravatar.com/avatar/d659a021c9077ceb60e6ab1ea4287b9f209edb97e93bceceb7fb539d8302e894?d=identicon)[calvinalkan](/maintainers/calvinalkan)

---

Top Contributors

[![snicco-bot](https://avatars.githubusercontent.com/u/101470239?v=4)](https://github.com/snicco-bot "snicco-bot (25 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/snicco-better-wp-api/health.svg)

```
[![Health](https://phpackages.com/badges/snicco-better-wp-api/health.svg)](https://phpackages.com/packages/snicco-better-wp-api)
```

###  Alternatives

[stripe/stripe-php

Stripe PHP Library

4.0k143.3M475](/packages/stripe-stripe-php)[twilio/sdk

A PHP wrapper for Twilio's API

1.6k92.9M270](/packages/twilio-sdk)[knplabs/github-api

GitHub API v3 client

2.2k15.8M186](/packages/knplabs-github-api)[facebook/php-business-sdk

PHP SDK for Facebook Business

90121.9M34](/packages/facebook-php-business-sdk)[microsoft/microsoft-graph

The Microsoft Graph SDK for PHP

65723.5M95](/packages/microsoft-microsoft-graph)[meilisearch/meilisearch-php

PHP wrapper for the Meilisearch API

73813.7M114](/packages/meilisearch-meilisearch-php)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
