Code bắt đầu viết một plugin: đây là code để chúng ta bắt đầu triển khai một plugin
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<?php /* Plugin Name: Top Shop List Plugin URI: https://chilitheme.com/ Description: Top Shop List Text Domain: topshoplist Domain Path: /languages Author: Tung Nguyen Author URI: https://chilitheme.com/ License: GPLv2 Version: 1.0.0 */ // BASIC SECURITY defined( 'ABSPATH' ) or die( 'Unauthorized Access!' ); $plugin_data = get_file_data(__FILE__, array('Version' => 'Version'), false); $plugin_version = $plugin_data['Version']; define('TOPSHOPLIST_FILE', basename(__FILE__)); define('TOPSHOPLIST_URL', plugin_dir_url(__FILE__)); define('TOPSHOPLIST_ASSETS_URL', TOPSHOPLIST_URL."assets/"); define('TOPSHOPLIST_CSS_URL', TOPSHOPLIST_URL."assets/css/"); define('TOPSHOPLIST_IMAGES_URL', TOPSHOPLIST_URL."assets/images/"); define('TOPSHOPLIST_JS_URL', TOPSHOPLIST_URL."assets/js/"); define('TOPSHOPLIST_MENU_POSITION', 66); //previously 30 define('TOPSHOPLIST_PLUGIN_DIR', plugin_dir_path( __FILE__ ) ); define('TOPSHOPLIST_VIEWS_DIR', TOPSHOPLIST_PLUGIN_DIR."includes/views/" ); define('TOPSHOPLIST_PLUGIN_VER', $plugin_version ); class Top_Shop_List{ function __construct(){ } } new Top_Shop_List(); |
Đoạn code dưới giúp chúng ta tạo cơ sở dữ liệu khi kích hoạt plugin
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
class Visitor_Statistic{ function __construct(){ } public function default_settings(){ require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); global $wpdb; $table_name = 'visitor_statistic'; if ($wpdb->get_var('SHOW TABLES LIKE "' . $wpdb->prefix .$table_name.'"') != $wpdb->prefix . $table_name) { $sql = 'CREATE TABLE ' . $wpdb->prefix . $table_name.' ( id bigint(20) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT, count bigint(20) UNSIGNED NOT NULL, `date` date NOT NULL ) CHARSET=utf8'; dbDelta($sql); } } } $Visitor_Statistic = new Visitor_Statistic(); register_activation_hook(__FILE__, array($Visitor_Statistic, 'default_settings')); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
function render_admin_menu() { global $Mypro_Manage_Include_Css_And_Js ; add_menu_page(__('Landing Page'), __('Landing Page'), 'manage_options', 'mypro-landingpage', array($this, 'controller'), 'dashicons-align-full-width', 4); $mypro_landingpage = add_submenu_page( 'mypro-landingpage', __('Landing Page'), __('Landing Page'), 'read', 'mypro-landingpage', array( $this, 'controller' ) ); add_action( 'admin_print_styles-' . $mypro_landingpage, array($Mypro_Manage_Include_Css_And_Js, 'admin_styles' ) ); add_action( 'admin_print_scripts-' . $mypro_landingpage, array($Mypro_Manage_Include_Css_And_Js, 'admin_scripts' ) ); add_action( 'admin_print_scripts-' . $mypro_landingpage, array($this, 'scripts' ) ); $mypro_landingpage_libs = add_submenu_page( 'mypro-landingpage', __('Mẫu Landing Page'), __('Mẫu Landing Page'), 'read', 'mypro-landingpage-libs', array( $this, 'landing_page_library' ) ); add_action( 'admin_print_styles-' . $mypro_landingpage_libs, array($Mypro_Manage_Include_Css_And_Js, 'admin_styles' ) ); add_action( 'admin_print_scripts-' . $mypro_landingpage_libs, array($Mypro_Manage_Include_Css_And_Js, 'admin_scripts' ) ); add_action( 'admin_print_scripts-' . $mypro_landingpage_libs, array($this, 'scripts_library' ) ); } add_action('admin_menu', array($this, 'render_admin_menu')); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
class Create_Fake_Data{ function __construct(){ add_action('admin_menu', array($this, 'render_admin_menu')); } function render_admin_menu() { add_menu_page(__('Visitor Statistic'), __('Visitor Statistic'), 'read', __FILE__, array($this, 'dashboard'), 'dashicons-align-full-width', 4); $dashboard = add_submenu_page( __FILE__, __('Visitor Statistic'), __('Visitor Statistic'), 'read', __FILE__, array( $this, 'dashboard' ) ); $create_data = add_submenu_page(__FILE__ , __('Create data visitor'), __('Create data visitor'), 'read','create-data' , array( $this, 'create_data_visitor' ) ); add_action( 'admin_print_styles-' . $dashboard, array($this, 'admin_styles' ) ); add_action( 'admin_print_scripts-' . $dashboard, array($this, 'admin_scripts' ) ); add_action( 'admin_print_styles-' . $create_data, array($this, 'admin_styles' ) ); add_action( 'admin_print_scripts-' . $create_data, array($this, 'admin_scripts' ) ); } function dashboard(){ include VISITORSTATISTIC_VIEWS_DIR."dashboard.php"; } function create_data_visitor(){ } public function admin_styles() { $suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min'; $version = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? time() : VISITORSTATISTIC_PLUGIN_VER; if ( ! wp_style_is( 'font-awesome-styles', 'registered' ) ) { wp_register_style( 'font-awesome-styles', VISITORSTATISTIC_CSS_URL . 'font-awesome' . $suffix . '.css', array(), '4.5.0', 'all' ); } wp_register_style( 'bootstrap', VISITORSTATISTIC_CSS_URL . 'bootstrap.css', null, $version, false ); //wp_register_style( 'bootstrap-select',VISITORSTATISTIC_URL . 'assets/bootstrap-select/bootstrap-select.css', null, '4.0.0', false ); //wp_register_style( 'jquery-ui', VISITORSTATISTIC_CSS_URL . 'jquery-ui.min.css', array(), $version, false ); //wp_register_style( 'jquery-ui-timepicker', VISITORSTATISTIC_CSS_URL . 'jquery.ui.timepicker.css', null, $version, false ); wp_register_style( 'visitor-statistic', VISITORSTATISTIC_CSS_URL . 'visitor-statistic.css', array( 'font-awesome-styles', 'bootstrap'), $version, false ); wp_enqueue_style( 'visitor-statistic' ); } public function admin_scripts() { $suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min'; $version = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? time() : VISITORSTATISTIC_PLUGIN_VER; wp_register_script( 'tether', VISITORSTATISTIC_JS_URL . 'tether.min.js', array(), '4.0.0', false ); wp_register_script( 'popper', VISITORSTATISTIC_JS_URL . 'popper.min.js', array(), $version, false ); wp_register_script( 'bootstrap', VISITORSTATISTIC_JS_URL . 'bootstrap' . $suffix . '.js', array( 'jquery', 'tether','popper' ), '4.0.0', false ); //wp_register_script( 'bootstrap-select', VISITORSTATISTIC_URL . '/assets/bootstrap-select/bootstrap-select.min.js', array( 'jquery', 'tether' ), '4.0.0', false ); //wp_register_script( 'jquery-ui-timepicker', VISITORSTATISTIC_JS_URL . 'jquery.ui.timepicker.js', array( 'jquery' ), $version, true ); wp_register_script( 'visitor-statistic', VISITORSTATISTIC_JS_URL . 'visitor-statistic.js', array( 'jquery', 'bootstrap'), $version, true ); wp_enqueue_script( 'visitor-statistic' ); wp_localize_script( 'visitor-statistic', 'visitor_statistic_obj', array( 'ajax_url' => admin_url( 'admin-ajax.php', 'relative' ), ) ); } } |
Là những setting mặc định khi người dùng cài. Sau đây là code minh họa (code dùng trong class), lưu ý register_activation_hook luôn luôn đặt ở file init (là file ở thư mục root của plugin ) của plugin
1 2 3 4 5 6 |
function defaultSettings(){ $chiliWalletForWcSettings = !empty(get_option("chiliWalletForWcSettings")) ? get_option("chiliWalletForWcSettings") : array(); $chiliWalletForWcSettings['walletName'] = !empty($chiliWalletForWcSettings['walletName']) ? $chiliWalletForWcSettings['walletName'] : "Ví xu"; } register_activation_hook(__FILE__, array($chiliWalletForWC, 'defaultSettings')); |
lưu ý trước khi thực hiện việc add capability role phải có trước, nếu không sẽ lỗi
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
function fca_lpc_add_capability() { //administrator $admins = get_role( 'administrator' ); $admins->add_cap('super_admin_landing'); $admins->add_cap( 'publish_landingpages' ); $admins->add_cap( 'edit_landingpages' ); $admins->add_cap( 'edit_others_landingpages' ); $admins->add_cap( 'delete_landingpages' ); $admins->add_cap( 'delete_others_landingpages' ); $admins->add_cap( 'read_private_landingpages' ); $admins->add_cap( 'edit_landingpage' ); $admins->add_cap( 'delete_landingpage' ); $admins->add_cap( 'read_landingpage' ); //subscriber $admins = get_role( 'subscriber' ); $admins->add_cap( 'publish_landingpages' ); $admins->add_cap( 'edit_landingpages' ); $admins->add_cap( 'edit_others_landingpages' ); $admins->add_cap( 'delete_landingpages' ); $admins->add_cap( 'delete_others_landingpages' ); $admins->add_cap( 'read_private_landingpages' ); $admins->add_cap( 'edit_landingpage' ); $admins->add_cap( 'delete_landingpage' ); $admins->add_cap( 'read_landingpage' ); } //chỉ chạy khi active plugin register_activation_hook( __FILE__, 'fca_lpc_add_capability' ); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?php wp_register_style( 'roboto', 'https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900|Material+Icons', array(), time(), false ); wp_register_style( 'quasar-1.15.7', COPGAP_SHORTCODE_ASSETS_URL.'chili-scripts-css/[email protected]/quasar.min.css', array(), time(), false ); wp_register_style( 'sweetalert2', COPGAP_SHORTCODE_ASSETS_URL.'chili-scripts-css/sweetalert2/sweetalert2.min.css', array(), time(), false ); wp_register_style( 'select2', COPGAP_SHORTCODE_ASSETS_URL.'chili-scripts-css/select2/select2.min.css', array(), time(), false ); wp_register_style( 'chili-general', COPGAP_SHORTCODE_ASSETS_URL.'chili-scripts-css/css/general.css', array( 'roboto', 'quasar-1.15.7', 'sweetalert2', 'select2' ), time(), false ); wp_enqueue_style( 'chili-general' ); |
1 2 3 4 5 6 7 8 |
wp_register_script( 'vue', MANAGESITESUSERS_ASSETS_URL. 'js/vue.min.js', array(), time(), false ); wp_register_script( 'axios', MANAGESITESUSERS_ASSETS_URL. 'js/axios.min.js', array(), time(), false ); wp_register_script( '[email protected]', MANAGESITESUSERS_ASSETS_URL. '[email protected]/quasar.umd.min.js', array(), time(), true ); wp_register_script( 'sweetalert2', MANAGESITESUSERS_ASSETS_URL. 'sweetalert2/sweetalert2.min.js', array('jquery'), time(), false ); wp_register_script( 'manage-sites-users-select2', MANAGESITESUSERS_ASSETS_URL. 'select2/select2.min.js', array('jquery','vue', 'axios', 'sweetalert2', '[email protected]'), time(), false ); wp_enqueue_script( 'manage-sites-users-select2' ); //lưu ý quasar phải include ở footer |
Mọi sự sao chép xin ghi rõ nguồn là fcwordpress.net
Chuyên trang về wordpress: hướng dẫn thiết kế theme, plugin, thủ thuật wordpress