Search by

Backdrop Attr provides devs a system for adding filterable attributes. This is sort of like `body_class()`, `post_class()`, and `comment_class()` on steroids.

Package info

github.com/backdrop-dev/attr

pkg:composer/backdrop-dev/attr

Statistics

Installs: 46

Dependents: 1

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.0 2026-11-17 21:08 UTC

This package is auto-updated.

Last update: 2026-11-17 21:09:03 UTC


README

Backdrop Attr builds filterable HTML attributes for any element. It is like body_class(), post_class() and comment_class(), but for any element you want, and every attribute can be changed with filters.

Requirements

Installation

composer require backdrop-dev/attr

Register the provider when you boot your application:

$app->provider( \Backdrop\Attr\Provider::class );

Usage

<body <?php Backdrop\Attr\display( 'body' ); ?>>
<article <?php Backdrop\Attr\display( 'post' ); ?>>
<aside <?php Backdrop\Attr\display( 'sidebar', 'primary', [ 'role' => 'complementary' ] ); ?>>
  • display( $name, $context = '', $attr = [] ) outputs the escaped attributes.
  • render( $name, $context = '', $attr = [] ) returns them as a string.
  • attr( $name, $context = '', $attr = [] ) returns the object, which supports get( 'id' ) and with( 'post', $post_id ).

Attribute values: a string or number is escaped and output, true outputs a bare attribute such as hidden, and false or null leaves the attribute out.

Without a custom class, the class is {name} {name}--{context}, for example sidebar sidebar--primary. The names html, body, post, entry and comment use the matching WordPress attributes and classes.

Filters

  • backdrop/attr/{name}/defaults filters the defaults before your input is merged in.
  • backdrop/attr and backdrop/attr/{name} filter the final array of attributes.
  • backdrop/attr/{name}/class filters the classes as an array.

Tests

php tests/run.php

License

GPL-2.0-or-later