Support » Developing with WordPress » Excluding a set of words from editor word count

  • Hello,

    I’d like to exclude some common words for editorial pourpose (stopwords) (“of”,”the”,”this”,”that”,…) from the standard editor word count, component:

    /wp-admin/js/word-count.js // word-count.min.js

    Unfortunately I coulnd’t find any way to inject my code or extend that functionality.

    I had an idea to unregister that component and register a mine similar in the name but with advanced features, but I couldn’t find an easy way to accomplish that.

    Last resort was to edit those js files but if possibile I would avoided that.

    Any idea?

    • This topic was modified 4 years, 5 months ago by DrLightman.
Viewing 5 replies - 1 through 5 (of 5 total)
  • in ‘wp-admin/js/post.js’ you cam take this part and change it in your plugin:

    /**
     * TinyMCE word count display
     */
    ( function( $, counter ) {
    	$( function() {
    		var $content = $( '#content' ),
    			$count = $( '#wp-word-count' ).find( '.word-count' ),
    			prevCount = 0,
    			contentEditor;
    
    		/**
    		 * Get the word count from TinyMCE and display it
    		 */
    		function update() {
    			var text, count;
    
    			if ( ! contentEditor || contentEditor.isHidden() ) {
    				text = $content.val();
    			} else {
    				text = contentEditor.getContent( { format: 'raw' } );
    			}
    
    			count = counter.count( text );
    
    			if ( count !== prevCount ) {
    				$count.text( count );
    			}
    
    			prevCount = count;
    		}
    
    		/**
    		 * Bind the word count update triggers.
    		 *
    		 * When a node change in the main TinyMCE editor has been triggered.
    		 * When a key has been released in the plain text content editor.
    		 */
    		$( document ).on( 'tinymce-editor-init', function( event, editor ) {
    			if ( editor.id !== 'content' ) {
    				return;
    			}
    
    			contentEditor = editor;
    
    			editor.on( 'nodechange keyup', _.debounce( update, 1000 ) );
    		} );
    
    		$content.on( 'input keyup', _.debounce( update, 1000 ) );
    
    		update();
    	} );
    } )( jQuery, new wp.utils.WordCounter() );

    The idea is that we will change one of the settings of ‘wp.utils.WordCounter()’.

    The plugin will be something like this:

    function myplugin_countStopWords(){ ?>
    	<script>
    					var settings = {
    					removeRegExp: new RegExp( [
    								'[',
    									// Basic Latin (extract)
    									'\u0021-\u0040\u005B-\u0060\u007B-\u007E',
    
    									// Latin-1 Supplement (extract)
    									'\u0080-\u00BF\u00D7\u00F7',
    									'\u2000-\u2BFF',
    										'of','the','this','that',
    									// Supplemental Punctuation
    									'\u2E00-\u2E7F',
    								']'
    							].join( '' ), 'g' )
    						};
    
    					( function( $, counter ) {
    						$( function() {
    							var $content = $( '#content' ),
    								$count = $( '#wp-word-count' ).find( '.word-count' ),
    								prevCount = 0,
    								contentEditor;
    
    							/**
    							 * Get the word count from TinyMCE and display it
    							 */
    							function update() {
    								var text, count;
    
    								if ( ! contentEditor || contentEditor.isHidden() ) {
    									text = $content.val();
    								} else {
    									text = contentEditor.getContent( { format: 'raw' } );
    								}
    
    								count = counter.count( text );
    
    								if ( count !== prevCount ) {
    									$count.text( count );
    								}
    
    								prevCount = count;
    							}
    
    							/**
    							 * Bind the word count update triggers.
    							 *
    							 * When a node change in the main TinyMCE editor has been triggered.
    							 * When a key has been released in the plain text content editor.
    							 */
    							$( document ).on( 'tinymce-editor-init', function( event, editor ) {
    								if ( editor.id !== 'content' ) {
    									return;
    								}
    
    								contentEditor = editor;
    
    								editor.on( 'nodechange keyup', _.debounce( update, 1000 ) );
    							} );
    
    							$content.on( 'input keyup', _.debounce( update, 1000 ) );
    
    							update();
    						} );
    					} )( jQuery, new wp.utils.WordCounter(settings) );
    	</script>
    <?php }
    add_action( 'after_wp_tiny_mce', 'myplugin_countStopWords');

    The final code you can use in your plugin or theme:

    function myplugin_countStopWords(){ ?>
    	<script>
    					var settings = {
    					removeRegExp: new RegExp( [
    								'[',
    									// Basic Latin (extract)
    									'\u0021-\u0040\u005B-\u0060\u007B-\u007E',
    
    									// Latin-1 Supplement (extract)
    									'\u0080-\u00BF\u00D7\u00F7',
    									'\u2000-\u2BFF',
    									//Here you can put a list of the words to uncount
    										'of','the','this','that',
    									// Supplemental Punctuation
    									'\u2E00-\u2E7F',
    								']'
    							].join( '' ), 'g' )
    						};
    
    					( function( $, counter ) {
    						$( function() {
    							var $content = $( '#content' ),
    								$count = $( '#wp-word-count' ).find( '.word-count' ),
    								prevCount = 0,
    								contentEditor;
    
    							/**
    							 * Get the word count from TinyMCE and display it
    							 */
    							function update() {
    								var text, count;
    
    								if ( ! contentEditor || contentEditor.isHidden() ) {
    									text = $content.val();
    								} else {
    									text = contentEditor.getContent( { format: 'raw' } );
    								}
    
    								count = counter.count( text );
    
    								if ( count !== prevCount ) {
    									$count.text( count );
    								}
    
    								prevCount = count;
    							}
    
    							/**
    							 * Bind the word count update triggers.
    							 *
    							 * When a node change in the main TinyMCE editor has been triggered.
    							 * When a key has been released in the plain text content editor.
    							 */
    							$( document ).on( 'tinymce-editor-init', function( event, editor ) {
    								if ( editor.id !== 'content' ) {
    									return;
    								}
    
    								contentEditor = editor;
    
    								editor.on( 'nodechange keyup', _.debounce( update, 1000 ) );
    							} );
    
    							$content.on( 'input keyup', _.debounce( update, 1000 ) );
    
    							update();
    						} );
    					} )( jQuery, new wp.utils.WordCounter(settings) );
    	</script>
    <?php }
    add_action( 'after_wp_tiny_mce', 'myplugin_countStopWords');
    Thread Starter DrLightman

    (@drlightman)

    Thank you I’ll give it a try as soon as I can.

    Thread Starter DrLightman

    (@drlightman)

    Thanks very much man it seems it works nicely. Only fix I had to do was wrap each stopword with \b regexp modifier to isolate them as words, otherwise they would get replaced even inside other longer words.

    “\bof\b”, “\bthis\b”, …

    • This reply was modified 4 years, 5 months ago by DrLightman.
    Thread Starter DrLightman

    (@drlightman)

    Okay so after some test, the regexp needs some reworking done, because it works as character range replacement [x-ya-bc-d], not full string.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Excluding a set of words from editor word count’ is closed to new replies.