【问题标题】:Add SEO using WP Rest API Request C#使用 WP Rest API 请求 C# 添加 SEO
【发布时间】:2017-04-09 20:07:34
【问题描述】:

我使用WP Rest API v2 在 Wordpress 上发帖,使用All In One SEO Pack 进行 SEO,但我不知道应该提供哪些参数来添加 元标题元描述元标记。我有以下代码:

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);
}

【问题讨论】:

  • 我看到您的代码与Post on WordPress using WP REST API C# 问题类似,但有人给出了任何解决方案。我不是 C# 人,但如果您需要任何关于 php 的帮助,我可以为您提供帮助。和 All In One SEO Pack 使用以下 post_meta 来保存元标记:_aioseop_title 用于元标题,_aioseop_description 用于元描述,_aioseop_keywords 用于元关键字。希望这对你有帮助。
  • @RaunakGupta 这很相似,因为这是我的问题(来自另一个帐户)...谢谢 :)

标签: c# wordpress seo


【解决方案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。您可以升级到具有内置 API 的“all-in-one-seo-pack”的Pro Version

或者您可以通过添加额外的端点自己扩展WordPress REST api v2。然后自己编写PHP代码,通过REST API管理你的SEO插件。

这并不难 -> 正如 Raunak Gupta 已经告诉你的那样:这些是“all-in-one-seo-pack”中使用的字段。使用其他端点扩展 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');
        });" );
    }
}

【讨论】:

  • 如我所见:他们要求 57 美元的年费才能使用专业版。而且您只能在 1 个网站上使用它。如果你能详细告诉我你想要什么确切的功能,我可能会为你构建它。也许更多的人需要这个功能,我可以向他们收取 $$$。
  • 由于所有的 SEO 内容都存储在 post_meta 字段中,它可以通过 WP REST API 进行修改,不需要 Pro 版本的插件。如果需要更改插件设置,它们将保存在 wp_options 表中,以便也可以通过 API 更改(我猜。)。
猜你喜欢
  • 2017-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多