Skip to content

Instantly share code, notes, and snippets.

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 barryhughes/9317e5dbecbde0254e6c6bf14ef38b51 to your computer and use it in GitHub Desktop.
Save barryhughes/9317e5dbecbde0254e6c6bf14ef38b51 to your computer and use it in GitHub Desktop.
<?php
/**
* Adds some JS to enforce a limit on the length of the submitted title.
*
* Modify the limit value (`var limit = 65`) according to your needs, etc.
* Remember that this is frontend only, nothing is enforced server side.
*
* Tested with Community Events 4.5.9.
*/
add_action( 'tribe_events_community_form', function() {
echo '
<script defer>
jQuery( document ).ready( function( $ ) {
var limit = 65;
var $warning_label;
var $post_title = $( "#post_title" );
function create_warning_label() {
$warning_label = $( "<p><b>The title must be " + limit + " characters or less!</b></p>" );
$warning_label.hide();
$( ".events-community-post-title" ).append( $warning_label );
}
function on_change() {
var title = $post_title.val();
// Display warning and truncate title if necessary
if ( title.length > limit ) {
$warning_label.show();
$post_title.val( title.substr( 0, limit ) );
}
else {
$warning_label.hide();
}
}
create_warning_label();
$post_title.on( "input", on_change );
} );
</script>
';
}, 20 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment