2016-11-25 55 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更改(我猜。)。 –