我需要一种使用action/filter
. 在query
, posts_clauses
或wp_insert_post/save_post/update_post
挂钩。
到目前为止我只是想return \'\';
, 这给了我大量的错误,因为在管理UI中缺少post对象部分的值。
这应该“无声地”发生,因此在php_error/WP_DEBUG
这些设置为TRUE/On
.
顺便说一句:我不是在问如何禁用自动保存功能。
我需要一种使用action/filter
. 在query
, posts_clauses
或wp_insert_post/save_post/update_post
挂钩。
到目前为止我只是想return \'\';
, 这给了我大量的错误,因为在管理UI中缺少post对象部分的值。
这应该“无声地”发生,因此在php_error/WP_DEBUG
这些设置为TRUE/On
.
顺便说一句:我不是在问如何禁用自动保存功能。
function disable_save( $maybe_empty, $postarr ) {
$maybe_empty = true;
return $maybe_empty;
}
add_filter( \'wp_insert_post_empty_content\', \'disable_save\', 999999, 2 );
因为wp_insert_post_empty_content
设置为true时,WordPress认为没有标题和内容,并停止更新帖子。EDIT: 一个更短的变体是:
add_filter( \'wp_insert_post_empty_content\', \'__return_true\', PHP_INT_MAX -1, 2 );
您收到停止插入通知的原因wp_insert_post_empty_content
如您在https://wordpress.stackexchange.com/a/51980/31794 答案是:为了post-new.php
自动起草流程需要获得$post->ID
通过get_default_post_to_edit()
和wp_insert_post()
, 并使用$post返回中的ID。i、 e.“添加新帖子”页面实际上每次都会创建并获取新的“帖子记录”。
遗憾的是,如果停止保存过程而不是预期的帖子ID,wp\\u insert\\u post()将返回0。换句话说,您无法使用“wp\\u insert\\u post\\u empty\\u content”筛选器停止“自动草稿”。如果你使用这个过滤器,很遗憾,你必须让“自动草稿”通过,以避免PHP的注意。这是一个非常糟糕的bug。
我发现,唯一可以停止无意义地创建新的自动草稿记录并绕过此错误的方法是,使用db扩展wpdb类。php插件:
class wpdb_ext extends wpdb
{
function insert($table, $data, $format = null) {
if (isset($data[\'post_status\']) && $data[\'post_status\'] === "auto-draft" && ($pa = (int)$data[\'post_author\'])
&& ($id = $this->get_var("SELECT ID FROM $table WHERE post_status = \'auto-draft\' AND post_author = $pa LIMIT 1"))){
//fake insert id and return id of existing auto-draft as base for New post page.
return $this->insert_id = (int)$id;
}
return parent::insert($table, $data, $format = null);//else do actual insert
}
}
$wpdb = new wpdb_ext(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);//overload wpdb
这样,每个作者只保留一个自动草稿,避免了毫无意义的新自动草稿记录白白浪费/跳过id增量。我想在wp admin中创建自己的函数,就像在wp content->functions中一样。php,但我不知道wp admin文件夹中的函数文件在哪里。有人知道wp admin文件夹中的函数文件在哪里吗?