PHPackages                             arraypress/wp-cast-utils - 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. arraypress/wp-cast-utils

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

arraypress/wp-cast-utils
========================

A lightweight WordPress library for type casting and data conversion

115PHP

Since Oct 3Pushed 7mo agoCompare

[ Source](https://github.com/arraypress/wp-cast-utils)[ Packagist](https://packagist.org/packages/arraypress/wp-cast-utils)[ RSS](/packages/arraypress-wp-cast-utils/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

WordPress Cast Utilities
========================

[](#wordpress-cast-utilities)

A lightweight WordPress library for smart type casting and data conversion. Focuses on genuinely useful conversions with special handling for WordPress-specific formats.

Features
--------

[](#features)

- 🎯 **Focused API**: Only the conversions that add real value
- 🔧 **WordPress-Ready**: Handles serialized data, JSON, and CSV
- ⚡ **Smart Conversions**: Intelligent array and number detection
- 🛡️ **Type Safety**: Predictable return types
- 📊 **Meta Support**: Direct casting of WordPress meta values

Requirements
------------

[](#requirements)

- PHP 7.4 or later
- WordPress 5.0 or later

Installation
------------

[](#installation)

```
composer require arraypress/wp-cast-utils
```

Basic Usage
-----------

[](#basic-usage)

### Type Casting

[](#type-casting)

```
use ArrayPress\CastUtils\Cast;

Boolean conversion (handles 'yes'/'no', 'true'/'false', etc.)
Cast::to_bool( 'yes' ); true
Cast::to_bool( 'false' ); false
Cast::to_bool( 1 ); true

Smart number conversion (preserves int vs float)
Cast::to_number( '123' ); 123 (int)
Cast::to_number( '123.45' ); 123.45 (float)
Cast::to_number( '-67', true ); 67 (absolute)
```

### Array Conversions

[](#array-conversions)

```
Smart array conversion - tries multiple formats
Cast::to_array( 'a,b,c' ); ['a', 'b', 'c'] (CSV)
Cast::to_array( '{"x":1,"y":2}' ); ['x' => 1, 'y' => 2] (JSON)
Cast::to_array( 'a:2:{i:0;s:1:"a";i:1;s:1:"b";}' ); ['a', 'b'] (serialized)

Array to CSV
Cast::to_csv( ['apple', 'banana', 'cherry'] ); 'apple,banana,cherry'
Cast::to_csv( ['red', 'green', 'blue'], '|' ); 'red|green|blue'
```

### JSON Conversion

[](#json-conversion)

```
JSON encoding with WordPress functions
Cast::to_json( ['name' => 'John', 'age' => 30] );
'{"name":"John","age":30}'

Pretty JSON for debugging
Cast::to_json( ['name' => 'John'], true );
{
    "name": "John"
}
```

### WordPress Meta Casting

[](#wordpress-meta-casting)

```
Cast meta values directly to the type you need
$featured = Cast::meta( 'post', $post_id, 'featured', 'bool' );
$tags = Cast::meta( 'post', $post_id, 'tags', 'array' );
$views = Cast::meta( 'post', $post_id, 'view_count', 'number' );

Works with options too
$settings = Cast::meta( 'option', 'my_plugin_settings', '', 'array' );
```

API Reference
-------------

[](#api-reference)

### Core Methods

[](#core-methods)

MethodDescriptionReturns`to_bool($value)`Convert to boolean with smart string handling`bool``to_number($value, $absolute = false)`Smart int/float detection`int|float``to_array($value)`Smart array conversion (JSON, CSV, serialized)`array``to_json($value, $pretty = false)`Convert to JSON string`string|null``to_csv($array, $separator = ',')`Convert array to CSV string`string`### Meta Method

[](#meta-method)

MethodDescriptionReturns`meta($type, $id, $key, $cast_type, $default = null)`Cast WordPress meta to type`mixed`**Meta Types Supported:**

- `'post'` - Post meta
- `'user'` - User meta
- `'term'` - Term meta
- `'option'` - WordPress options

**Cast Types:**

- `'array'` - Smart array conversion
- `'bool'` - Boolean
- `'number'` - Smart int/float
- `'int'` - Integer
- `'float'` - Float
- `'string'` - String

Common Use Cases
----------------

[](#common-use-cases)

### Form Data Processing

[](#form-data-processing)

```
Handle various input formats intelligently
$age = Cast::to_number( $_POST['age'] ?? 0 );
$interests = Cast::to_array( $_POST['interests'] ?? '' ); Handles CSV or array
$newsletter = Cast::to_bool( $_POST['newsletter'] ?? false );
```

### API Responses

[](#api-responses)

```
Prepare clean data for JSON APIs
$response = [
    'user_id' => Cast::to_number( $user->ID ),
    'is_active' => Cast::to_bool( $user_meta['active'] ),
    'tags' => Cast::to_array( $user_meta['tags'] )
];

return wp_send_json( $response );
```

### Plugin Settings

[](#plugin-settings)

```
Handle plugin options reliably
$enabled = Cast::meta( 'option', 'my_plugin_enabled', '', 'bool', false );
$max_items = Cast::meta( 'option', 'my_plugin_max', '', 'number', 10 );
$allowed_types = Cast::meta( 'option', 'my_plugin_types', '', 'array', [] );
```

Why This Library?
-----------------

[](#why-this-library)

Unlike generic PHP casting, this library:

- **Handles WordPress formats**: Serialized data, meta values
- **Smart detection**: Preserves int vs float types
- **Multiple format support**: JSON, CSV, serialized in one method
- **Clean API**: Predictable, easy-to-remember method names
- **No exceptions**: Safe conversions that won't break your site

Requirements
------------

[](#requirements-1)

- PHP 7.4+
- WordPress 5.0+

License
-------

[](#license)

GPL-2.0-or-later

Support
-------

[](#support)

- [Documentation](https:github.com/arraypress/wp-cast-utils)
- [Issue Tracker](https:github.com/arraypress/wp-cast-utils/issues)

###  Health Score

18

—

LowBetter than 8% of packages

Maintenance44

Moderate activity, may be stable

Popularity7

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity13

Early-stage or recently created project

 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.

### Community

Maintainers

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

---

Top Contributors

[![arraypress](https://avatars.githubusercontent.com/u/22668877?v=4)](https://github.com/arraypress "arraypress (2 commits)")

### Embed Badge

![Health badge](/badges/arraypress-wp-cast-utils/health.svg)

```
[![Health](https://phpackages.com/badges/arraypress-wp-cast-utils/health.svg)](https://phpackages.com/packages/arraypress-wp-cast-utils)
```

###  Alternatives

[panakour/analytics

Get whatever data you want from google analytics.

267.6k](/packages/panakour-analytics)[kakuilan/php-helper

k`s php helper/library/utils

314.8k3](/packages/kakuilan-php-helper)[damodar-bhattarai/settings

Package to store and get general website settings on the fly.

312.2k](/packages/damodar-bhattarai-settings)

PHPackages © 2026

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