如果帖子标题存在,如何禁用编辑帖子上的发布按钮

时间:2017-01-03 作者:Le Hi

我想问一个问题:我有一个源代码,它只适用于新帖子,但不适用于编辑帖子。那么,如果帖子标题存在,如何禁用编辑帖子上的发布按钮呢?

add_action(\'wp_ajax_check-posts-by-title\', function(){
    $title = isset( $_REQUEST[\'title\'] ) ? esc_attr( $_REQUEST[\'title\'] ) : null;
    // check for title
    if ( !$title ) {
        return wp_send_json(array(
            \'enable_btn\' => false,
            \'message\' => \'No title specified!\' 
        ));
    }
    $post = get_page_by_title($title, OBJECT, \'post\');
    // check for post
    if ( isset($post->ID) && \'publish\' === $post->post_status ) {
        return wp_send_json(array(
            \'enable_btn\' => false
        ));
    }
    // nothing caught
    return wp_send_json(array(
        \'enable_btn\' => true
    ));
    // kill response
    wp_die();
});

add_action(\'admin_footer\', function() { 
    global $pagenow;

    if ( !$pagenow || !in_array($pagenow, array( \'post-new.php\' )) )
        return; // only enqueue when writing/editing posts
    ?>
    <script>
        jQuery(document).ready(function($){
            $(document).on("change", "#titlediv #title", function(e){
                var i = $(this)
                  , f = i.closest(\'form\')
                  , t = i.val()
                  , b = $(\'input[name="publish"]\', f).first();

                if ( self.xhr ) {
                    self.xhr.abort();
                } else {
                    var xhr;
                }

                if ( !b.length ) return;
                b.attr(\'disabled\',\'disabled\');

                if ( $.trim( t ) ) {
                    self.xhr = $.ajax({type: \'post\', url: \'admin-ajax.php\', data: {
                        action: \'check-posts-by-title\',
                        title: t
                    }, success: function(res){
                        if ( res && res.enable_btn ) {
                            b.removeAttr(\'disabled\');
                        } else {
                            if ( res.message ) {
                                alert(res.message);
                            }
                        }
                    }, error: function(){
                        i.trigger(\'change\'); // roll over
                    }});
                }
            });
            $(\'#titlediv #title\').trigger(\'change\');
        });
    </script>
    <?php
});
谢谢<;3.

2 个回复
SO网友:Tunji

您可以通过添加post.php 在脚本排队之前,先执行条件检查。

NOTE: 这可能会有效地禁用更新帖子的功能。

add_action(\'admin_footer\', function() { 
    global $pagenow;

    if ( !$pagenow || !in_array($pagenow, array( \'post-new.php\', \'post.php\' )) )
        return; // only enqueue when writing/editing posts
    ?>
    <script>
        jQuery(document).ready(function($){
            $(document).on("change", "#titlediv #title", function(e){
                var i = $(this)
                  , f = i.closest(\'form\')
                  , t = i.val()
                  , b = $(\'input[name="publish"]\', f).first();

                if ( self.xhr ) {
                    self.xhr.abort();
                } else {
                    var xhr;
                }

                if ( !b.length ) return;
                b.attr(\'disabled\',\'disabled\');

                if ( $.trim( t ) ) {
                    self.xhr = $.ajax({type: \'post\', url: \'admin-ajax.php\', data: {
                        action: \'check-posts-by-title\',
                        title: t
                    }, success: function(res){
                        if ( res && res.enable_btn ) {
                            b.removeAttr(\'disabled\');
                        } else {
                            if ( res.message ) {
                                alert(res.message);
                            }
                        }
                    }, error: function(){
                        i.trigger(\'change\'); // roll over
                    }});
                }
            });
            $(\'#titlediv #title\').trigger(\'change\');
        });
    </script>
    <?php
});

SO网友:Gui

如果我理解得很好,您希望在标题不为空时隐藏“发布”按钮:

add_action(\'admin_head\', \'hide_publish_button\');
function hide_publish_button() {
    global $post;

    if(empty($post->post_title) {
        return;
    }
    ?>
    <script type="text/javascript">
    window.onload = function() {
        document.getElementById(\'publish\').disabled = true;
    }
    </script>
    <?php
}

相关推荐

删除具有实际标题的SANITIZE_TITLE_WITH_DASSES函数

求你了,我真的需要帮助。下面的代码是在我的网站上将搜索查询保存为标记的代码,但它将标题替换为“-”,请问有什么方法可以使标记标题名称保持搜索状态?。function addsometags() { //Don\'t do anything if we\'ve already got 20 tags $posttags = get_the_tags(); $count=0; if ($posttags) { foreach($posttags as $tag