没有不值得去解决的问题,也没有不值得去学习的技术!

Migrating CTA Configuration From Ad Inserter to WPCode: Optimization Practices

新的实现方式:基于WPCode Location的优化方案

作者:

在

Introduction

In WordPress website development, CTA (Call to Action) configuration is key to improving user conversion rates. This article details how to migrate from Ad Inserter to WPCode and optimize the setup based on From Ad Inserter to WPCode: CTA Configuration Migration and Priority Implementation, achieving a more flexible and efficient CTA configuration solution.

Original Implementation: The Old Approach Based on add_filter

Code Implementation

PHP
// ==========================================
// 使用 WordPress 原生钩子,强制在内容后插入
// ==========================================
add_filter('the_content', 'custom_desc_and_ads_inserter', 20);
function custom_desc_and_ads_inserter($content) {
    // 1. 仅在单篇文章页面执行,排除后台、Feed、首页等
    if (!is_singular('post') || is_admin() || is_feed()) {
        return $content;
    }
    // 2. 安全获取文章ID
    $post_id = get_the_ID();
    if (!$post_id) {
        global $wp_query;
        if (isset($wp_query->queried_object_id)) {
            $post_id = $wp_query->queried_object_id;
        }
    }
    // 3. 初始化变量
    $current_lang = '';
    $current_series_slugs = array();
    if (class_exists('SitePress')) {
        $current_lang = apply_filters('wpml_current_language', null);
    }
    if (empty($current_lang)) {
        $current_lang = (false !== strpos($_SERVER['REQUEST_URI'], '/en/')) ? 'en' : 'zh';
    }
    if ($post_id) {
        $current_series = get_the_terms($post_id, 'series');
        if (is_array($current_series) && !is_wp_error($current_series)) {
            $current_series_slugs = wp_list_pluck($current_series, 'slug');
        }
    }
    $output_html = '';
    // 4. 优先级判断逻辑 (保持不变)
    // ... (此处省略具体的判断逻辑)
    // 5. 将描述追加到文章内容末尾
    return $content . $output_html;
}

WPCode Configuration

  • Location: Frontend Only
  • Priority: 10
  • Insertion Method: Auto Insert

New Implementation: The Optimized Approach Based on WPCode Location

Code Implementation

PHP
// ==========================================
// 使用 WordPress 原生钩子,强制在内容后插入
// ==========================================
// add_filter('the_content', 'custom_desc_and_ads_inserter', 20);
function custom_desc_and_ads_inserter() {
    // 1. 仅在单篇文章页面执行,排除后台、Feed、首页等
    if (!is_singular('post') || is_admin() || is_feed()) {
        return;
    }
    // 2. 安全获取文章ID
    $post_id = get_the_ID();
    if (!$post_id) {
        global $wp_query;
        if (isset($wp_query->queried_object_id)) {
            $post_id = $wp_query->queried_object_id;
        }
    }
    // 3. 初始化变量
    $current_lang = '';
    $current_series_slugs = array();
    if (class_exists('SitePress')) {
        $current_lang = apply_filters('wpml_current_language', null);
    }
    if (empty($current_lang)) {
        $current_lang = (false !== strpos($_SERVER['REQUEST_URI'], '/en/')) ? 'en' : 'zh';
    }
    if ($post_id) {
        $current_series = get_the_terms($post_id, 'series');
        if (is_array($current_series) && !is_wp_error($current_series)) {
            $current_series_slugs = wp_list_pluck($current_series, 'slug');
        }
    }
    $output_html = '';
    // 4. 优先级判断逻辑 (保持不变)
    // ... (此处省略具体的判断逻辑)
    // 5. 返回描述HTML,不再拼接 $content
    return $output_html;
}
// 执行函数并输出描述
echo custom_desc_and_ads_inserter();
New implementation: Optimized approach based on WPCode Location
New implementation: Optimized approach based on WPCode Location

WPCode Configuration

  • Location: Insert After Content
  • Priority: 10
  • Insertion Method: Auto Insert

Comparative Analysis of the Optimization

Key Optimization Points

  1. Removal of the add_filter hook
  • Old implementation: Relied on the WordPress native the_content filter
  • New implementation: Directly uses WPCode’s Location feature, avoiding hook conflicts
  1. Simplified function parameters
  • Old implementation: The function accepted the $content parameter and required content concatenation
  • New implementation: The function no longer accepts parameters, resulting in a single, focused responsibility
  1. Clarified execution flow
  • Old implementation: Executed automatically via the filter
  • New implementation: Explicitly called via echo, making the execution flow clearer
  1. Improved configuration flexibility
  • Old implementation: Location was set to “Frontend Only”, which is relatively fixed
  • New implementation: Location is set to “Insert After Content”, which is more precise

Analysis of Advantages

  1. Code simplicity
  • Removed the content concatenation logic, making the code more concise and readable
  • The function has a single responsibility, making it easier to maintain and debug
  1. Configuration flexibility
  • Insertion position and priority can be adjusted directly through the WPCode interface
  • CTA display position can be changed without modifying code
  1. Avoidance of hook conflicts
  • No longer conflicts with other plugins using the the_content filter
  • Improved compatibility with other plugins
  1. More precise execution control
  • Explicit invocation ensures the function only executes when needed
  • Avoids the overhead of unnecessary function calls

Conclusion

By migrating from Ad Inserter to WPCode and optimizing the original implementation, we achieved:

  • A more concise code structure
  • A more flexible configuration approach
  • More reliable execution order control
  • Better compatibility with other plugins

This optimization approach not only resolves the previous hook conflict issue but also provides greater flexibility for future CTA configurations. For WordPress developers, this is a highly recommended practical solution.

系列导航

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系