You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用WP Fusion时,如何在WP-API文章响应中添加Infusionsoft标签?

Adding Infusionsoft Tags (via WP Fusion) to WP REST API Post Responses

Absolutely! You can totally add Infusionsoft tags synced through WP Fusion to your WP REST API post responses. Here's a straightforward way to make it happen:

Step 1: Use the rest_prepare_post Filter

WordPress provides the rest_prepare_post filter specifically to modify the data returned when fetching posts via the REST API. We’ll use this hook to inject the Infusionsoft tags into each post’s response.

Add this code to your theme’s functions.php file (or a custom plugin—this is the better choice for long-term maintainability):

add_filter( 'rest_prepare_post', 'add_infusionsoft_tags_to_api', 10, 3 );

function add_infusionsoft_tags_to_api( $response, $post, $request ) {
    // Guard clause: Make sure WP Fusion is active to avoid errors
    if ( ! function_exists( 'wp_fusion' ) ) {
        return $response;
    }

    // Fetch Infusionsoft tags linked to the post using WP Fusion's official method
    $infusionsoft_tags = wp_fusion()->post_meta->get_tags( $post->ID );

    // Add tags to the API response (customize the key name if you want)
    $response->data['infusionsoft_tags'] = ! empty( $infusionsoft_tags ) ? $infusionsoft_tags : [];

    return $response;
}

Step 2: What This Code Does

Let’s break down the key parts:

  • Guard Clause: We first check if WP Fusion is active to prevent fatal errors if the plugin is ever disabled.
  • Official WP Fusion Method: Instead of digging directly into post meta, we use wp_fusion()->post_meta->get_tags()—this uses the plugin’s supported API, so it won’t break if WP Fusion changes how it stores tags in future updates.
  • Consistent Data: We add a new top-level field infusionsoft_tags to the post response. If the post has no Infusionsoft tags, we set it to an empty array to keep the response structure consistent.

Step 3: Test the API

After adding the code, send your GET request to {{base-url}}/wp-json/wp/v2/posts again. You’ll now see the infusionsoft_tags field in each post’s response, containing the list of associated Infusionsoft tags.

Bonus: Custom Post Types

If you need this for a custom post type (like product or event), duplicate the filter and replace rest_prepare_post with rest_prepare_{your_post_type_slug}. For example, for WooCommerce products:

add_filter( 'rest_prepare_product', 'add_infusionsoft_tags_to_api', 10, 3 );

内容的提问来源于stack exchange,提问作者Amit Gupta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:02:00