Bunu yıllardır kimseye ben anlatamadım.
Söylediklerinize ek olarak eklenti yerine şunu yapsak müthiş olur:
Seo eklentisini 15-20 yıldır sadece özel meta açıklama veya title için kullandım. Artık temaya basit bir edit yaptırıp içerik sayfasında bu alanların özel veri ile manipüle edilmesi çok kolay.
prompt: rank math seo eklentisi kullanmak yerine bana aşağıdakileri sağlayacak bir fonksiyon yaz:wordpress ürün, post, page içeriklerine özel title ve meta desc yazabileceğim özel alanların açılmasını sağla, eğer onlar doluysa da ön yüzde ilgili veriler o alanlardan çekilmeli. temaya eklenecek bir fonksiyon.
kod(denenmedi bence çalışır):
/**
* WordPress Özel SEO Meta Alanları
* Bu kodu temanızın functions.php dosyasına ekleyin
*/
// 1. Meta Box'ları Ekle
add_action('add_meta_boxes', 'custom_seo_meta_boxes');
function custom_seo_meta_boxes() {
$post_types = array('post', 'page', 'product'); // Desteklenen post tipleri
foreach ($post_types as $post_type) {
add_meta_box(
'custom_seo_meta',
'SEO Ayarları',
'custom_seo_meta_box_callback',
$post_type,
'normal',
'high'
);
}
}
// 2. Meta Box İçeriği
function custom_seo_meta_box_callback($post) {
wp_nonce_field('custom_seo_meta_box', 'custom_seo_meta_box_nonce');
$custom_title = get_post_meta($post->ID, '_custom_seo_title', true);
$custom_description = get_post_meta($post->ID, '_custom_seo_description', true);
?>
<style>
.seo-meta-box {
margin: 10px 0;
}
.seo-meta-box label {
display: block;
font-weight: bold;
margin-bottom: 5px;
}
.seo-meta-box input[type="text"],
.seo-meta-box textarea {
width: 100%;
padding: 8px;
box-sizing: border-box;
}
.seo-meta-box textarea {
min-height: 80px;
resize: vertical;
}
.seo-meta-box .description {
color: #666;
font-size: 13px;
margin-top: 5px;
}
.seo-char-count {
text-align: right;
font-size: 12px;
color: #666;
margin-top: 3px;
}
.seo-char-count.warning {
color: #d63638;
}
.seo-char-count.good {
color: #00a32a;
}
</style>
<div class="seo-meta-box">
<label for="custom_seo_title">SEO Başlık (Title)</label>
<input type="text" id="custom_seo_title" name="custom_seo_title" value="<?php echo esc_attr($custom_title); ?>" />
<div id="title-char-count" class="seo-char-count"></div>
<p class="description">Arama motorlarında görünecek başlık. Boş bırakılırsa varsayılan başlık kullanılır. (Önerilen: 50-60 karakter)</p>
</div>
<div class="seo-meta-box">
<label for="custom_seo_description">Meta Açıklama (Description)</label>
<textarea id="custom_seo_description" name="custom_seo_description"><?php echo esc_textarea($custom_description); ?></textarea>
<div id="desc-char-count" class="seo-char-count"></div>
<p class="description">Arama sonuçlarında görünecek açıklama. Boş bırakılırsa otomatik oluşturulur. (Önerilen: 150-160 karakter)</p>
</div>
<script>
jQuery(document).ready(function($) {
// Karakter sayacı fonksiyonu
function updateCharCount(input, counterId, optimalMin, optimalMax) {
var $input = $(input);
var $counter = $(counterId);
var length = $input.val().length;
$counter.text(length + ' karakter');
$counter.removeClass('warning good');
if (length > 0) {
if (length < optimalMin || length > optimalMax) {
$counter.addClass('warning');
} else {
$counter.addClass('good');
}
}
}
// Title karakter sayacı
$('#custom_seo_title').on('input', function() {
updateCharCount(this, '#title-char-count', 50, 60);
}).trigger('input');
// Description karakter sayacı
$('#custom_seo_description').on('input', function() {
updateCharCount(this, '#desc-char-count', 150, 160);
}).trigger('input');
});
</script>
<?php
}
// 3. Meta Verileri Kaydet
add_action('save_post', 'save_custom_seo_meta');
function save_custom_seo_meta($post_id) {
// Güvenlik kontrolleri
if (!isset($_POST['custom_seo_meta_box_nonce'])) {
return;
}
if (!wp_verify_nonce($_POST['custom_seo_meta_box_nonce'], 'custom_seo_meta_box')) {
return;
}
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
if (!current_user_can('edit_post', $post_id)) {
return;
}
// Verileri kaydet
if (isset($_POST['custom_seo_title'])) {
update_post_meta($post_id, '_custom_seo_title', sanitize_text_field($_POST['custom_seo_title']));
}
if (isset($_POST['custom_seo_description'])) {
update_post_meta($post_id, '_custom_seo_description', sanitize_textarea_field($_POST['custom_seo_description']));
}
}
// 4. Ön Yüzde Meta Tagları Göster
add_action('wp_head', 'output_custom_seo_meta_tags', 1);
function output_custom_seo_meta_tags() {
global $post;
// Varsayılan değerler
$title = '';
$description = '';
if (is_singular()) {
// Tekil sayfa/yazı/ürün sayfalarında
if ($post) {
// Özel title kontrolü
$custom_title = get_post_meta($post->ID, '_custom_seo_title', true);
if (!empty($custom_title)) {
$title = $custom_title;
} else {
// Varsayılan title
$title = get_the_title($post->ID) . ' - ' . get_bloginfo('name');
}
// Özel description kontrolü
$custom_description = get_post_meta($post->ID, '_custom_seo_description', true);
if (!empty($custom_description)) {
$description = $custom_description;
} else {
// Varsayılan description (excerpt veya içeriğin ilk kısmı)
if (has_excerpt($post->ID)) {
$description = wp_strip_all_tags(get_the_excerpt($post->ID));
} else {
$content = wp_strip_all_tags($post->post_content);
$description = wp_trim_words($content, 30, '...');
}
}
}
} elseif (is_home() || is_front_page()) {
// Ana sayfa
$title = get_bloginfo('name') . ' - ' . get_bloginfo('description');
$description = get_bloginfo('description');
} elseif (is_category() || is_tag() || is_tax()) {
// Kategori, etiket ve taksonomi sayfaları
$term = get_queried_object();
if ($term) {
$title = $term->name . ' - ' . get_bloginfo('name');
$description = !empty($term->description) ? wp_strip_all_tags($term->description) : $term->name . ' ile ilgili içerikler';
}
} elseif (is_author()) {
// Yazar sayfası
$author = get_queried_object();
if ($author) {
$title = $author->display_name . ' - ' . get_bloginfo('name');
$description = get_the_author_meta('description', $author->ID);
if (empty($description)) {
$description = $author->display_name . ' tarafından yazılan içerikler';
}
}
} elseif (is_archive()) {
// Arşiv sayfaları
$title = get_the_archive_title() . ' - ' . get_bloginfo('name');
$description = get_the_archive_description();
} elseif (is_search()) {
// Arama sayfası
$title = 'Arama: ' . get_search_query() . ' - ' . get_bloginfo('name');
$description = '"' . get_search_query() . '" için arama sonuçları';
} elseif (is_404()) {
// 404 sayfası
$title = '404 - Sayfa Bulunamadı - ' . get_bloginfo('name');
$description = 'Aradığınız sayfa bulunamadı.';
}
// Meta tagları çıktısı
if (!empty($title)) {
// Mevcut title tag'ini kaldır ve yenisini ekle
remove_action('wp_head', '_wp_render_title_tag', 1);
echo '<title>' . esc_html($title) . '</title>' . "\n";
}
if (!empty($description)) {
echo '<meta name="description" content="' . esc_attr($description) . '" />' . "\n";
}
// Open Graph meta tagları (bonus)
if (is_singular() && $post) {
echo '<meta property="og:title" content="' . esc_attr($title) . '" />' . "\n";
echo '<meta property="og:description" content="' . esc_attr($description) . '" />' . "\n";
echo '<meta property="og:url" content="' . esc_url(get_permalink($post->ID)) . '" />' . "\n";
echo '<meta property="og:type" content="article" />' . "\n";
// Öne çıkan görsel varsa
if (has_post_thumbnail($post->ID)) {
$image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'large');
if ($image) {
echo '<meta property="og:image" content="' . esc_url($image[0]) . '" />' . "\n";
}
}
}
// Twitter Card meta tagları (bonus)
echo '<meta name="twitter:card" content="summary_large_image" />' . "\n";
if (!empty($title)) {
echo '<meta name="twitter:title" content="' . esc_attr($title) . '" />' . "\n";
}
if (!empty($description)) {
echo '<meta name="twitter:description" content="' . esc_attr($description) . '" />' . "\n";
}
}
// 5. WordPress varsayılan title tag'ini düzelt
add_filter('pre_get_document_title', 'custom_seo_filter_title', 10);
function custom_seo_filter_title($title) {
global $post;
if (is_singular() && $post) {
$custom_title = get_post_meta($post->ID, '_custom_seo_title', true);
if (!empty($custom_title)) {
return $custom_title;
}
}
return $title;
}
// 6. Yoast SEO veya Rank Math ile çakışmayı önle
add_action('init', 'disable_other_seo_plugins_meta');
function disable_other_seo_plugins_meta() {
// Yoast SEO varsa meta taglarını kaldır
if (defined('WPSEO_VERSION')) {
add_filter('wpseo_title', '__return_false');
add_filter('wpseo_metadesc', '__return_false');
add_filter('wpseo_opengraph_title', '__return_false');
add_filter('wpseo_opengraph_desc', '__return_false');
}
// Rank Math varsa meta taglarını kaldır
if (class_exists('RankMath')) {
add_filter('rank_math/frontend/title', '__return_false');
add_filter('rank_math/frontend/description', '__return_false');
}
}