WordPress 通过API自动提交新内容到熊掌号

WordPress 通过API自动提交新内容到熊掌号

当我们每次发布新的内容的时候,后台自动就会提交到熊掌号,免得我们每次都要自己手动提交了 在主题的 function.php 中加入: 直接上代码:

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

/
*
*

*
  
WordPress
API 方式自动推送到百度熊掌号
*

*
/

if
(
!
function_exists
(
‘Baidu_XZH_Submit’
)
)
{

    
function
Baidu_XZH_Submit
(
$
post_ID
)
{

        
/
/已成功推送的文章不再推送

        
if
(
get_post_meta
(
$
post_ID
,
‘BaiduXZHsubmit’
,
true
)
==
1
)
return
;

        
$
url
=
get_permalink
(
$
post_ID
)
;

        
$
api
=
‘http://data.zz.baidu.com/urls?appid=你的APPID&token=你的TOKEN&type=realtime’
;

        
$
request
=
new
WP_Http
;

        
$
result
=
$
request
->
request
(
$
api
,
array
(
‘method’
=
>
‘POST’
,
‘body’
=
>
$
url
,
‘headers’
=
>
‘Content-Type: text/plain’
)
)
;

        
$
result
=
json_decode
(
$
result
[
‘body’
]
,
true
)
;

        
/
/如果推送成功则在文章新增自定义栏目
BaiduXZHsubmit,值为
1

        
if
(
array_key_exists
(
‘success’
,
$
result
)
)
{

            
add_post_meta
(
$
post_ID
,
‘BaiduXZHsubmit’
,
1
,
true
)
;

        
}

    
}

    
add_action
(
‘publish_post’
,
‘Baidu_XZH_Submit’
,
0
)
;

}

WordPress 通过API自动提交新内容到熊掌号

相关文章:

你感兴趣的文章:

标签云: