Skip to content

Syntax error in Usage Example for wp_register_ability_args filter (missing semicolon) #2110

@itsgajendraSingh

Description

@itsgajendraSingh

Issue Summary

On the Abilities API Hooks documentation page:
https://developer.wordpress.org/apis/abilities-api/hooks/#usage-example-5

The Usage Example provided for the wp_register_ability_args filter contains a PHP syntax error.
The example is missing a semicolon after the closure assigned to $args['permission_callback'].

This results in a fatal parse error when the code is copied into a plugin.


Problematic example from the docs

The code currently ends like this:

$args['permission_callback' ] = static function ( $input = null ) use ( $args, $ability_name ) {
    $previous_check = is_callable( $args['permission_callback'] ) ? $args['permission_callback']( $input ) : true;

    // If we already failed, no need for stricter checks.
    if ( ! $previous_check || is_wp_error( $previous_check ) ) {
        return $previous_check;
    }

    return current_user_can( 'my_custom_ability_cap', $ability_name );
}
    
return $args;

The } that ends the anonymous function must be followed by a semicolon.

Actual code copied from the page that triggers the error

add_filter( 'wp_register_ability_args', 'my_modify_ability_args', 10, 2 );

function my_modify_ability_args( array $args, string $ability_name ): array {
    if ( 'my-namespace/my-ability' !== $ability_name ) {
      return $args;
    }

    $args['label'] = __('My Custom Ability Label');

    $args['description'] = sprintf(
        __('This is a custom description for the ability %s. Previously the description was %s', 'text-domain'),
        $ability_name,
        $args['description'] ?? 'N/A'
    );

    $args['permission_callback' ] = static function ( $input = null ) use ( $args, $ability_name ) {
        $previous_check = is_callable( $args['permission_callback'] ) ? $args['permission_callback']( $input ) : true;

        if ( ! $previous_check || is_wp_error( $previous_check ) ) {
            return $previous_check;
        }

        return current_user_can( 'my_custom_ability_cap', $ability_name );
    }  // <-- semicolon is missing here

    return $args;
}

Error produced

Parse error: syntax error, unexpected token "return"

Expected fix

Add the missing semicolon after the closure:

};
return $args;

Metadata

Metadata

Labels

APIsIssues for Common APIs Handbook[Status] DoneIssue is completeddeveloper documentation (DevHub)Improvements or additions to developer documentation

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions