【问题标题】:How to make a tag description required如何使标签描述成为必需
【发布时间】:2016-09-18 01:32:37
【问题描述】:

我对 WordPress 还是很陌生,但基本上我想要实现的是让标签的描述成为我的 WordPress 4.5.2 自定义主题的必填字段

我尝试了三种方法,但都失败了,所以如果有 WordPress 专家可以指导我,那就太好了。

方法#1

functions.php

我尝试在调用 edit_tag_form_fieldsadd_tag_form 钩子时“编辑”钩子,然后通过 Javascript 进行修改

function require_category_description(){
    require_once('includes/require_category_description.php');
}
add_action('edit_tag_form_fields', 'require_category_description');
add_action('add_tag_form', 'require_category_description');

require_category_description.php

<script>
    jQuery(document).ready(function(){
        var description = jQuery('#tag-description');
        if(!description) description = jQuery('#description');

        if(description){
            description.parents('form').submit(function(){
                if(description.val().trim().length < 1){
                    console.log('Please enter a description...');
                    return false;
                }
            });
        }
    });
</script>

它不起作用,即使描述字段为空,表单仍在提交,最重要的是,事件侦听器中的 console.log 从未发生过。我试图记录描述变量以确保它在 if 情况下。因此,我假设表单从未提交,整个“提交”是通过 Ajax 完成的,在按钮单击时完成。

方法#2

functions.php 与方法 #1 保持一致,但我对 Javascript 进行了一些更改,以针对按钮单击事件而不是表单提交事件。

require_category_description.php

<script>
    jQuery(document).ready(function(){
        var description = jQuery('#tag-description');
        if(!description) description = jQuery('#description');

        if(description){
            var button = description.parents('form').find('#submit');

            button.on('click', function(e){
                if(description.val().trim().length < 1)
                    console.log('Please enter a description...');

                e.preventDefault();
                return false;
            });
        }
    });
</script>

然而,表单仍在提交,但这次,我看到了控制台日志消息。

请输入描述...

我的理论是,WordPress 在我的事件之前将一个事件绑定到按钮的点击,因此它在转到我的自定义点击事件之前使用 Ajax 处理内置事件。

方法#3

require_category_description.php

在添加我自己的点击事件之前,我已尝试解除按钮与点击事件的绑定。

<script>
    jQuery(document).ready(function(){
        var description = jQuery('#tag-description');
        if(!description) description = jQuery('#description');

        if(description){
            var button = description.parents('form').find('#submit');
            button.unbind('click');

            button.on('click', function(e){
                if(description.val().trim().length < 1)
                    console.log('Please enter a description...');

                e.preventDefault();
                return false;
            });
        }
    });
</script>

结果与方法#2 相同。表单仍在提交,但我看到控制台日志消息。

【问题讨论】:

  • 它可能使用了提交事件而不是onclick!尝试在表单上取消绑定以查看您是否可以删除流程操作

标签: javascript php jquery wordpress forms


【解决方案1】:

编辑标签

编辑标签时,WordPress 调用wp_update_term。但是没有过滤器或AJAX调用,所以我们必须使用get_term(),它被wp_update_term()调用:

add_filter('get_post_tag', function($term, $tax)
{
    if ( isset($_POST['description']) && empty($_POST['description']) ) {
        return new \WP_Error('empty_term_name', __('Tag description cannot be empty!', 'text-domain'));
    } else {
        return $term;
    }
}, -1, 2);

我们还需要更新term_updated_message 以明确错误:

add_filter('term_updated_messages', function($messages)
{
    $messages['post_tag'][5] = sprintf('<span style="color:#dc3232">%s</span>', __('Tag description cannot be empty!', 'text-domain'));
    return $messages;
});

因为 WordPress hardcoded the notice message div,我使用内联 css 使错误看起来像一个警告。根据您的喜好更改它。


添加新标签

AJAX 请求调用wp_insert_term,因此我们可以使用pre_insert_term 过滤器。在您的 functions.php 中试试这个

add_filter('pre_insert_term', function($term, $tax)
{
    if ( ('post_tag' === $tax) && isset($_POST['description']) && empty($_POST['description']) ) {
        return new \WP_Error('empty_term_name', __('Tag description cannot be empty!', 'text-domain'));
    } else {
        return $term;
    }
}, -1, 2);

这里我使用了内置的empty_term_name 错误来显示通知消息,但你应该注册自己的。

另外,请查看wp_ajax_add_tag 以充分了解我们在做什么。

演示:

【讨论】:

  • 抱歉回复晚了!这适用于 ajax,但你知道它是用于编辑标签的哪个钩子吗?就像你在标签本身内一样。
  • @ChinLeung 我不知道为什么你还需要在editing tag screen 上这样做,因为不可能添加没有描述的新标签。但我已经如你所愿更新了答案。
  • 我需要编辑标签屏幕,因为即使用户无法添加空标签...他们可以编辑标签并简单地删除会导致标签带有空描述的描述。 :)
【解决方案2】:

它是 Ajax,所以你不能依赖 submit 事件,这里有一个解决方案,你可以怎么做。

您要做的就是将form-required 类包含到特定元素的父标记中,但它是有作用的。他们的validateForm 只检查input 标签而不是textarea 所以我实现了一个想法,它有效。

试试这个

function put_admin_script() { ?>
<script>

    jQuery(document).ready(function(){
        var description = jQuery('#tag-description');
        if( !description ) { 
            description = jQuery('#description'); 
        }   
        if( description ) {
            description.after( $('<p style="visibility:hidden;" class="form-field form-required term-description-wrap"><input type="text" id="hidden-tag-desc" aria-required="true" value="" /></p>') );    
        }   
        description.keyup(function(){
            $("#hidden-tag-desc").val( $(this).val() ); 
        });

        jQuery("#addtag #submit").click(function(){
            console.log("Not empty"+description.val().trim().length);
            if( description.val().trim().length < 1 ) {
                description.css( "border", "solid 1px #dc3232" );
            } else {
                description.css( "border", "solid 1px #dddddd" );
            }                   
        }); 
    }); 

</script>
<?php 
}
add_action('admin_footer','put_admin_script');

【讨论】:

  • 刚试了一下,表单正在提交,没有控制台消息。正如我在方法 #1 中提到的,我认为表单本身永远不会提交。
  • 哦,刚才我也查过了。这是阿贾克斯
  • wp-admin/js/tags.js LN : 31,但我们似乎无能为力。
  • 我想避免在wp-admin 中进行更改,因为它会在下一个版本的 WordPress 更新中被覆盖...
  • 找到了一个解决方案,它有效,但不是一个完整的解决方案,你无论如何都可以改进它。
猜你喜欢
  • 1970-01-01
  • 2011-11-06
  • 1970-01-01
  • 1970-01-01
  • 2015-08-08
  • 1970-01-01
  • 2016-06-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多