Title: wpdb::get_table_charset
Published: April 23, 2015
Last modified: February 24, 2026

---

# wpdb::get_table_charset( string $table ): string|󠀁[WP_Error](https://developer.wordpress.org/reference/classes/wp_error/)󠁿

## In this article

 * [Parameters](https://developer.wordpress.org/reference/classes/wpdb/get_table_charset/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/classes/wpdb/get_table_charset/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/classes/wpdb/get_table_charset/?output_format=md#source)
 * [Hooks](https://developer.wordpress.org/reference/classes/wpdb/get_table_charset/?output_format=md#hooks)
 * [Related](https://developer.wordpress.org/reference/classes/wpdb/get_table_charset/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/classes/wpdb/get_table_charset/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/classes/wpdb/get_table_charset/?output_format=md#wp--skip-link--target)

Retrieves the character set for the given table.

## 󠀁[Parameters](https://developer.wordpress.org/reference/classes/wpdb/get_table_charset/?output_format=md#parameters)󠁿

 `$table`stringrequired

Table name.

## 󠀁[Return](https://developer.wordpress.org/reference/classes/wpdb/get_table_charset/?output_format=md#return)󠁿

 string|[WP_Error](https://developer.wordpress.org/reference/classes/wp_error/) 
Table character set, [WP_Error](https://developer.wordpress.org/reference/classes/wp_error/)
object if it couldn’t be found.

## 󠀁[Source](https://developer.wordpress.org/reference/classes/wpdb/get_table_charset/?output_format=md#source)󠁿

    ```php
    protected function get_table_charset( $table ) {
    	$tablekey = strtolower( $table );

    	/**
    	 * Filters the table charset value before the DB is checked.
    	 *
    	 * Returning a non-null value from the filter will effectively short-circuit
    	 * checking the DB for the charset, returning that value instead.
    	 *
    	 * @since 4.2.0
    	 *
    	 * @param string|WP_Error|null $charset The character set to use, WP_Error object
    	 *                                      if it couldn't be found. Default null.
    	 * @param string               $table   The name of the table being checked.
    	 */
    	$charset = apply_filters( 'pre_get_table_charset', null, $table );
    	if ( null !== $charset ) {
    		return $charset;
    	}

    	if ( isset( $this->table_charset[ $tablekey ] ) ) {
    		return $this->table_charset[ $tablekey ];
    	}

    	$charsets = array();
    	$columns  = array();

    	$table_parts = explode( '.', $table );
    	$table       = '`' . implode( '`.`', $table_parts ) . '`';
    	$results     = $this->get_results( "SHOW FULL COLUMNS FROM $table" );
    	if ( ! $results ) {
    		return new WP_Error( 'wpdb_get_table_charset_failure', __( 'Could not retrieve table charset.' ) );
    	}

    	foreach ( $results as $column ) {
    		$columns[ strtolower( $column->Field ) ] = $column;
    	}

    	$this->col_meta[ $tablekey ] = $columns;

    	foreach ( $columns as $column ) {
    		if ( ! empty( $column->Collation ) ) {
    			list( $charset ) = explode( '_', $column->Collation );

    			$charsets[ strtolower( $charset ) ] = true;
    		}

    		list( $type ) = explode( '(', $column->Type );

    		// A binary/blob means the whole query gets treated like this.
    		if ( in_array( strtoupper( $type ), array( 'BINARY', 'VARBINARY', 'TINYBLOB', 'MEDIUMBLOB', 'BLOB', 'LONGBLOB' ), true ) ) {
    			$this->table_charset[ $tablekey ] = 'binary';
    			return 'binary';
    		}
    	}

    	// utf8mb3 is an alias for utf8.
    	if ( isset( $charsets['utf8mb3'] ) ) {
    		$charsets['utf8'] = true;
    		unset( $charsets['utf8mb3'] );
    	}

    	// Check if we have more than one charset in play.
    	$count = count( $charsets );
    	if ( 1 === $count ) {
    		$charset = key( $charsets );
    	} elseif ( 0 === $count ) {
    		// No charsets, assume this table can store whatever.
    		$charset = false;
    	} else {
    		// More than one charset. Remove latin1 if present and recalculate.
    		unset( $charsets['latin1'] );
    		$count = count( $charsets );
    		if ( 1 === $count ) {
    			// Only one charset (besides latin1).
    			$charset = key( $charsets );
    		} elseif ( 2 === $count && isset( $charsets['utf8'], $charsets['utf8mb4'] ) ) {
    			// Two charsets, but they're utf8 and utf8mb4, use utf8.
    			$charset = 'utf8';
    		} else {
    			// Two mixed character sets. ascii.
    			$charset = 'ascii';
    		}
    	}

    	$this->table_charset[ $tablekey ] = $charset;
    	return $charset;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/class-wpdb.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/class-wpdb.php#L3211)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/class-wpdb.php#L3211-L3298)

## 󠀁[Hooks](https://developer.wordpress.org/reference/classes/wpdb/get_table_charset/?output_format=md#hooks)󠁿

 [apply_filters( ‘pre_get_table_charset’, string|WP_Error|null $charset, string $table )](https://developer.wordpress.org/reference/hooks/pre_get_table_charset/)

Filters the table charset value before the DB is checked.

## 󠀁[Related](https://developer.wordpress.org/reference/classes/wpdb/get_table_charset/?output_format=md#related)󠁿

| Uses | Description | 
| [__()](https://developer.wordpress.org/reference/functions/__/)`wp-includes/l10n.php` |

Retrieves the translation of $text.

  | 
| [apply_filters()](https://developer.wordpress.org/reference/functions/apply_filters/)`wp-includes/plugin.php` |

Calls the callback functions that have been added to a filter hook.

  | 
| [wpdb::get_results()](https://developer.wordpress.org/reference/classes/wpdb/get_results/)`wp-includes/class-wpdb.php` |

Retrieves an entire SQL result set from the database (i.e., many rows).

  | 
| [WP_Error::__construct()](https://developer.wordpress.org/reference/classes/wp_error/__construct/)`wp-includes/class-wp-error.php` |

Initializes the error.

  |

[Show 2 more](https://developer.wordpress.org/reference/classes/wpdb/get_table_charset/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/classes/wpdb/get_table_charset/?output_format=md#)

| Used by | Description | 
| [wpdb::get_col_length()](https://developer.wordpress.org/reference/classes/wpdb/get_col_length/)`wp-includes/class-wpdb.php` |

Retrieves the maximum string length allowed in a given column.

  | 
| [wpdb::get_col_charset()](https://developer.wordpress.org/reference/classes/wpdb/get_col_charset/)`wp-includes/class-wpdb.php` |

Retrieves the character set for the given column.

  | 
| [wpdb::check_safe_collation()](https://developer.wordpress.org/reference/classes/wpdb/check_safe_collation/)`wp-includes/class-wpdb.php` |

Checks if the query is accessing a collation considered safe.

  | 
| [wpdb::strip_invalid_text_from_query()](https://developer.wordpress.org/reference/classes/wpdb/strip_invalid_text_from_query/)`wp-includes/class-wpdb.php` |

Strips any invalid characters from the query.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/classes/wpdb/get_table_charset/?output_format=md#changelog)󠁿

| Version | Description | 
| [4.2.0](https://developer.wordpress.org/reference/since/4.2.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwpdb%2Fget_table_charset%2F)
before being able to contribute a note or feedback.