哪种方法优先级更高?

时间:2014-01-19 18:02:40

标签: wordpress wordpress-plugin

有两种限制WordPress修订的方法; WP_POST_REVISIONS和wp_revisions_to_keep()。哪个会有更高的优先级?在阅读完codex之后我无法理解。

1 个答案:

答案 0 :(得分:1)

WP_POST_REVISIONS是常量,不是函数。如果您设置了WP_POST_REVISIONS,请设置define( 'WP_POST_REVISIONS', 3 );,那么当您调用函数wp_revisions_to_keep时,您将获得3(前提是没有过滤器正在修改该值)。如果未设置WP_POST_REVISIONS,那么它将存储每个修订版本,当您致电-1时,您将获得wp_revisions_to_keep。这是wp_revisions_to_keep的源代码。

/**
 * Determine how many revisions to retain for a given post.
 * By default, an infinite number of revisions are stored if a post type supports revisions.
 *
 * @since 3.6.0
 *
 * @uses post_type_supports()
 * @uses apply_filters() Calls 'wp_revisions_to_keep' hook on the number of revisions.
 *
 * @param object $post The post object.
 * @return int The number of revisions to keep.
 */
function wp_revisions_to_keep( $post ) {
    $num = WP_POST_REVISIONS;

    if ( true === $num )
        $num = -1;
    else
        $num = intval( $num );

    if ( ! post_type_supports( $post->post_type, 'revisions' ) )
        $num = 0;

    return (int) apply_filters( 'wp_revisions_to_keep', $num, $post );
}

由此可见,函数wp_revisions_to_keep()在其中使用了WP_POST_REVISIONS。但是,为了100%确定您建议的修订数量,您应该将函数挂钩到wp_revisions_to_keep。这样的事情: -

add_filter('wp_revisions_to_keep', 'custom_revisions_number', 10, 2);
function custom_revisions_number($num, $post) {
    $num = 5; // <---- change this accordingly.
    return $num; 
}

最高优先级是最后执行的挂钩。

相关问题