Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
WPB Kupası
25 Million+
Websites using our plugins
16+
Years of WordPress experience
3000+
WordPress tutorials
by experts

WordPress’te Yazı Yayınlandığında Özel Alanlar Otomatik Olarak Nasıl Eklenir

Editoryal Not: WPBeginner üzerindeki ortak bağlantılardan komisyon kazanıyoruz. Komisyonlar, editörlerimizin görüşlerini veya değerlendirmelerini etkilemez. Editoryal Süreç hakkında daha fazla bilgi edinin.

WordPress gönderilerinizi yayınlarken otomatik olarak özel alanlar oluşturmanız mı gerekiyor?

Bu, geliştiricilerin WordPress web sitelerine yeni özellikler eklerken kullanabilecekleri basit bir hiledir.

Bu makalede, WordPress’te yazı yayınlarken özel alanları otomatik olarak nasıl ekleyeceğinizi göstereceğiz.

How to Add Custom Fields Automatically on Post Publish in WordPress

Neden Özel Alanları Otomatik Olarak Ekleyelim?

Özel alanlar yazılarınıza ek bilgiler eklemenizi sağlar. Bu bilgiler web sitenizde görüntülenebilir, gizli tutulabilir veya WordPress web sitenizin işlevselliğini artırmak için temalar ve eklentiler tarafından kullanılabilir.

Özel alanları kullanmanın birçok yolu vardır. Özel alanlar ipuçları, püf noktaları ve hack ‘ler kılavuzumuzda yararlı fikirlerin bir listesini bulacaksınız.

Bazen bir yazı yayınladığınızda özel bir alanın otomatik olarak oluşturulmasını istersiniz. Bu, özellikle WordPress’e işlevsellik eklediğinizde, onu basit bir blogdan daha fazlası olarak kullanabilmeniz için geçerlidir.

Bu yöntemi bir galeri web sitesi oluştururken kullandık. Galeriye gönderilen her öğe için kısa URL’leri saklamak istedik. Bu nedenle, her gönderi yayınlandığında kısa URL ‘yi saklamak için otomatik olarak özel bir alan oluşturduk.

Bu numara, WordPress’i bir sonraki seviyeye taşımak isteyen geliştiriciler için çok yararlı olabilir.

Gönderi Yayınlandığında Özel Alanları Otomatik Olarak Ekleme

Bu yöntem, temanızın functions.php dosyasına özel bir kod parçacığı eklemeyi içerir. Tema dosyalarınızı deneyimsiz kullanıcıların düzenlemesini önermiyoruz, çünkü küçük bir hata bile web sitenizi bozabilir.

Bunun yerine, bu eğitimde size WPCode eklentisini nasıl kullanacağınızı göstereceğiz.

WPCode

WPCode, temanızın functions.php dosyasını düzenlemek zorunda kalmadan WordPress’e kod parçacıkları eklemeyi kolaylaştırır. Ayrıca tüm kod parçacıklarınızı tek bir merkezi ekrandan yönetebilirsiniz.

WordPress’e ilk kez kod ekliyorsanız, daha fazla ayrıntı için WordPress’te kod parçacıklarının nasıl kopyalanıp yapıştırılacağına ilişkin kılavuzumuza göz atmalısınız.

Başlamak için ücretsiz WPCode eklentisini yüklemeniz ve etkinleştirmeniz gerekir. Yardıma ihtiyacınız varsa, WordPress eklentisi yükleme hakkındaki eğitimimize bakın.

Not: WPCode’un ücretsiz sürümü WordPress’e özel kod eklemek için ihtiyacınız olan her şeye sahiptir. Zamanlanmış snippet’ler, dönüşüm pikselleri ve daha fazlası gibi daha gelişmiş özellikler için WPCode Pro‘ya yükseltebilirsiniz.

Eklenti etkinleştirildikten sonra, WordPress yönetici çubuğunuza ‘Code Snippets’ etiketli yeni bir menü öğesi eklenecektir. Üzerine tıklayın ve ardından bir sonraki ekranda ‘Yeni Ekle’ düğmesine basın.

Click the Add New Button to Add Your First Custom Code Snippet in WPCode

Buradan, ‘Özel Kodunuzu Ekleyin (Yeni Snippet)’ seçeneğine gidin ve altındaki ‘Snippet kullan’ düğmesine tıklayın.

Add your new custom code snippet in WPCode

Bundan sonra, snippet’e bir başlık vermeniz ve ardından aşağıdaki kodu kopyalayıp ‘Kod Önizleme’ kutusuna yapıştırmanız gerekir.

add_action('publish_page', 'add_custom_field_automatically');
add_action('publish_post', 'add_custom_field_automatically');
function add_custom_field_automatically($post_ID) {
    global $wpdb;
    if(!wp_is_post_revision($post_ID)) {
        add_post_meta($post_ID, 'field-name', 'custom value', true);
    }
}

‘field-name’ ve ‘custom value’ ifadelerini özel alan için kullanmak istediğiniz gerçek ad ve değerle değiştirmeniz gerekir.

Sağdaki açılır menüden kod türü olarak ‘PHP Snippet’ seçmeyi unutmayın.

Paste code snippet in WPCode code box

Ardından, ‘Ekleme’ bölümüne ilerleyin. Burada, ‘Otomatik Ekle’ yöntemini seçili bırakmanız gerekecektir.

Pick the insertion method for your custom code snippet

Otomatik Ekleme yöntemiyle, snippet otomatik olarak uygun konuma eklenecek ve yürütülecektir.

İşiniz bittiğinde, anahtarı ‘Etkin Değil’den ‘Etkin’e getirmeniz ve ardından ‘Snippet’i Kaydet’ düğmesine tıklamanız gerekir.

Save and activate your custom code snippet

Snippet etkinleştirildikten sonra, bir gönderi yayınladığınızda özel alan oluşturulacaktır.

Umarız bu eğitim WordPress’te yazı yayınlarken otomatik olarak nasıl özel alan ekleyeceğinizi öğrenmenize yardımcı olmuştur. Ayrıca en iyi WordPress hosting’i nasıl seçeceğinizi öğrenmek veya web sitenizi büyütmek için sahip olunması gereken WordPress eklentileri listemize göz atmak isteyebilirsiniz.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

Açıklama: İçeriğimiz okuyucu desteklidir. Bu, bazı bağlantılarımıza tıklarsanız komisyon kazanabileceğimiz anlamına gelir. WPBeginner'ın nasıl finanse edildiğini, neden önemli olduğunu ve nasıl destek olabileceğinizi görün. İşte editoryal sürecimiz.

Avatar

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi with over 16 years of experience in WordPress, Web Hosting, eCommerce, SEO, and Marketing. Started in 2009, WPBeginner is now the largest free WordPress resource site in the industry and is often referred to as the Wikipedia for WordPress.

Ultimate WordPress Araç Kiti

Araç setimize ÜCRETSİZ erişim sağlayın - her profesyonelin sahip olması gereken WordPress ile ilgili ürün ve kaynaklardan oluşan bir koleksiyon!

Reader Interactions

17 yorumBir Cevap Bırakın

  1. Syed Balkhi says

    Hey WPBeginner readers,
    Did you know you can win exciting prizes by commenting on WPBeginner?
    Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
    You can get more details about the contest from here.
    Start sharing your thoughts below to stand a chance to win!

  2. nayan says

    I want to add category Id for the post in the post_meta table. How can be the function function add_custom_field_automatically($post_ID) be twicked to accomodate that?

    Thanks

  3. chris says

    Instead of adding the custom field at the time of creating the post, how do I display a custom field by default on the admin page?

    • Editorial Staff says

      If you notice, the code above doesn’t have anything related to the custom post types. It only adds to Post and Page “content type”. So you would have to specify the hook for your custom post type.

      Yönetici

      • puanthanh says

        Thanks for the reply. Can you help me out on this code.
        add_action(‘publish_page’, ‘add_custom_field_automatically’);
        add_action(‘publish_post’, ‘add_custom_field_automatically’);
        add_action( ‘save_post’, ‘add_custom_field_automatically’ );

        function add_custom_field_automatically($post_ID) {
        global $post;
        if(!wp_is_post_revision($post_ID)) {
        global $wpdb;
        $user_id = $wpdb->get_var("SELECT post_author FROM $wpdb->posts WHERE ID = $post_ID");
        $themename = $wpdb->get_var("SELECT meta_value FROM $wpdb->usermeta WHERE meta_key = ‘themeperauthor’ AND user_id = $user_id");
        add_post_meta($post_ID, ‘themeperauthor’, $themename, true);
        }
        }
        When the user meta field is changed, I want to automatically delete the custom field value and update with the new one

  4. brian says

    I’ve been working on adding some hidden custom fields on post publish/update (by preceding the field name with “_”) but for some reason I have to update the post twice before the wp_postmeta entries are written to the database.

    I’ve tried messing with the priority and experimenting with other action hooks (draft_to_publish, edit_post, etc) but it doesn’t seem to make a difference.

    Any ideas?

  5. Piet says

    will this work too for custom post types?
    sth like add_action(‘publish_custom-post-typ-name’, ‘add_custom_field_automatically’);

    • Otto says

      Yes, it will.

      One downside to this technique that people should be aware of is that if somebody edits a published post, this hook WILL get fired again on the edit. Therefore, you need to check for the meta before adding it, or to update it, or to do whatever makes the most sense for your use-case.

      If you only want to get your code fired off on the initial publish only, then you can use the transition_post_status hook. This hook works like this:


      add_action('transition_post_status','example',10,3);
      function example($new, $old, $post) {
      // $new is the new post status ('publish')
      // $old is the old post status ('draft')
      // $post is the complete Post Object (so use $post->ID for the ID, etc)
      }

      Then, in here you can do a check for something like this:

      if ($new == 'publish' && $old != 'publish')

      To have your code only used when the post status actually transitions to publish from whatever it was before. This hook is fired at the same time as the {$status}_{$post-type} hooks are, so the operation of them is basically the same.

Bir Cevap Bırakın

Yorum bırakmayı seçtiğiniz için teşekkür ederiz. Lütfen tüm yorumların yorum poli̇ti̇kasi uyarınca denetlendiğini ve e-posta adresinizin yayımlanmayacağını unutmayın. Ad alanında anahtar kelime KULLANMAYIN. Kişisel ve anlamlı bir sohbet edelim.