1

I'm not trying to set the exerpt for a custom post type, but a word limit in the admin of my custom post type.

I have just set up a custom post type:

add_action( 'init', 'test_post_type' ); 
function create_post_type() 
    register_post_type( 
       'test_post_type',
        array(
            'labels' => array(
                'name' => __( 'test' ),
                'singular_name' => __( 'test' )
            ),
            'public' => true,
            'has_archive' => true,
        )
    );
}

I would like to find out how to set a word limit for this custom post type, so when the publish button is clicked, it will display an error due to the amount of words going over the word limit.

I only know how to set up a word limit for the title on all posts:

function maxWord($title){
global $post;
$title = $post->post_title;
if (str_word_count($title) >= 15 ) // maximum of 15 words
    wp_die( __('Your post title is over the maximum word count.') );
} 
add_action('publish_post', 'maxWord');

I would like it the same as above, but a word limit on the tinymce post in my custom post type only.

Any help much appreciated.

  • @kaiser not a duplicate. the question you linked to is about limiting the post content when displayed on front end. Andy wants to limit the content entered by editor/admin in the WP admin editor window, so when they hit Publish/Save, they get an error message if content limit is reached. – IntricatePixels Dec 20 '14 at 1:20
1

Filters and Actions

The hooks you can chose to run whatever limits the post content length from, is either save_post, save_post_{post_type} or edit_post (or both) (the following actions are executed inside wp_publish_post()

/** This action is documented in wp-includes/post.php */
do_action( 'edit_post', $post->ID, $post );
do_action( "save_post_{$post->post_type}", $post->ID, $post, true );
do_action( 'save_post', $post->ID, $post, true );
do_action( 'wp_insert_post', $post->ID, $post, true );

or one of the post transition hooks executing inside wp_transition_post_status(). Namely those are:

do_action( 'transition_post_status', $new_status, $old_status, $post );
do_action( "{$old_status}_to_{$new_status}", $post );
do_action( "{$new_status}_{$post->post_type}", $post_id, $post );

To check inside a callback on an filter or action if you are on the correct filter, you can use current_filter() or current_action() (where later is just a wrapper for current_filter()), which returns a string with the name of the filter or action currently running.

Limits

To limit the amount of words, you may want to use

wp_trim_words( $content, 300 );

to limit the content to for e.g. 300 words.

There is more to it when there is already content present. To get around this, you can leverage the plugins in this answer I wrote to limit excerpt length. Just alter it if you want to run it on the content only.

The general idea (in pseudo code) always is:

Explode the string of words in an array on empty space
Count the number of array items
If they exceed $limit, loop through them or slice the array
Return everything below and equal to the threshold

Core API

The reason why you may want to use wp_trim_words() instead of a simple loop is, that it strips all HTML tags from a string (so they don't add to the count) and that there is a filter running callbacks before the result is returned, allowing for a very fine grained and targetted approach

apply_filters( 'wp_trim_words', $text, $num_words, $more, $original_text );

Putting a plugin together

Just fill in whatever you may want to do. Make sure that you inform the user properly by using something like post_updated_messages to tell the user why you stripped off words, refused to save the content or whatever UX you want deliver

<?php 
/** Plugin Name: WPSE (#172544) Limit Content by Word Count */

add_action( 'save_post_test_post_type', function( $id, \WP_Post $post, $updated ) 
{
    # debug:
    # var_dump( $post, $_POST );
    // Here you can do whatever you want to do to limit the 
}, 10, 3 );
  • 1
    Thanks a lot Kaiser, that is impressive. Thanks for unmarking this as a duplicate, I wasn't going to because I didn't think anyone would know how to do it. I'll follow your guide and try to get it to work. – Andy Dec 20 '14 at 18:18
0

Personally I would not recommend making any such changes to the default way the Wordpress editor works as it could pose issues in the future.

However, I think you may be able to accomplish this with some Javascript/jQuery.

Take a look at this example. Make sure to test suggested implementation thoroughly, I have not tested it.

  • Answers should be more than plain links. They should actually be an answer instead of a route where someone will maybe find an answer. Please help preventing link rot, edit your answer and provide the needed information that helps the OP as well as later visitors to solve their problem. – kaiser Dec 17 '14 at 22:37
  • Thanks for your advice, that link looks interesting, I'll see if I can get it to work. – Andy Dec 18 '14 at 21:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.