0

Is there a filter that will throw an error if the posts creator enters a post title that is too long?

I am looking to throw an error like: "The title you've entered is more than 30 characters. Please change the title and try again." Preferably this might be a banner alert like standard errors or at least something noticeable. In addition the post will not be saved until the title is of the correct length.

I know there is the wp_insert_post_data filter where you can modify the title before it is saved, but I don't want to do something like truncating as it might not make sense to the end user. This is something I would like the post writer to change.

0

The key thing I think you're missing is understanding that WordPress doesn't use a linear path for saving posts. Because posts are autosaved you'll want to handle this in a way that defines it according to it's publish status rather than it's save state. Basically, instead of trying to truncate or adjust the title, do your check in the wp_insert_post_data or via a post status transition and prevent the post from being set to a publish or future status until all of your conditions are met. The action for an admin notice is admin_notices.

| improve this answer | |
  • Whats the best way to implement the admin_notices here since it runs on every page load. I hate coupling business logic in the notices function thats actually based on another function; just feels wrong. – Shawn May 23 '14 at 21:15
0

I solved this using the transition_post_status.

function check_post_title_length($new_status, $old_status, $post){

    if($new_status == 'auto-draft' || $new_status == 'draft'){
        return;
    }

    if(str_word_count($post->post_title, 0) > 12){
        //update-nag is the term for a yellow warning box in admin_notices
        //notice here I use the WP_Error data field to choose the type of error box to show
        $admin_notices = new WP_Error('check_post_title_length','If possible, consider making the post title shorter.', 'update-nag');
        //we could also if we needed, add more notices to the list such as
        //$admin_notices->add('some_other_check', 'Sorry you need to add an author');
        add_user_meta(get_current_user_id(), 'admin_notices', $admin_notices, true);
    }
}
add_action('transition_post_status', 'check_post_title_length', 10, 3);

function display_admin_notices(){
    $user_id = get_current_user_id();
    $admin_notices = get_user_meta($user_id, 'admin_notices', true);

    if(!empty($admin_notices)){
        //make sure its a WP_Error object
        if(is_wp_error($admin_notices)){
            //delete error from user meta so error is gone on page reload
            delete_user_meta($user_id, 'admin_notices');
            $notices = $spm_admin_notices->get_error_messages();

            if(!empty($notices)){
                $notice_type = $admin_notices->get_error_data();
                if(!empty($notice_type)){
                ?>
                    <div class="<?php echo $notice_type; ?>">
                <?php
                } else {
                ?>
                    <div class="error">
                <?php
                }
                //here we loop through all notices we want to display
                //this is useful if the user needs to adjust more than one item
                foreach($notices AS $notice){
                ?>
                    <p><?php echo $notice; ?></p>
                <?php
                }
                ?>
                </div>
                <?php
            }
        }
    }
}
add_action('admin_notices', 'display_admin_notices');
| improve this answer | |

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.