【问题标题】:In wordpress to mark required fields in Add New Post page在 wordpress 中标记添加新帖子页面中的必填字段
【发布时间】:2015-05-04 10:42:17
【问题描述】:

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

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

谢谢!

【问题讨论】:

标签: wordpress validation


【解决方案1】:

您可以使用以下代码自行调用您的 jquery 验证,

将此代码添加到主题的functions.phpcustom.js 中的主题js 文件夹中

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);
            }
        });

    }
});

【讨论】:

  • 感谢您的帮助!插件必填字段很有用,但似乎不适用于使用 register_post_type 函数创建的自定义类型。我将尝试根据我的情况对其进行编辑...还有一个问题,即 RTF 编辑器的 id 和名称值(在我的情况下为 CKEditor,因为我安装了“CKEditor for WordPress”插件)以获取其值为 [code]$( "#post_content").val(); ?至于我的JS脚本,我不需要禁用“发布按钮,我更需要停止提交页面。如果有办法吗?
  • 您要检查帖子内容区域是否为空吗?并且你还想知道post contentID 显示编辑器的区域吗?
  • 您还必须禁用发布按钮,直到所有字段都不为空,否则您的验证将毫无用处。
  • 是的,我想知道帖子内容的名称和 ID。我在萤火虫中没有找到它
  • CKEDITOR 使用content 作为名称。所以你需要if ( CKEDITOR.instances.content.getData() == '' ){ alert( 'There is no data available' ); }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-13
相关资源
最近更新 更多