WordPress加入神马站长平台MIP数据提交功能

神马站长平台有一个MIP数据提交功能,WordPress发布文章时可以利用这个功能,去提交文章的URL到神马搜搜索引擎。具体操作方法是在WordPress主题文件functions.php中或者使用Code Snippets插件添加自定义代码:

add_action( 'publish_post', function( $post_id ) {
  if ( wp_is_post_revision( $post_id ) ) {
    return;
  }

  $post = get_post( $post_id );
  if ( $post->post_type === 'post' ) {
    $authkey = '填写你的Authkey';
    $urls = array(
      get_permalink( $post_id ),
    );
    $admin_email = get_option( 'admin_email' );
    $api = 'https://data.zhanzhang.sm.cn/push?site=wpmore.cn&[email protected]&resource_name=mip_add&token=' . $authkey;
    $ch = curl_init();
    $options = array(
      CURLOPT_URL => $api,
      CURLOPT_POST => true,
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_POSTFIELDS => implode( '\n', $urls ),
      CURLOPT_HTTPHEADER => array(
        'Content-Type: text/plain',
      ),
    );
    curl_setopt_array( $ch, $options );
    $result = curl_exec( $ch );

    // get returnCode
    $returnCode = curl_getinfo( $ch, CURLINFO_HTTP_CODE );

    // result to string
    $result = (string) $result;

    // send email
    $subject = '';
    if ( $returnCode === 200 ) {
      $subject = '神马站长MIP推送成功';
    } else {
      $subject = '神马站长MIP推送失败';
    }
    $message = '<p>' . implode( '\n', $urls ) . '</p>';
    $message .= '<p>' . $result . '</p>';
    $headers = array(
      'Content-Type: text/html; charset=UTF-8',
      'From: ' . $admin_email,
    );
    wp_mail( $admin_email, $subject, $message, $headers );
    curl_close( $ch );
  }
}, 10, 2 );

本站的WordPress主题加入了这个功能:

MIP数据提交成功
MIP数据提交成功

上面是推送成功邮件,还可以吧。

发表回复

您的电子邮箱地址不会被公开。