Wordpress validation for title

2019-07-07 06:18发布

Need to add blank and already exist validation for 'supports' => array( 'title') on my custom post type. But i dont want to use any plugin for this. Thanks in advance.

add_action( 'admin_notices', 'custom_error_notice' );
function custom_error_notice(){
global $current_screen, $post;
if ( $current_screen->parent_base == 'edit' ){
    if((!$post->post_name) && $_GET['post']) {
        wp_redirect(admin_url('post-new.php?empty=1'));
    }
    if($_GET['empty']) echo '<div class="error"><p>Warning - Please fill up all fields correctly!</p></div>';
}
}

But this not working properly.

2条回答
▲ chillily
2楼-- · 2019-07-07 06:44

i got the solution.

    /** ADD Validation for title */
    function force_post_title_init()
{
    wp_enqueue_script('jquery');
}
function force_post_title()
{
    echo "<script type='text/javascript'>\n";
    echo "
  jQuery('#publish').click(function(){
        var testervar = jQuery('[id^=\"titlediv\"]')
        .find('#title');
        if (testervar.val().length < 1)
        {
            jQuery('[id^=\"titlediv\"]').css('border', '1px solid red');
            alert('Post title is required');
            return false;
        }
    });
  ";
    echo "</script>\n";
}
add_action('admin_init', 'force_post_title_init');
add_action('edit_form_advanced', 'force_post_title');
// Add this row below to get the same functionality for page creations.
add_action('edit_page_form', 'force_post_title');

May be this also help for all of you.

查看更多
放荡不羁爱自由
3楼-- · 2019-07-07 06:50

This may help you:-

add_action('save_post', 'album_save_post', 10, 2);

function album_save_post( $album_id, $album ) {

    if( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE || $album->post_type != 'music_album') return;

    // echo '<pre>';
    // print_r($album);
    // echo '</pre>';
    // die();

    $errors = array();

    // Validation filters
    $title = $album->post_title;
    if ( ! $title ) {
        $errors['title'] = "The title is required";
    }

    // if we have errors lets setup some messages
    if (! empty($errors)) {

        // we must remove this action or it will loop for ever
        remove_action('save_post', 'album_save_post');

        // save the errors as option
        update_option('album_errors', $errors);

        // Change post from published to draft
        $album->post_status = 'draft';

        // update the post
        wp_update_post( $album );

        // we must add back this action
        add_action('save_post', 'album_save_post');

        // admin_notice is create by a $_GET['message'] with a number that wordpress uses to
        // display the admin message so we will add a filter for replacing default admin message with a redirect
        add_filter( 'redirect_post_location', 'album_post_redirect_filter' );
    }
}

function album_post_redirect_filter( $location ) {
    // remove $_GET['message']
    $location = remove_query_arg( 'message', $location );

    // add our new query sting
    $location = add_query_arg( 'album', 'error', $location );

    // return the location query string
    return $location;
}

// Add new admin message
add_action( 'admin_notices', 'album_post_error_admin_message' );

function album_post_error_admin_message() {
    if ( isset( $_GET['album'] ) && $_GET['album'] == 'error' ) {
        // lets get the errors from the option album_errors
        $errors = get_option('album_errors');

        // now delete the option album errors
        delete_option('album_errors');

        $display = '<div id="notice" class="error"><ul>';

        // Because we are storing as an array we should loop through them
        foreach ( $errors as $error ) {
            $display .= '<li>' . $error . '</li>';
        }

        $display .= '</ul></div>';

        // finally echo out our display
        echo $display;

        // add some jQuery
        ?>
        <script>
        jQuery(function($) {
            $("#title").css({"border": "1px solid red"})
        });
        </script>
        <?php
    }
}
查看更多
登录 后发表回答