在wordpress中标记“添加新帖子”页面

时间:2015-05-04 10:42:17

标签: wordpress validation

在WP 4.2中打开“添加新帖子”页面而不编辑任何字段,单击"发布"按钮页面已提交并重新打开,但没有任何字段根据需要标记为红色背景颜色。 怎么做?似乎,这是此页面的原始行为。 我希望它像类别编辑器一样工作,在类似情况下,字段名称根据需要用红色背景颜色标记。

我还使用register_post_type函数向New Post页面添加了几个字段,我想根据需要用红色背景颜色标记。哪种方法最好?

谢谢!

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码

自行调用您的jquery验证

在主题functions.php文件夹中的主题custom.jsjs中添加此代码

add_action( 'admin_print_scripts-post-new.php', 'custom_admin_script', 11 );
add_action( 'admin_print_scripts-post.php', 'custom_admin_script', 11 );

    function custom_admin_script() {
        global $post_type;
        // Post type = "post" or "page" you can load script wheater it is post or page , if page then change 'post' to 'page' in below condition

        if( 'post' == $post_type )
        wp_enqueue_script( 'custom-admin-script', get_stylesheet_directory_uri() . '/js/custom.js' );
    }

custom.js

jQuery(document).ready(function ($) {

    if ($("#post").length > 0) {

        var frm = true;

        $("#publish").click(function () {

            var title = $("#title").val();
            var excerpt = $("#excerpt").val();

            // your custom field variable goes here 

            if (jQuery.trim(title) == "" || jQuery.trim(excerpt) == "") {
                frm = false;
            }

            if (frm == false) {
                $("#publish").prop('disabled', true);
            } else {
                $("#publish").prop('disabled', false);
            }
        });

    }
});