Using WordPress ‘options_reading_blog_charset()’ PHP function

The options_reading_blog_charset() WordPress PHP function retrieves the site charset setting.

Usage

echo options_reading_blog_charset();

Parameters

  • None

More information

See WordPress Developer Resources: options_reading_blog_charset

Examples

Display the current site charset

// Display the current site charset
echo 'The current site charset is: ' . options_reading_blog_charset();

Set the Content-Type header using the site charset

// Set the Content-Type header using the site charset
header('Content-Type: text/html; charset=' . options_reading_blog_charset());

Use the site charset for a custom meta tag

// Add a custom meta tag with the site charset
echo '<meta http-equiv="Content-Type" content="text/html; charset=' . options_reading_blog_charset() . '" />';

Output JSON with the site charset

// Output JSON with the site charset
header('Content-Type: application/json; charset=' . options_reading_blog_charset());
echo json_encode(array('message' => 'Hello, World!'));

Convert a string to the site charset

// Convert a string to the site charset
$original_string = 'Hello, World!';
$converted_string = mb_convert_encoding($original_string, options_reading_blog_charset(), 'UTF-8');
echo $converted_string;