Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Last active June 4, 2022 15:09
Show Gist options
  • Save wpmudev-sls/1ab10b70ba0c7970fb82f79b412e9d15 to your computer and use it in GitHub Desktop.
Save wpmudev-sls/1ab10b70ba0c7970fb82f79b412e9d15 to your computer and use it in GitHub Desktop.
[Forminator] Limit Post Title
<?php
/**
* Plugin Name: [Forminator] Limit Post Title
* Plugin URI: https://wpmudev.com/
* Description: Limit the post title to 60 characters and allow just numbers and latters.
* Author: Glauber Silva @ WPMUDEV
* Author URI: https://wpmudev.com/
* Task: SLS-3331
* License: GPLv2 or later
*
* @package Forminator_Limit_Post_Title
*/
defined( 'ABSPATH' ) || exit;
function wpmudev_forminator_intercept_data_post_info( $post, $field, $data ){
if ( isset( $post['post_title'] ) ) {
$title = sanitize_text_field( $post['post_title'] );
$title = preg_replace( "/[^a-zA-Z0-9 ]+/", "", $title ); // Remove everything except a-z, A-Z and 0-9 and space.
$title = trim( preg_replace('/\s\s+/', ' ', str_replace("\n", " ", $title) ) ); // Remove extra spaces between the words.
$title = substr( $title, 0, 60 ); // Limit the size of the title to 60 characters.
$post['post_name'] = $title;
$post['post_title'] = $title;
}
return $post;
}
add_filter( 'forminator_post_data_post_info', 'wpmudev_forminator_intercept_data_post_info', 999, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment