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();
WPCode Configuration
- Location: Insert After Content
- Priority: 10
- Insertion Method: Auto Insert
Comparative Analysis of the Optimization
Key Optimization Points
- Removal of the add_filter hook
- Old implementation: Relied on the WordPress native
the_contentfilter - New implementation: Directly uses WPCode’s Location feature, avoiding hook conflicts
- Simplified function parameters
- Old implementation: The function accepted the
$contentparameter and required content concatenation - New implementation: The function no longer accepts parameters, resulting in a single, focused responsibility
- Clarified execution flow
- Old implementation: Executed automatically via the filter
- New implementation: Explicitly called via
echo, making the execution flow clearer
- 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
- 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
- Configuration flexibility
- Insertion position and priority can be adjusted directly through the WPCode interface
- CTA display position can be changed without modifying code
- Avoidance of hook conflicts
- No longer conflicts with other plugins using the
the_contentfilter - Improved compatibility with other plugins
- 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

