// TEMP: Replace existing related links
add_action('rest_api_init', function() {
register_rest_route('custom/v1', '/replace-related-links', array(
'methods'=>'POST','callback'=>'custom_replace_rel_links',
'permission_callback'=>function(){return current_user_can('edit_posts');}
));
});
function custom_replace_rel_links($request) {
$items = $request->get_param('items') ?: array();
$res = array('success'=>0,'failed'=>0,'errors'=>array());
foreach ($items as $item) {
$pid = intval($item['post_id'] ?? 0);
$html = wp_kses_post($item['html'] ?? '');
if (!$pid || !$html) { $res['failed']++; continue; }
$post = get_post($pid);
if (!$post) { $res['failed']++; continue; }
// Remove old related-products-links block and add new one
$content = $post->post_content;
// Remove any existing related-products-links paragraph
$content = preg_replace('/
]*class="related-products-links"[^>]*>.*?<\/p>/s', '', $content);
$content = rtrim($content) . $html;
$r = wp_update_post(array('ID'=>$pid,'post_content'=>$content), true);
if (is_wp_error($r)) { $res['failed']++; $res['errors'][] = $pid; } else { $res['success']++; }
}
return $res;
}
Commercial Fire Alarms, Smoke Sensors & Bases | Life Safety Consultants
“Kidde (GSA-CR) Control Relay Module” has been added to your cart.
View cart