Pass in Closure for Hooks

Open
#235 4 comments 0 reactions 0 assignees View on GitHub

@faisalahammad is already working on this.

Since Jul 19, 2026.

  • #272 by @faisalahammad — open

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
php
Domain
testing

Research direction

Start at the WP_Mock::expectFilter entry point and compare how WP_Mock::userFunction handles a Closure return value. Review the existing hook expectation tests and add coverage for a Closure supplied as the return value with a call count. Done means hook callbacks can return values dynamically across repeated calls, with the existing behavior preserved.

Written by the indexing model from the issue text.

Description

Enhancement Question

Feature request

At the moment we can currently pass a Closure as the return value when mocking functions using the \WP_Mock::userFunction like so:

WP_Mock::userFunction(
    'get_post_meta',
    [
        'times' => 3,
        'return' => function( $post_id, $key, $single ) {
            if ( $key === 'some_meta_key' ) {
                return 'some value';
            }
        
            return 'another value';
        }
    ]
);

This really comes in handy when we are in a loop situation and we need to do this several times.

Proposed solution

Can this same approach be employed for hooks? It would be very handy to do this in one swoop, just like its userFunction counterpart.

WP_Mock::expectFilter(
    'custom_post_types',
    [
        'times' => 3,
        'return' => function( $post_type ) {
            return $post_type
        }
    ]
)
Dominant language
PHP
Stars
714
Forks
72
PR merge metrics
No merged PRs in 30d

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from 10up/wp_mock

All issues in 10up/wp_mock

Similar issues

More PHP issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.