çekim ayarlarından rss adresi ekliyorsunuz

gerekli ayarlamaları yapıyorsunuz

şimdi çek derseniz 20 adet çekti taslak olarak bıraktı


çekilen haber makalesi örnek içeriği

bu eklenti geliştirilebilir daha iyi hale getirilebilir
<?php
/**
* Plugin Name: Gelişmiş Haber Çekici
* Description: RSS beslemelerinden haberleri çeker, otomatik olarak taslak yazılar olarak kaydeder.
* Version: 2.0
* Author: Yapay Zeka Asistanı
* Author URI: https://ornek.com
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: gelismis-haber-cekici
*/
// Güvenlik kontrolü
if (!defined('ABSPATH')) {
exit;
}
// --- 1. AKTİVASYON VE DEAKTİVASYON KANCALARI ---
// Eklenti aktif olduğunda zamanlanmış görevi oluştur
register_activation_hook(__FILE__, 'ghc_activate');
function ghc_activate() {
// Varsayılan ayarları oluştur
$default_options = [
'rss_url' => '',
'author_id' => get_current_user_id(),
'category_id' => 1,
'schedule' => 'hourly',
];
add_option('ghc_settings', $default_options);
// Zamanlanmış görevi (WP-Cron) ayarla
if (!wp_next_scheduled('ghc_cron_event')) {
wp_schedule_event(time(), 'hourly', 'ghc_cron_event');
}
}
// Eklenti deaktif olduğunda zamanlanmış görevi temizle
register_deactivation_hook(__FILE__, 'ghc_deactivate');
function ghc_deactivate() {
wp_clear_scheduled_hook('ghc_cron_event');
}
// --- 2. ZAMANLANMIŞ GÖREVİN ÇALIŞTIRILMASI ---
add_action('ghc_cron_event', 'ghc_proceed_import');
// --- 3. ÇEKİM İŞLEMLERİNİ YAPAN ANA FONKSİYON ---
function ghc_proceed_import() {
$options = get_option('ghc_settings');
$rss_url = !empty($options['rss_url']) ? esc_url_raw($options['rss_url']) : null;
if (!$rss_url) {
return; // RSS URL boşsa işlemi durdur.
}
$rss = fetch_feed($rss_url);
if (is_wp_error($rss)) {
error_log('Gelişmiş Haber Çekici Hatası: ' . $rss->get_error_message());
return;
}
$maxitems = $rss->get_item_quantity(10); // Bir seferde en fazla 10 haber çek
$rss_items = $rss->get_items(0, $maxitems);
$author_id = !empty($options['author_id']) ? intval($options['author_id']) : 1;
$category_id = !empty($options['category_id']) ? intval($options['category_id']) : 1;
foreach ($rss_items as $item) {
$permalink = $item->get_permalink();
// TEKRAR ENGELLEME: Bu link ile daha önce yazı eklenmiş mi kontrol et
if (get_posts(['meta_key' => '_ghc_source_url', 'meta_value' => $permalink])) {
continue; // Eğer eklenmişse bu haberi atla
}
$post_data = [
'post_title' => wp_strip_all_tags($item->get_title()),
'post_content' => $item->get_description() . '<br><br><a href="' . esc_url($permalink) . '" target="_blank">Kaynaktan Devamını Oku</a>',
'post_status' => 'draft',
'post_author' => $author_id,
'post_category' => [$category_id],
'post_type' => 'post',
];
$post_id = wp_insert_post($post_data);
// Yazı başarıyla oluşturulursa kaynak linkini meta olarak kaydet
if (!is_wp_error($post_id)) {
update_post_meta($post_id, '_ghc_source_url', $permalink);
}
}
}
// --- 4. AYARLAR SAYFASI ---
add_action('admin_menu', 'ghc_add_admin_menu');
add_action('admin_init', 'ghc_settings_init');
function ghc_add_admin_menu() {
add_options_page(
'Gelişmiş Haber Çekici Ayarları',
'Gelişmiş Haber Çekici',
'manage_options',
'gelismis-haber-cekici',
'ghc_options_page_html'
);
}
function ghc_settings_init() {
register_setting('ghc_settings_group', 'ghc_settings');
add_settings_section(
'ghc_settings_section',
'Çekim Ayarları',
null,
'gelismis-haber-cekici'
);
add_settings_field('ghc_rss_url', 'RSS Besleme Adresi', 'ghc_rss_url_render', 'gelismis-haber-cekici', 'ghc_settings_section');
add_settings_field('ghc_author_id', 'Yazar', 'ghc_author_id_render', 'gelismis-haber-cekici', 'ghc_settings_section');
add_settings_field('ghc_category_id', 'Kategori', 'ghc_category_id_render', 'gelismis-haber-cekici', 'ghc_settings_section');
add_settings_field('ghc_schedule', 'Çekim Sıklığı', 'ghc_schedule_render', 'gelismis-haber-cekici', 'ghc_settings_section');
}
// Ayarlar alanlarını render eden fonksiyonlar
function ghc_rss_url_render() {
$options = get_option('ghc_settings');
echo '<input type="url" name="ghc_settings[rss_url]" value="' . esc_attr($options['rss_url']) . '" size="60" placeholder="https://www.ornek.com/rss.xml" />';
}
function ghc_author_id_render() {
$options = get_option('ghc_settings');
wp_dropdown_users([
'name' => 'ghc_settings[author_id]',
'selected' => $options['author_id'],
'show_option_none' => 'Site Yöneticisi'
]);
}
function ghc_category_id_render() {
$options = get_option('ghc_settings');
wp_dropdown_categories([
'name' => 'ghc_settings[category_id]',
'selected' => $options['category_id'],
'show_option_none' => 'Genel',
'hide_empty' => 0,
]);
}
function ghc_schedule_render() {
$options = get_option('ghc_settings');
$schedules = wp_get_schedules();
echo '<select name="ghc_settings[schedule]">';
foreach ($schedules as $key => $schedule) {
echo '<option value="' . esc_attr($key) . '" ' . selected($options['schedule'], $key, false) . '>' . esc_html($schedule['display']) . '</option>';
}
echo '</select>';
}
function ghc_options_page_html() {
// Manuel çekim işlemini burada handle et
if (isset($_POST['ghc_manual_import']) && check_admin_referer('ghc_manual_import_nonce')) {
ghc_proceed_import();
echo '<div class="notice notice-success is-dismissible"><p>Manuel çekim işlemi başlatıldı. Oluşturulan taslakları <a href="/wp-admin/edit.php?post_status=draft&post_type=post">buradan</a> görebilirsiniz.</p></div>';
}
// Ayarlar güncellendiğinde cron job'u yeniden ayarla
if (isset($_GET['settings-updated']) && $_GET['settings-updated'] == 'true') {
$options = get_option('ghc_settings');
wp_clear_scheduled_hook('ghc_cron_event');
wp_schedule_event(time(), $options['schedule'], 'ghc_cron_event');
}
?>
<div class="wrap">
<h1><?php echo esc_html(get_admin_page_title()); ?></h1>
<form action="options.php" method="post">
<?php
settings_fields('ghc_settings_group');
do_settings_sections('gelismis-haber-cekici');
submit_button('Ayarları Kaydet');
?>
</form>
<hr>
<h2>Manuel Haber Çekimi</h2>
<p>Ayarları kaydettikten sonra, RSS beslemesinden hemen haber çekmek için aşağıdaki butonu kullanabilirsiniz.</p>
<form method="post">
<?php wp_nonce_field('ghc_manual_import_nonce'); ?>
<input type="submit" name="ghc_manual_import" class="button button-primary" value="Şimdi Haberleri Çek">
</form>
</div>
<?php
}yapmanız gerekn klasör oluşturup haber-cekici klasör içine de verdiğim bu kodu notepad++ içine atıp habercekici.php olarak kayıt edip zip lemek oradan eklentiyi wordpresse atmak olacak geliştirmek isteyenler geliştirsin diye verdim başarılar
