如何检查是否在wordpress中启用了XML-RPC

时间:2013-03-19 13:49:31

标签: php wordpress xml-rpc

是否可以检查(通过php)在wordpress中启用XML-RPC。 有点像,写一个函数会测试这个......

if(is_xmlrpc_enabled()) {
   //action
}
else {
   //another action
}

2 个答案:

答案 0 :(得分:8)

WP版本默认启用XML-RPC> 3.5(使用'xmlrpc_enabled'钩子允许禁用它) 对于旧版本,数据库中有一个字段(选项表),表示它是否已启用。(对于wp> 3.5,此选项已删除)

function is_xmlrpc_enabled() {
    $returnBool = false; 
    $enabled = get_option('enable_xmlrpc'); //for ver<3.5

    if($enabled) {
        $returnBool = true;
    }
    else {
        global $wp_version;
        if (version_compare($wp_version, '3.5', '>=')) {
            $returnBool = true; //its on by default for versions above 3.5
        }
        else {
            $returnBool = false;
        }  
    }
    return $returnBool;
}

答案 1 :(得分:3)

WordPress在其XML-RPC服务器中有两种测试方法:

demo.sayHello – Returns a standard “Hello!” message.
demo.addTwoNumbers – Accepts an array containing two numbers and returns the sum.

function sayHello()  
{  
    $params = array();  
    return $this->send_request('demo.sayHello',$params);  
} 

$objXMLRPClientWordPress = new XMLRPClientWordPress("http://localhost/wordpress31/xmlrpc.php" , "username" , "passowrd"); 

function send_request($requestname, $params)  
{  
            $request = xmlrpc_encode_request($requestname, $params);  
            $ch = curl_init();  
            curl_setopt($ch, CURLOPT_POSTFIELDS, $request);  
            curl_setopt($ch, CURLOPT_URL, $this->XMLRPCURL);  
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
            curl_setopt($ch, CURLOPT_TIMEOUT, 1);  
            $results = curl_exec($ch);  
            curl_close($ch);  
            return $results;  
}  

如果您得到相同的结果,则意味着您可以将请求正确发送到您的WordPress XML-RPC服务器并正确接收请求。