Last active
April 25, 2018 06:06
-
-
Save barryhughes/9317e5dbecbde0254e6c6bf14ef38b51 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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