禁用wooccommerce api身份验证


Disable woocommerce api authentication?

wooccommerce api对HTTP请求采用oauth1.0,对HTTPS请求采用基本的HTTP身份验证。我的查询很简单。如何简单地删除此身份验证?我做了一些挖掘,发现在wooccommerce插件中有一个类的构造函数为

public function __construct() {
        // To disable authentication, hook into this filter at a later priority and return a valid WP_User
        add_filter( 'woocommerce_api_check_authentication', array( $this, 'authenticate' ), 0 );
    }

我的工作就是简单地删除身份验证部分。这里说的是把这个过滤器挂在以后的优先级上。如何执行此操作以及如何返回有效的WP_User?

创建自己的插件并放置以下代码:

function wc_authenticate_alter(){
    //return wp_get_current_user();
    if( 'GET' ==  WC()->api->server->method ){
        return new WP_User( 1 );
    } else {
        throw new Exception( __( 'You dont have permission', 'woocommerce' ), 401 );
    }
}
add_filter( 'woocommerce_api_check_authentication', 'wc_authenticate_alter', 1 );

这将绕过wooccommerce api身份验证。风险自负。

(您可以将其添加到主题的functions.php中,而不是自己的插件中。但不经过测试。)