在发布帖子之前,是否有任何操作过滤器/钩子用于验证自定义字段?


11

我有一个名为的自定义文件xxxx_url。xxxx_url应该是唯一的。

因此,在发布该帖子之前,我想确保它xxxx_url是否唯一?如果不是唯一的,则应拒绝发布该帖子。

我试过了publish_post。但这是不正确的,因为它在我们发布帖子时触发。我想在发布之前运行我的代码。


1
为什么不将当前帖子ID设置为唯一值。像:帖子ID 132_url在哪里132。比起您在这里始终拥有独特的价值。除此之外:自定义字段应保存在save_post操作中。在此操作中,您可以基于此字段检查自定义字段(如果该字段不为空,并且具有唯一值)update_post_meta。我想您也可以检查自定义字段,如果它没有唯一值,请将设置post-status为draft或其他设置以禁用发布。否则,我认为您需要jQuery来执行此操作。
— LWS-Mo

Answers:


5

在的开头wp_insert_post,用于保存/更新帖子的功能有一个名为的过滤器wp_insert_post_empty_content。默认情况下,此过滤器检查标题,编辑器和摘录字段是否都为空,在这种情况下,保存过程将被暂停。

但是,由于所有要保存的字段都传递给此过滤器,因此您可以扩展此过滤器以包括任何其他测试以确定该帖子是否应视为空。就像这样:

add_filter ('wp_insert_post_empty_content','wpse312975_check_unique_url',10,2);
function wpse312975_check_unique_url ($maybe_empty, $postarr) {

  // extract custom field from $postarr, check uniqueness

  if ($unique) return false else return true;
  }

注意:该函数必须返回“ true”以停止保存过程。

如果自定义字段不是唯一的,则您可能还需要回显警告。


2
这可能会起作用,但看起来更像是骇客,而不是解决方案。我也考虑过这一点,但后来决定不发布,因为wp_insert_post_empty_content在语义上意为空内容。话虽如此,我也没有找到任何在语义上合适的钩子。
— 斯科特(Scott)

好吧,如果只是用于空白标题和内容,那么开发人员就不会有过滤器。
— cjbj

1
如果您只关注“空”一词,那我想说这个过滤器旨在检查帖子是否符合保存的最低要求。OP希望扩大最低要求。也许应该命名过滤器wp_insert_post_valid_content来表达这一点,但对于其他过滤器来说,它恰好在正确的位置。
— cjbj

@cjbj遇到类似问题时,如何找到正确的过滤器?我的意思是,您如何找到wp_insert_post_empty_content过滤器?
— 我是最愚蠢的人

1
好吧,当您使用WP一段时间后,您就知道代码的哪一部分生成了管理员的哪一部分。因此,您查找文件并检查源代码中是否有可用的过滤器。
— cjbj

1

在提交帖子进行发布之前使用AJAX检查唯一性如何?

$( '#post' ).on( 'submit', function( event ) {
  event.preventDefault(); // Prevent publishing

  //Now do some AJAX Checks
  $.post( ajaxurl, data, function(response) {
    if ( response === 'success' ) {
        $( this ).off( event ).submit();
    } else {
        alert( 'The custom field must be unique' );
    }
  });
});  

虽然该代码未经测试,但是应该可以工作。您可能需要使用它才能获得所需的结果。


0

我会加入wp_insert_post_data过滤器,并尽可能减少干扰,因为据我了解您不想阻止帖子的插入,您只是想避免发布具有重复元值的帖子。

在这种情况下,我不能多余,因为您没有共享任何代码,但是下面是可以使用的过滤器伪代码:

function wp8193131_check_if_meta_value_is_unique ( $data, $postarr ) {
    // setup an uniqueness flag.
    $meta_is_unique = true;

    // check if the meta is unique and modify the `$meta_is_unique` flag accordingly.
    // {...} <- your code

    // if the meta is NOT unique keep the post status in draft.
    if ( ! $meta_is_unique ) {
        // you can force the current post to be draft until the meta value will became unique.
        $data['post_status'] = 'draft';
        // maybe, update the meta value with a hint of the fact that it's not unique.
        // or display a dashboard notice about it.
    }

    return $data;
}
add_filter( 'wp_insert_post_data', 'wp8193131_check_if_meta_value_is_unique' );

此过滤器的另一个好处是,它与附件one分离wp_insert_attachment_data。

希望对您有所帮助,无论您做什么,听起来都很棒!


2
那可能不是一个好主意。如果$data['post_status']是publish并且用户正在更新该怎么办?不会将帖子设为该帖子draft的404问题吗?
— 斯科特(Scott)

@Scott换句话说,如果已发布的帖子在保存时检测到其元值不是唯一的,则应拒绝该发布,并应起草该帖子。同样,我不知道该元值有多重要,但是如果它是一种排序数据类型或必不可少的东西,那么该职位的状态应该是草稿(这并不意味着404问题,它只是说明该职位已经从公众视野中退休)
— Andrei

-1

支票应该去wp_insert_post。每当发布或编辑帖子时都会触发此挂钩。

在那里,您可以执行自定义查询以检查是否有任何帖子已经具有相同的xxxx_url值。

add_action('wp_insert_post', function($post_id) {
    $meta_key = 'xxxx_url';
    $meta_value = get_post_meta($post_id, $meta_key, true);
    $query = new WP_Query([
        'post_type' => get_post_type($post_id),     // This might be unnecessary, if you check `post` post type only. Or use `any`.
        'meta_query' => [
            [
                'meta_key' => $meta_key,
                'meta_value' => $meta_value,
            ]
        ]
    ]);
    if ($query->have_posts()) {
        // invalid key, post with the same value already exists
    } else {
        // valid, key was not found anywhere
    }
});

3
据我所知,无论您在if / else语句后写的内容是什么,都会被发布,因为wp_insert_post一旦保存了帖子,动作就会触发,它不会阻止发布或插入帖子。
— 我是最愚蠢的人'18

因此,如果帖子验证失败,您想阻止发布吗?您还没有在问题中写道。
— Petr Cibulka '18

我已经If it isn't unique it should reject publishing post.在问题中明确提到了这一点。
— 我是最愚蠢的人
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.