PHPackages                             typisttech/wp-kses-view - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. typisttech/wp-kses-view

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

typisttech/wp-kses-view
=======================

Safely rendering for WordPress, the OOP way

0.5.0(7y ago)638.1k↓50%3[1 issues](https://github.com/typisttech/wp-kses-view/issues)4GPL-2.0-or-laterPHPPHP ^7.2

Since Oct 20Pushed 3y ago1 watchersCompare

[ Source](https://github.com/typisttech/wp-kses-view)[ Packagist](https://packagist.org/packages/typisttech/wp-kses-view)[ Docs](https://typist.tech/projects/wp-kses-view)[ RSS](/packages/typisttech-wp-kses-view/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (1)Versions (8)Used By (4)

WP Kses View
============

[](#wp-kses-view)

[![Latest Stable Version](https://camo.githubusercontent.com/6e8c0126a7e70f7fec2035577a23b97f29a344423099a094f31a4a6a823fa6e0/68747470733a2f2f706f7365722e707567782e6f72672f747970697374746563682f77702d6b7365732d766965772f762f737461626c65)](https://packagist.org/packages/typisttech/wp-kses-view)[![Total Downloads](https://camo.githubusercontent.com/3019086299bed22b168a9704050e7b0bd5cf4eb679d1bcd0514f835cd080059f/68747470733a2f2f706f7365722e707567782e6f72672f747970697374746563682f77702d6b7365732d766965772f646f776e6c6f616473)](https://packagist.org/packages/typisttech/wp-kses-view)[![Build Status](https://camo.githubusercontent.com/1bd7b3736d03b05a3a4cb5e7255a24438532ee3c312eff2598fc0ef0efd85485/68747470733a2f2f7472617669732d63692e6f72672f547970697374546563682f77702d6b7365732d766965772e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/TypistTech/wp-kses-view)[![License](https://camo.githubusercontent.com/5392bcf2d81b71269f6d30f122e705c7ffe6fbcf4fee8471a9d679073024467a/68747470733a2f2f706f7365722e707567782e6f72672f747970697374746563682f77702d6b7365732d766965772f6c6963656e7365)](https://packagist.org/packages/typisttech/wp-kses-view)[![Donate via PayPal](https://camo.githubusercontent.com/b57c445af971e3e99c2d0ccdbf4fa7faa4358ba27fecc8f68459b30289f82eda/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f446f6e6174652d50617950616c2d626c75652e737667)](https://typist.tech/donate/wp-kses-view/)[![Hire Typist Tech](https://camo.githubusercontent.com/e392a964bbdc0c32d95825bbc8253027387bcb9b021176d807d22ac75d86f308/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f486972652d547970697374253230546563682d6666363962342e737667)](https://typist.tech/contact/)

Safely rendering for WordPress, the OOP way.

- [Install](#install)
- [Usage](#usage)
    - [Static Example](#static-example)
    - [Render with `Context` Example](#render-with-context-example)
    - [View](#view)
        - [`__construct(string $template, array $allowedHtml)`](#__constructstring-template-array-allowedhtml)
        - [`render($context = null)`](#rendercontext--null)
        - [`toHtml($context = null): string`](#tohtmlcontext--null-string)
    - [Template](#template)
    - [Helpers](#helpers)
- [Frequently Asked Questions](#frequently-asked-questions)
    - [Why some HTML tags are stripped out?](#why-some-html-tags-are-stripped-out)
    - [Is this a plugin?](#is-this-a-plugin)
    - [What to do when wp.org plugin team tell me to clean up the `vendor` folder?](#what-to-do-when-wporg-plugin-team-tell-me-to-clean-up-the-vendor-folder)
    - [Can two different plugins use this package at the same time?](#can-two-different-plugins-use-this-package-at-the-same-time)
    - [Do you have real life examples that use this package?](#do-you-have-real-life-examples-that-use-this-package)
    - [It looks awesome. Where can I find some more goodies like this?](#it-looks-awesome-where-can-i-find-some-more-goodies-like-this)
- [Support](#support)
    - [Why don't you hire me?](#why-dont-you-hire-me)
    - [Want to help in other way? Want to be a sponsor?](#want-to-help-in-other-way-want-to-be-a-sponsor)
- [Developing](#developing)
- [Running the Tests](#running-the-tests)
- [Feedback](#feedback)
- [Change log](#change-log)
- [Security](#security)
- [Contributing](#contributing)
- [Credits](#credits)
- [License](#license)

Install
-------

[](#install)

Installation should be done via composer, details of how to install composer can be found at .

```
$ composer require typisttech/wp-kses-view
```

You should put all `WP Kses View` classes under your own namespace to avoid class name conflicts.

- [imposter-plugin](https://github.com/Typisttech/imposter-plugin)
- [mozart](https://github.com/coenjacobs/mozart)

Usage
-----

[](#usage)

### Static Example

[](#static-example)

```

Or, it can be plain HTML
alert('XSS hacking!');
```

```
use TypistTech\WPKsesView\View;

$template = '/path/to/template.php';
$view = new Factory::build($template);

$view->render();
// This echos:
// Hello World!
// Using PHP echo
// Or, it can be plain HTML
// alert('XSS hacking!');
```

Note that `` has been sanitized.

### Render with `Context` Example

[](#render-with-context-example)

```
// This is `template.php`.

printf(
    '%1$s has %2$d dragons.',
    $context->name,
    $context->dragons
);
```

```
use TypistTech\WPKsesView\View;

$template = '/path/to/template.php';
$context = (object) [
    'name' => 'Daenerys Targaryen',
    'dragons' => 3,
];
$view = new Factory::build($template);

$view->render($context);
// This echos:
// Daenerys Targaryen has 3 dragons.
```

### View

[](#view)

#### `__construct(string $template, array $allowedHtml)`

[](#__constructstring-template-array-allowedhtml)

`View` constructor.

- @param string $template Filename of the template to render.
- @param array $allowedHtml List of allowed HTML elements.

`$allowedHtml` will later be passed to [`wp_kses`](https://codex.wordpress.org/Function_Reference/wp_kses).

[`wp_kses_allowed_html('post')`](https://codex.wordpress.org/Function_Reference/wp_kses_allowed_html) is a good start if you not sure which HTML tags to use.

```
$template = '/path/to/my/template.php';

$view = new View(
    $template,
    wp_kses_allowed_html('post')
);
```

#### `render($context = null)`

[](#rendercontext--null)

Echo the view safely with optional context object.

- @param mixed $context Optional. Context object for which to render the view.

```
$view->render();

$view->render($someObject);
```

#### `toHtml($context = null): string`

[](#tohtmlcontext--null-string)

Convert the view to safe HTML.

- @param mixed $context Optional. Context object for which to render the view.

```
$html = $view->toHtml();

$htmlWithContext = $view->toHtml($someObject);
```

If you pass in a context object, you can reference it in your template as `$context`. Think `$context` as the `M` in MVC pattern.

### Template

[](#template)

A template can be anything, not limited to `.php` files. Common use cases are:

- `.php`
- `.html`
- `.js`

If you pass in a context object, you can reference it in your template as `$context`.

Think templates are `.erb` files under `app/view` directory in a Rails app.

### Helpers

[](#helpers)

This package provides `Factory`, `ViewAwareTrait` and `NullView` to reduce boilerplate code for common use cases. Check their well-documented source code and their tests to learn more.

Frequently Asked Questions
--------------------------

[](#frequently-asked-questions)

### Why some HTML tags are stripped out?

[](#why-some-html-tags-are-stripped-out)

This is the heart of this package, removing dangerous HTML tags during rendering.

To allow a HTML tag:

- Add the tag when instantiating a `view` object.

Check [`wp_kses`'s document](https://codex.wordpress.org/Function_Reference/wp_kses) to learn more.

When in doubt, [`wp_kses_allowed_html('post')`](https://codex.wordpress.org/Function_Reference/wp_kses_allowed_html) is a good start.

### Is this a plugin?

[](#is-this-a-plugin)

No, this is a package that should be part of your plugin.

### What to do when wp.org plugin team tell me to clean up the `vendor` folder?

[](#what-to-do-when-wporg-plugin-team-tell-me-to-clean-up-the-vendor-folder)

Re-install packages via the following command. This package exports only necessary files to `dist`.

```
$ composer install --no-dev --prefer-dist --optimize-autoloader
```

### Can two different plugins use this package at the same time?

[](#can-two-different-plugins-use-this-package-at-the-same-time)

Yes, if put all `WP Kses View` classes under your own namespace to avoid class name conflicts.

- [imposter-plugin](https://github.com/Typisttech/imposter-plugin)
- [mozart](https://github.com/coenjacobs/mozart)

### Do you have real life examples that use this package?

[](#do-you-have-real-life-examples-that-use-this-package)

Here you go:

- [Sunny](https://github.com/Typisttech/sunny)
- [WP Cloudflare Guard](https://github.com/TypistTech/wp-cloudflare-guard)
- [WP Better Settings](https://github.com/TypistTech/wp-better-settings)
- [WP Admin Tabs](https://github.com/TypistTech/wp-admin-tabs)
- [WP Tabbed Admin Pages](https://github.com/TypistTech/wp-tabbed-admin-pages)

*Add your own plugin [here](https://github.com/TypistTech/wp-kses-view/edit/master/README.md)*

### It looks awesome. Where can I find some more goodies like this?

[](#it-looks-awesome-where-can-i-find-some-more-goodies-like-this)

- Articles on Typist Tech's [blog](https://typist.tech)
- [Tang Rufus' WordPress plugins](https://profiles.wordpress.org/tangrufus#content-plugins) on wp.org
- More projects on [Typist Tech's GitHub profile](https://github.com/TypistTech)
- Stay tuned on [Typist Tech's newsletter](https://typist.tech/go/newsletter)
- Follow [Tang Rufus' Twitter account](https://twitter.com/TangRufus)
- Hire [Tang Rufus](https://typist.tech/contact) to build your next awesome site

Support
-------

[](#support)

Love `wp-kses-view`? Help me maintain it, a [donation here](https://typist.tech/donation/) can help with it.

### Why don't you hire me?

[](#why-dont-you-hire-me)

Ready to take freelance WordPress jobs. Contact me via the contact form [here](https://typist.tech/contact/) or, via email

### Want to help in other way? Want to be a sponsor?

[](#want-to-help-in-other-way-want-to-be-a-sponsor)

Contact: [Tang Rufus](mailto:tangrufus@gmail.com)

Developing
----------

[](#developing)

To setup a developer workable version you should run these commands:

```
$ composer create-project --keep-vcs --no-install typisttech/wp-kses-view:dev-master
$ cd wp-kses-view
$ composer install
```

Running the Tests
-----------------

[](#running-the-tests)

TODO: Re-add tests.

See:

Feedback
--------

[](#feedback)

**Please provide feedback!** We want to make this package useful in as many projects as possible. Please submit an [issue](https://github.com/TypistTech/wp-kses-view/issues/new) and point out what you do and don't like, or fork the project and make suggestions. **No issue is too small.**

Change log
----------

[](#change-log)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Security
--------

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

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

[](#contributing)

Please see [CONTRIBUTING](.github/CONTRIBUTING.md) and [CODE\_OF\_CONDUCT](./CODE_OF_CONDUCT.md) for details.

Credits
-------

[](#credits)

[WP Kses View](https://github.com/TypistTech/wp-kses-view) is a [Typist Tech](https://typist.tech) project and maintained by [Tang Rufus](https://twitter.com/Tangrufus), freelance developer for [hire](https://typist.tech/contact/).

Full list of contributors can be found [here](https://github.com/TypistTech/wp-kses-view/graphs/contributors).

License
-------

[](#license)

[WP Kses View](https://github.com/TypistTech/wp-kses-view) is licensed under the GPLv2 (or later) from the [Free Software Foundation](http://www.fsf.org/). Please see [License File](LICENSE) for more information.

###  Health Score

32

—

LowBetter than 71% of packages

Maintenance17

Infrequent updates — may be unmaintained

Popularity32

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity53

Maturing project, gaining track record

 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 ~91 days

Recently: every ~136 days

Total

7

Last Release

2573d ago

PHP version history (2 changes)0.1.0PHP &gt;7.0

0.4.2PHP ^7.2

### Community

Maintainers

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

---

Top Contributors

[![tangrufus](https://avatars.githubusercontent.com/u/2259834?v=4)](https://github.com/tangrufus "tangrufus (75 commits)")

---

Tags

wordpresswordpress-developmentwordpress-php-librarywordpresswp

### Embed Badge

![Health badge](/badges/typisttech-wp-kses-view/health.svg)

```
[![Health](https://phpackages.com/badges/typisttech-wp-kses-view/health.svg)](https://phpackages.com/packages/typisttech-wp-kses-view)
```

###  Alternatives

[justcoded/wordpress-theme-boilerplate

WordPress theme boilerplate with better code structure and OOP support.

563.9k1](/packages/justcoded-wordpress-theme-boilerplate)[typisttech/wp-better-settings

A simplified OOP implementation of the WP Settings API

1634.8k1](/packages/typisttech-wp-better-settings)[balbuf/composer-wp

Manage WordPress core, plugins, and themes with composer.

173.6k](/packages/balbuf-composer-wp)[typisttech/wp-admin-notices

A simplified OOP implementation of the WordPress admin notices

141.2k](/packages/typisttech-wp-admin-notices)

PHPackages © 2026

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