Skip to content

Instantly share code, notes, and snippets.

@19h47
Last active November 16, 2022 15:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 19h47/cde74dede05ad9ea9b9e4904d8ef19c2 to your computer and use it in GitHub Desktop.
Save 19h47/cde74dede05ad9ea9b9e4904d8ef19c2 to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin Name: WP Sanitize Title With Underscores
* Plugin URI: https://gist.github.com/19h47/c54fd0e7a5c6fbc99a6087e7606054c0
* Description: Simple wrapper around sanitize_title_with_dashes to replacing dashes with underscores.
* Version: 0.0.1
* Author: Jérémy Levron
* Author URI: https://19h47.fr
*
* @package 19h47/wp-sanitize-title-with-underscore
*/
if ( ! function_exists( 'sanitize_title_with_underscores' ) && function_exists( 'sanitize_title_with_dashes' ) ) {
/**
* Sanitize a title, replacing dashes by underscores.
*
* @param string $title The title to be sanitized.
* @param string $raw_title (Optional) Not used. Default value: ''.
* @param string $context (Optional) The operation for which the string is sanitized. When set to 'save', additional entities are converted to hyphens or stripped entirely. Default value: 'display'.
*
* @see https://developer.wordpress.org/reference/functions/sanitize_title_with_dashes/
*
* @return string The sanitized title.
*/
function sanitize_title_with_underscores( string $title, string $raw_title = '', string $context = 'display' ) : string {
return str_replace( '-', '_', sanitize_title_with_dashes( $title, $raw_title, $context ) );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment