<?
php
/*
Plugin Name: Feed to Post Importer
Description: Fetch posts from an RSS feed every hour and publish them with category
mapping. Redirect posts to referral URLs.
Version: 1.0
Author: Your Name
*/
if (!defined('ABSPATH')) {
exit; // Exit if accessed directly.
}
class FeedToPostImporter {
public function __construct() {
// Hooks
add_action('admin_menu', [$this, 'add_settings_page']);
add_action('admin_init', [$this, 'register_settings']);
add_action('feed_to_post_import_event', [$this, 'fetch_and_publish_feed']);
add_action('template_redirect', [$this, 'redirect_to_referral_url']);
// Schedule the event if not scheduled
if (!wp_next_scheduled('feed_to_post_import_event')) {
wp_schedule_event(time(), 'hourly', 'feed_to_post_import_event');
}
}
// Add settings page
public function add_settings_page() {
add_options_page(
'Feed to Post Settings',
'Feed to Post',
'manage_options',
'feed-to-post-settings',
[$this, 'settings_page_html']
);
}
// Register settings
public function register_settings() {
register_setting('feed_to_post_settings', 'feed_url');
register_setting('feed_to_post_settings', 'category_mapping');
}
// Settings page HTML
public function settings_page_html() {
?>
<div class="wrap">
<h1>Feed to Post Settings</h1>
<form method="post" action="options.php">
<?php
settings_fields('feed_to_post_settings');
do_settings_sections('feed_to_post_settings');
?>
<table class="form-table">
<tr valign="top">
<th scope="row">Feed URL</th>
<td><input type="text" name="feed_url" value="<?php echo
esc_attr(get_option('feed_url')); ?>" style="width: 100%;" /></td>
</tr>
<tr valign="top">
<th scope="row">Category Mapping (Feed Category => WP
Category ID)</th>
<td><textarea name="category_mapping" rows="5"
style="width: 100%;"><?php echo esc_textarea(get_option('category_mapping'));
?></textarea>
<p>Example: Technology=2, Business=3</p></td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php
}
// Fetch and publish feed
public function fetch_and_publish_feed() {
$feed_url = get_option('feed_url');
if (!$feed_url) return;
$feed = fetch_feed($feed_url);
if (is_wp_error($feed)) return;
$max_items = $feed->get_item_quantity(5);
$items = $feed->get_items(0, $max_items);
$category_mapping = $this-
>parse_category_mapping(get_option('category_mapping'));
foreach ($items as $item) {
$title = $item->get_title();
$description = $item->get_description();
$link = $item->get_permalink();
$categories = $item->get_categories();
$enclosure = $item->get_enclosure();
$image_url = $enclosure ? $enclosure->get_link() : '';
// Check if post already exists
if (post_exists($title)) continue;
// Map categories
$wp_categories = [];
if ($categories) {
foreach ($categories as $category) {
$cat_name = $category->get_label();
if (isset($category_mapping[$cat_name])) {
$wp_categories[] = (int)$category_mapping[$cat_name];
}
}
}
// Insert post
$post_id = wp_insert_post([
'post_title' => $title,
'post_content' => $description,
'post_status' => 'publish',
'post_category' => $wp_categories,
'meta_input' => ['referral_url' => esc_url_raw($link)]
]);
// Set featured image if available
if ($image_url) {
$this->set_featured_image($image_url, $post_id);
}
}
}
// Set featured image
private function set_featured_image($image_url, $post_id) {
require_once(ABSPATH . 'wp-admin/includes/image.php');
require_once(ABSPATH . 'wp-admin/includes/file.php');
require_once(ABSPATH . 'wp-admin/includes/media.php');
$tmp = download_url($image_url);
if (is_wp_error($tmp)) return;
$file_array = [
'name' => basename($image_url),
'tmp_name' => $tmp
];
$id = media_handle_sideload($file_array, $post_id);
if (!is_wp_error($id)) {
set_post_thumbnail($post_id, $id);
}
}
// Parse category mapping
private function parse_category_mapping($mapping_str) {
$mapping = [];
$pairs = explode(',', $mapping_str);
foreach ($pairs as $pair) {
list($feed_cat, $wp_cat_id) = array_map('trim', explode('=', $pair));
if ($feed_cat && $wp_cat_id) {
$mapping[$feed_cat] = $wp_cat_id;
}
}
return $mapping;
}
// Redirect to referral URL
public function redirect_to_referral_url() {
if (is_single()) {
global $post;
$referral_url = get_post_meta($post->ID, 'referral_url', true);
if ($referral_url) {
wp_redirect($referral_url);
exit;
}
}
}
// Clear scheduled event on plugin deactivation
public static function deactivate() {
$timestamp = wp_next_scheduled('feed_to_post_import_event');
wp_unschedule_event($timestamp, 'feed_to_post_import_event');
}
}
new FeedToPostImporter();
// Register deactivation hook
register_deactivation_hook(__FILE__, ['FeedToPostImporter', 'deactivate']);