2016-11-25 114 views
1

我使用WP Rest API v2張貼在Wordpress和All In One SEO PackSEO添加搜索引擎優化,但我不知道參數我應該給什麼加元標題meta描述元標記。我有以下代碼:使用WP REST API請求C#

var request = (HttpWebRequest)WebRequest.Create(Website + "wp-json/wp/v2/posts/"); 
request.Headers["Authorization"] = "Bearer " + token; 
request.Method = "POST"; 
request.UseDefaultCredentials = true; 

var postData = "title=" + Title + 
        "&content=" + Content + // HTML Code 
        "&status=publish" + 
        "&date=" + TimeZone.CurrentTimeZone.ToUniversalTime(date).ToString("yyyy-MM-ddTHH:mm:ss"); 

var data = Encoding.ASCII.GetBytes(postData); 
request.ContentType = "application/x-www-form-urlencoded"; 
request.ContentLength = data.Length; 

using (var stream = request.GetRequestStream()) 
    stream.Write(data, 0, data.Length); 

try 
{ 
    var response = (HttpWebResponse)request.GetResponse(); 
    var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd(); 
    var json = JObject.Parse("{" + JObject.Parse(responseString)["guid"].First + "}"); 
    LOG.Text = "Post posted at " + json["rendered"].ToString(); 
    list.Add(json["rendered"].ToString()); 
} 
catch (Exception exception) 
{ 
    MessageBox.Show(exception.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); 
} 
+0

我看到了什麼,你的代碼是呈三角[發佈WordPress的使用WP REST API C#](http://stackoverflow.com/questions/38458678/post-on-wordpress-using-wp-rest- api-c-sharp)的問題,但有人提出了任何解決方案。我不是C#的人,但如果你需要任何幫助,我可以幫助你。和_All In One SEO Pack_使用以下post_meta來保存元標籤的元標籤:'_aioseop_title',元描述的'_aioseop_description'和Meta關鍵字的'_aioseop_keywords'。希望這可以幫助你。 –

+0

@RaunakGupta這是類似的,因爲這是我的問題(來自另一個帳戶)...謝謝:) –

回答

1

WordPress基於PHP構建。所以WP REST API插件也是用PHP編寫的。 正如你所說的,你使用WordPress REST api v2。 讓我從共享代碼的位置開始:WordPress REST api v2

正如你可以看到這個WP REST API不包括任何SEO的東西。

現在你還安裝了'all-in-one-seo-pack'插件。 free version不包含內置API。你可以升級到Pro Version這個'all-in-one-seo-pack',它有一個內置的API。

或者你可以自己擴展WordPress REST api v2通過添加額外的端點。然後自己編寫PHP代碼,通過REST API管理你的SEO插件。

它並不那麼困難 - >正如Raunak Gupta已經告訴你的那樣:這些都是'一體式seo包裝'中使用的領域。用其他端點擴展REST API來配置它們。然後從C#代碼中調用您的端點。

<wpml-config> 
    <custom-fields> 
     <custom-field action="translate">_aioseop_title</custom-field> 
     <custom-field action="translate">_aioseop_description</custom-field> 
     <custom-field action="translate">_aioseop_keywords</custom-field> 
    </custom-fields> 
    <admin-texts> 
     <key name="aioseop_options"> 
      <key name="aiosp_home_title" /> 
      <key name="aiosp_home_description" /> 
      <key name="aiosp_home_keywords" /> 
      <key name="aiosp_post_title_format" /> 
      <key name="aiosp_page_title_format" /> 
      <key name="aiosp_category_title_format" /> 
      <key name="aiosp_archive_title_format" /> 
      <key name="aiosp_tag_title_format" /> 
      <key name="aiosp_search_title_format" /> 
      <key name="aiosp_description_format" /> 
      <key name="aiosp_404_title_format" /> 
      <key name="aiosp_paged_format" /> 
     </key> 
    </admin-texts>  
</wpml-config> 

看看免費版本的代碼。使用此代碼可以在編寫自己的代碼時獲得靈感。 WordPress希望你擴展功能。爲社區做貢獻!

if (!function_exists('aioseop_ajax_save_meta')) { 
    function aioseop_ajax_save_meta() { 
     if (!empty($_POST['_inline_edit']) && ($_POST['_inline_edit'] != 'undefined')) 
      check_ajax_referer('inlineeditnonce', '_inline_edit'); 
     $post_id = intval($_POST['post_id']); 
     $new_meta = strip_tags($_POST['new_meta']); 
     $target = $_POST['target_meta']; 
     check_ajax_referer('aioseop_meta_' . $target . '_' . $post_id, '_nonce'); 
     $result = ''; 
     if (in_array($target, Array('title', 'description', 'keywords')) && current_user_can('edit_post', $post_id)) { 
      update_post_meta($post_id, '_aioseop_' . $target, esc_attr($new_meta)); 
      $result = get_post_meta($post_id, '_aioseop_' . $target, true); 
     } else { 
      die(); 
     } 
     if($result != ''): 
      $label = "<label id='aioseop_label_{$target}_{$post_id}'><span style='width: 20px;display: inline-block;'></span>" . $result . '</label>'; 
     else: 
      $label = "<label id='aioseop_label_{$target}_{$post_id}'></label><span style='width: 20px;display: inline-block;'></span><strong><i>" . __('No', 'all-in-one-seo-pack') . ' ' . $target . '</i></strong>'; 
     endif; 
     $nonce = wp_create_nonce("aioseop_meta_{$target}_{$post_id}"); 
     $output = '<a id="' . $target . 'editlink' . $post_id . '" class="aioseop_edit_link" href="javascript:void(0);"' 
      . 'onclick=\'aioseop_ajax_edit_meta_form(' . $post_id . ', "' . $target . '", "' . $nonce . '");return false;\' title="' . __('Edit') . '">' 
      . '<img class="aioseop_edit_button" id="aioseop_edit_id" src="' . AIOSEOP_PLUGIN_IMAGES_URL . '/cog_edit.png" /></a> ' . $label; 
     die("jQuery('div#aioseop_" . $target . "_" . $post_id . "').fadeOut('fast', function() { var my_label = " . json_encode($output) . "; 
       jQuery('div#aioseop_" . $target . "_" . $post_id . "').html(my_label).fadeIn('fast'); 
     });"); 
    } 
} 
+0

正如我所看到的:他們要求57美元的年費使用專業版。你只能在1個網站上使用它。如果你可以詳細地告訴我你想要什麼確切的功能,我可能會爲你構建它。也許更多的人需要這個功能,我可以向他們收取$$$。 –

+0

由於所有的SEO內容都存儲在'post_meta'字段中,所以它可以通過WP REST API進行修改,Pro版本的插件是不需要的。如果需要修改插件設置,它們將保存在wp_options表中,以便也可以通過API更改(我猜。)。 –