Function WordPress all บันทึกส่วนตัว
การนำไฟล์จากภายนอกเข้ามาประมวลผล
include(plugin_dir_path(__FILE__)."repair-menu.php"); //เรียกไฟล์หน้า php ที่อยู่ในพาทเดียวกัน include(plugin_dir_path(__FILE__)."job/repair-menu.php"); //เรียกไฟล์หน้า php ที่อยู่ในโฟเดอร์ชื่อ job
สร้างการแจ้งเตือน
add_action( 'admin_notices', 'sample_admin_notice__success' );
wp_head ใช้กำหนดส่วน head tag <head></head> ที่ไฟล์ header.php ของธีม
function hook_css() { ?> <style> .wp_head_example { background-color : #f1f1f1; } </style> <?php } add_action('wp_head', 'hook_css');
wp_footer ใช้กำหนดส่วน ท้ายก่อน </body>
<?php function your_function() { echo '<p>This is inserted at the bottom</p>'; } add_action( 'wp_footer', 'your_function' ); ?>
add_option() ฟั่งชั่นที่จะเอาค่าออฟชั่นไปเก็บในตาราง wp_options
add_option( $option, $value = '', $deprecated = '', $autoload = 'yes' )
register_activation_hook( ) เมือเปิดการใช้งานปลั๊กอิน ให้ไปที่ฟังชั่นบางอย่างเช่น สร้างดาต้าเบส จะทำงานเมื่อ plugin activated ฟังก์ชันอัตโนมัติเมื่อ Activate plugin
function my_plugin_activate() { /* activation code here */ } register_activation_hook( __FILE__, 'my_plugin_activate' );
register_deactivation_hook( ) เมือถูกกดปิดการใช้งานปลั๊กอินให้มาทำงานในนี้ จะถูกทำงานเมื่อ plugin deactivated ฟังก์ชันอัตโนมัติเมื่อ deactivated plugin
function register_deactivation_hook( $file, $function ) { $file = plugin_basename( $file ); add_action( 'deactivate_' . $file, $function ); }
wp_nonce_field( ) คือ field ใส่ไว้ที่ form สำหรับการป้องกันการ reqeust จาก site ของตัวเอง และไม่ให้ ภายนอก request เข้ามาแนะนำว่าให้ควรใส่ไว้ใน form
function wp_nonce_field( $action = -1, $name = '_wpnonce', $referer = true, $echo = true ) { $name = esc_attr( $name ); $nonce_field = '<input type="hidden" id="' . $name . '" name="' . $name . '" value="' . wp_create_nonce( $action ) . '" />'; if ( $referer ) { $nonce_field .= wp_referer_field( false ); } if ( $echo ) { echo $nonce_field; } return $nonce_field; }
Classes
wpdb เชื่อมต่อกับฐานข้อมูล เป็นคลาส ต่อกับฐานขอมูลได้ มีฟังชั่นสำเร็จรูปให้ใช้งาน
// 1st Method - Declaring $wpdb as global and using it to execute an SQL query statement that returns a PHP object global $wpdb; $results = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}options WHERE option_id = 1", OBJECT );