Skip to content

msls_get_switcher() should accept no arguments — $attr is effectively optional but lacks a default #643

@apermo

Description

@apermo

Summary

msls_get_switcher() is the documented public template tag for rendering the
language switcher from theme code. Its signature requires a positional
argument, but the body already treats that argument as optional — any
non-array value is coerced to an empty array. As a result, the natural no-arg
call msls_get_switcher() throws a fatal ArgumentCountError even though it
would otherwise work fine.

Current behavior

includes/api.php (as of 3.0.0):

function msls_get_switcher( $attr ): string {
    $arr = is_array( $attr ) ? $attr : array();   // non-array already → empty array
    $obj = apply_filters( 'msls_get_output', null );

    return ! is_null( $obj ) ? strval( $obj->set_tags( $arr ) ) : '';
}

Calling it without arguments:

echo msls_get_switcher();

fatals the page with:

ArgumentCountError: Too few arguments to function msls_get_switcher(),
0 passed and exactly 1 expected

Expected behavior

The no-arg call should work. Theme authors reasonably expect it to, because:

  • the body already normalizes anything non-array to array(), so $attr is
    effectively optional;
  • the sibling template tag msls_the_switcher( array $arr = array() ) already
    defaults its argument.

Use case

A theme rendering the switcher with default markup just wants:

echo msls_get_switcher();

Today it has to pass msls_get_switcher( array() ) purely to satisfy the
signature.

Proposed change

Give the parameter a default, matching msls_the_switcher():

function msls_get_switcher( $attr = array() ): string {

The parameter is intentionally left untyped so the existing is_array()
coercion keeps working. This preserves the [sc_msls] shortcode
(add_shortcode( 'sc_msls', 'msls_get_switcher' )), which WordPress invokes
with the empty string '' when no attributes are given — an array type hint
would break that case. Existing msls_get_switcher( $attr ) callers and the
deprecated get_the_msls( $attr ) shim are unaffected.

Backward compatible; no behavior change for any current caller.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions