Selamlar, rest api ile seo eklentilerinin title ve description alanlarını doldurmak istiyorum sabit bir meta_title ve meta_description var bu alanlardan çekecek. Kodlar aşağıdaki gibi mantık olarak çalışması gerekiyor ancak yoastı bir türlü dolduramıyorum. Çözebilecek arkadaşları bekliyorum
add_action('wp_ajax_publish_to_site', 'publish_to_site');
function publish_to_site() {
check_ajax_referer('custom_nonce_makale_yayinla', 'nonce');
if (!current_user_can('edit_posts')) {
wp_send_json_error('İzin verilmedi.');
return;
}
$post_id = isset($_POST['post_id']) ? intval($_POST['post_id']) : 0;
$site_id = isset($_POST['site_id']) ? intval($_POST['site_id']) : 0;
$publish_status = sanitize_text_field($_POST['publish_status']);
$seo_eklentisi = sanitize_text_field($_POST['seo_eklentisi']);
$meta_title = get_post_meta($post_id, 'meta_title', true);
$meta_description = get_post_meta($post_id, 'meta_description', true);
$publish_date = sanitize_text_field($_POST['publish_date']);
if (!$post_id || !$site_id) {
wp_send_json_error('Geçersiz makale veya site ID.');
return;
}
$site_adresi = get_post_meta($site_id, 'site_adresi', true);
$site_kullanici_adi = get_post_meta($site_id, 'site_kullanici_adi', true);
$sifre = get_post_meta($site_id, 'sifre', true);
if (!$site_adresi || !$site_kullanici_adi || !$sifre) {
wp_send_json_error('Site bilgileri eksik.');
return;
}
if (!filter_var($site_adresi, FILTER_VALIDATE_URL)) {
wp_send_json_error('Geçersiz site adresi.');
return;
}
$allowed_statuses = ['publish', 'draft', 'future']; // 'future' planlanmış gönderiler için kullanılır
if (!in_array($publish_status, $allowed_statuses, true)) {
wp_send_json_error('Geçersiz yayın durumu.');
return;
}
$data = [
'title' => get_the_title($post_id),
'content' => get_post_field('post_content', $post_id),
'status' => $publish_status,
'meta_input' => []
];
if ($publish_status === 'future' && !empty($publish_date)) {
$date = date_create($publish_date);
if (!$date) {
wp_send_json_error('Geçersiz yayın tarihi formatı.');
return;
}
$data['date'] = date_format($date, 'Y-m-d H:i:s');
}
// Yoast SEO meta alanlarını ekleme
if ($seo_eklentisi && $seo_eklentisi === 'yoast') {
if (!empty($meta_title)) {
$data['meta_input']['_yoast_wpseo_title'] = sanitize_text_field($meta_title);
}
if (!empty($meta_description)) {
$data['meta_input']['_yoast_wpseo_metadesc'] = sanitize_text_field($meta_description);
}
}
$json_data = json_encode($data);
$response = wp_remote_post($site_adresi, [
'method' => 'POST',
'body' => $json_data,
'headers' => [
'Authorization' => 'Basic ' . base64_encode($site_kullanici_adi . ':' . $sifre),
'Content-Type' => 'application/json',
],
'sslverify' => false,
]);
if (is_wp_error($response)) {
wp_send_json_error('Siteye bağlanırken bir hata oluştu.');
return;
}
$body = wp_remote_retrieve_body($response);
$response_code = wp_remote_retrieve_response_code($response);
if ($response_code < 200 || $response_code >= 300) {
error_log('Publish to site error: ' . $response_code . ' - ' . $body);
wp_send_json_error('Hata oluştu, lütfen daha sonra tekrar deneyin.');
return;
}
error_log('Publish to site success: ' . $body);
wp_send_json_success('Makale başarıyla yayınlandı.');
}