Created
February 14, 2021 13:26
-
-
Save mebaysan/8c10ffe5ab66223f66dfb94384041114 to your computer and use it in GitHub Desktop.
WordPress Custom Plugin Örneği
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: BaysanSoft Deneme Plugin | |
Plugin URI: https://baysansoft.com | |
Description: BaysanSoft tarafından oluşturulan deneme plugini | |
Version: 1.0 | |
Author: Baysan | |
Author URI: https://baysansoft.com | |
License: GPL2 | |
*/ | |
/* | |
wp-content klasörü altına plugin adıyla bir klasör açıp index.php'i içine yapıştırıp, | |
wp-admin de eklentiler sekmesinde pluginimizi görebiliriz | |
*/ | |
add_action("admin_menu", "baysanPlugin"); # admin menüye ekleyeceğimiz plugin bileşenleri | |
function baysanPlugin() | |
{ | |
add_menu_page( # sidebar'a item ekleyen fonksiyon | |
"Eklenti başlığı", # Sayfanın html title'ı | |
"BaysanSoft Test Plugin", # Sidebar'da gözüken eklenti adı | |
"manage_options", # sabit | |
"baysansoft-tst-plgn", # Eklenti'nin uygulama içerisindeki linki | |
"icerikFunc" # bu sayfada çalışacak olan fonksiyon | |
); | |
} | |
function icerikFunc() | |
{ ?> | |
<form method="POST"> | |
<label><b>my-custom-meta</b> Değeri</label> | |
<input type="text" name="metaName"> | |
<input type="submit" value="Güncelle"> | |
</form> | |
<?php | |
add_post_meta( # postmeta tablosuna veri eklememizi sağlar, eğer bu keyde varsa eklemez | |
5, # metanın id'si | |
'my-custom-meta', # meta key, bu veriyi temanın herhangi bir yerinde çekmemizi sağlayan değer (key) | |
'Default Meta Değeri', # bu keyin value'si | |
true # unique mi | |
); | |
$newMeta = $_POST['metaName']; | |
$oldMeta = get_post_meta( # içine verdiğimiz id'lerdeki keylerin valuelerini döner | |
5, # hangi id'li (postmeta tablosundan) | |
'my-custom-meta', # key'i nedir | |
true # liste olarak mı gelsin yoksa tek değer mi | |
); | |
if ($newMeta) { | |
if ($newMeta != $oldMeta) { | |
update_post_meta( | |
5, # hangi id'li meta | |
'my-custom-meta', # hangi key | |
$newMeta, # yeni değer | |
$oldMeta # eski değer | |
); | |
echo '<h3>Güncellenen my-custom-meta Değeri: ' . '<b style="color:red;">' . $newMeta . '</b>' . '</h3>'; | |
} | |
} | |
} | |
function tabloOlustur() | |
{ | |
global $wpdb; # uygulamanın kullandığı db'i temsil eder | |
$charset = $wpdb->get_charset_collate(); # db'nin charseti | |
$tabloAdi = $wpdb->prefix . 'customTablo'; # tablonunu adı | |
$sql = "CREATE TABLE $tabloAdi( | |
id mediumint(9) NOT NULL AUTO_INCREMENT, | |
name VARCHAR(300) NOT NULL, | |
UNIQUE KEY id (id) ) $charset;"; # id'i unique set etmezsek çalışmaz | |
require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); # bu dosyayı çağırmamız gerek | |
dbDelta($sql); # sql scripti çalıştırır | |
register_activation_hook(__FILE__, 'creating_plugin_table'); # eklentiyi etkinleştirdiğimiz an bu db'nin oluşmasını sağlar | |
} | |
function veriEkle($tabloAdi, $params) | |
{ | |
global $wpdb; | |
$wpdb->insert( | |
$tabloAdi, # veri eklenecek tablonun adı | |
$params # array şeklinde ('kolon1' => 'değer1', 'kolon2' => 'değer2') | |
); | |
} | |
tabloOlustur(); | |
veriEkle('wp_customTablo', array('name' => 'Enes Baysan')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment