Php wordpress插件-网络站点数据库
您好,谢谢您的光临并抽出时间回答我的问题。 首先,我从未为Wordpress编写过插件,这是我的第一个noob 我制作了主题,使用了第三方插件,并对它们进行了编辑。然而,这是一种全新的球类运动 我希望能够做到以下一点——以最好的为准Php wordpress插件-网络站点数据库,php,mysql,wordpress,Php,Mysql,Wordpress,您好,谢谢您的光临并抽出时间回答我的问题。 首先,我从未为Wordpress编写过插件,这是我的第一个noob 我制作了主题,使用了第三方插件,并对它们进行了编辑。然而,这是一种全新的球类运动 我希望能够做到以下一点——以最好的为准 1表-所有网络站点都有自己的数据,但都存储在一个表中 或者我应该把它分成他们自己的表格,如果是这样的话,怎么做 这是代码,但不是我想要的。我认为这是为了简单,而不是火箭科学-请一个简单的插件,概述了hello world在插件格式,将在单一和网络网站与数据库创建工作
<?php
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
/**
* Plugin Name: Book Online
* Plugin URI: http://harrower.xyz
* Description: This plugin allows your customers to schedual a time to meet with you and pay any fee that you require.
* Version: 1.0.0
* Author: Harrower.xyz
* Author URI: http://harrower.xyz
* License: GPL2
*/
/**
* Start the plugin only if in Admin side and if site is Multisite
*/
if( is_admin() && is_multisite() )
{
add_action(
'plugins_loaded',
array ( book_online_multisite::get_instance(), 'plugin_setup' )
);
}
class book_online_multisite
{
protected static $instance = NULL;
public $blogs = array();
public $plugin_url = '';
public $plugin_path = '';
public static function get_instance()
{
NULL === self::$instance and self::$instance = new self;
return self::$instance;
}
/**
* Plugin URL and Path work as normal
*/
public function plugin_setup()
{
$this->plugin_url = plugins_url( '/', __FILE__ );
$this->plugin_path = plugin_dir_path( __FILE__ );
add_action(
'book-online-dashboard.php',
array( $this, 'load_blogs' )
);
add_action( 'admin_menu', 'register_my_custom_menu_page' );
}
// this is the menu area
function register_my_custom_menu_page() {
add_menu_page( 'Book Online', 'Book Online <span class="update-plugins count-1"><span class="plugin-count">1</span></span>', 'manage_options', 'book-online/book-online-admin.php', 'book-online-admin-menu', 'dashicons-clock', 6 );
}
/// function to create the DB / Options / Defaults
function book_online_options_install() {
global $wpdb;
global $bonlinedb;
$book_online_caldb = $wpdb->prefix . "booking_online_calendar";
$book_online_customersdb = $wpdb->prefix . "booking_online_customers";
// create the ECPT metabox database table
if($wpdb->get_var("show tables like '$book_online_customersdb'") != $book_online_customersdb)
{
//$bonlinedb = $wpdb->prefix . 'bookonline';
$sql = "CREATE TABLE `$book_online_customersdb` (
`id` INT() NOT NULL AUTO_INCREMENT,
`booking_date`DATE NOT NULL,
UNIQUE KEY id (id)) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
// create the ECPT metabox database table
if($wpdb->get_var("show tables like '$book_online_caldb'") != $book_online_caldb)
{
$sql = "CREATE TABLE `$book_online_caldb` (
`id` INT() NOT NULL AUTO_INCREMENT,
`bid` INT() NOT NULL,
`booking_time` TIME NOT NULL,
`status` INT() NOT NULL DEFAULT '0';
UNIQUE KEY id (id))ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
// this is the menu area
function register_my_custom_menu_page() {
add_menu_page( 'Book Online', 'Book Online <span class="update-plugins count-1"><span class="plugin-count">1</span></span>', 'manage_options', 'book-online/book-online-admin.php', 'book-online-admin-menu', 'dashicons-clock', 6 );
add_submenu_page( 'book-online-admin-menu', 'My Custom Page', 'My Custom Page',
'manage_options', 'my-top-level-slug');
add_submenu_page( 'ook-online-admin-menu', 'My Custom Submenu Page', 'My Custom Submenu Page',
'manage_options', 'my-secondary-slug');
}
add_action( 'admin_menu', 'register_my_custom_menu_page' );
/*
public function __construct() {}
public function load_blogs()
{
/**
* Using "is_network" property from $current_screen global variable.
* Run only in /wp-admin/network/plugins.php
*/
global $current_screen;
if( !$current_screen->is_network )
return;
/*
*A couple of Multisite-only filter hooks and a regular one.
*/
add_action(
'network_admin_plugin_action_links',
array( $this, 'list_plugins' ),
10, 4
);
* add_filter(
* 'views_plugins-network', // 'views_{$current_screen->id}'
* array( $this, 'inactive_views' ),
* 10, 1
*);
*add_action(
* 'admin_print_scripts',
* array( $this, 'enqueue')
* );
*/
/**
* This query is quite frequent to retrieve all blog IDs.
*/
global $wpdb;
$this->blogs = $wpdb->get_results(
" SELECT blog_id, domain
FROM {$wpdb->blogs}
WHERE site_id = '{$wpdb->siteid}'
AND spam = '0'
AND deleted = '0'
AND archived = '0' "
);
}
/**
* Enqueue script and style normally.
*/
public function enqueue()
{
wp_enqueue_script(
'ndbae-js',
$this->plugin_url . 'core/ndbae.js',
array(),
false,
true
);
wp_enqueue_style(
'main',
$this->plugin_url . 'core/main.css'
);
}
/**
* Check if plugin is active in any blog
* Using Multisite function get_blog_option
*/
private function get_network_plugins_active( $plug )
{
$active_in_blogs = array();
foreach( $this->blogs as $blog )
{
$the_plugs = get_blog_option( $blog['blog_id'], 'active_plugins' );
foreach( $the_plugs as $value )
{
if( $value == $plug )
$active_in_blogs[] = $blog['domain'];
}
}
return $active_in_blogs;
}
}
class book_online_single_site{
// function to create the DB / Options / Defaults
function book_online_options_install() {
global $wpdb;
global $bonlinedb;
$book_online_caldb = $wpdb->prefix . "booking_online_calendar";
$book_online_customersdb = $wpdb->prefix . "booking_online_customers";
// create the ECPT metabox database table
if($wpdb->get_var("show tables like '$book_online_customersdb'") != $book_online_customersdb)
{
//$bonlinedb = $wpdb->prefix . 'bookonline';
$sql = "CREATE TABLE `$book_online_customersdb` (
`id` INT() NOT NULL AUTO_INCREMENT,
`booking_date`DATE NOT NULL,
UNIQUE KEY id (id)) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
// create the ECPT metabox database table
if($wpdb->get_var("show tables like '$book_online_caldb'") != $book_online_caldb)
{
$sql = "CREATE TABLE `$book_online_caldb` (
`id` INT() NOT NULL AUTO_INCREMENT,
`bid` INT() NOT NULL,
`booking_time` TIME NOT NULL,
`status` INT() NOT NULL DEFAULT '0';
UNIQUE KEY id (id))ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
// this is the menu area
function register_my_custom_menu_page() {
add_menu_page( 'Book Online', 'Book Online <span class="update-plugins count-1"><span class="plugin-count">1</span></span>', 'manage_options', 'book-online/book-online-admin.php', 'book-online-admin-menu', 'dashicons-clock', 6 );
add_submenu_page( 'book-online-admin-menu', 'My Custom Page', 'My Custom Page',
'manage_options', 'my-top-level-slug');
add_submenu_page( 'ook-online-admin-menu', 'My Custom Submenu Page', 'My Custom Submenu Page',
'manage_options', 'my-secondary-slug');
}
add_action( 'admin_menu', 'register_my_custom_menu_page' );
add_action( 'wp_head', 'book_online' );
function book_online() {
echo 'I am in the head section';
}
add_shortcode( 'bookonline', 'book_online_live' );
function book_online_live() {
return 'this is a test';
}
// run the install scripts upon plugin activation
register_activation_hook(__FILE__,'book_online_options_install');
}
}
?>
你能精确地回答你的问题吗?是否要为来自同一网络的多个Wordpress站点创建一个表?如果在安装时使用插件代码创建表,请使用$wpdb->prefix.“tablename”,这样,当某个站点的插件处于活动状态时,该表将自动为该站点创建。在这些表中插入数据时,请使用$wp->前缀。这应该符合你的目的。@AbhisekMalakar这也能解决如何为每个网络站点创建每个表的问题吗?你能精确地回答你的问题吗?是否要为来自同一网络的多个Wordpress站点创建一个表?如果在安装时使用插件代码创建表,请使用$wpdb->prefix.“tablename”,这样,当某个站点的插件处于活动状态时,该表将自动为该站点创建。在这些表中插入数据时,请使用$wp->前缀。这应该符合你的目的。@AbhisekMalakar这也能解决如何为每个网络站点创建每个表的问题吗?
<?php
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
/**
* Plugin Name: Book Online
* Plugin URI: http://harrower.xyz
* Description: This plugin allows your customers to schedual a time to meet with you and pay any fee that you require.
* Version: 1.0.0
* Author: Harrower.xyz
* Author URI: http://harrower.xyz
* License: GPL2
*/
/**
* Start the plugin only if in Admin side and if site is Multisite
*/
if( is_admin() && is_multisite() )
{
add_action(
'plugins_loaded',
array ( book_online_multisite::get_instance(), 'plugin_setup' )
);
}
class book_online_multisite
{
protected static $instance = NULL;
public $blogs = array();
public $plugin_url = '';
public $plugin_path = '';
public static function get_instance()
{
NULL === self::$instance and self::$instance = new self;
return self::$instance;
}
/**
* Plugin URL and Path work as normal
*/
public function plugin_setup()
{
$this->plugin_url = plugins_url( '/', __FILE__ );
$this->plugin_path = plugin_dir_path( __FILE__ );
add_action(
'book-online-dashboard.php',
array( $this, 'load_blogs' )
);
add_action( 'admin_menu', 'register_my_custom_menu_page' );
}
// this is the menu area
function register_my_custom_menu_page() {
add_menu_page( 'Book Online', 'Book Online <span class="update-plugins count-1"><span class="plugin-count">1</span></span>', 'manage_options', 'book-online/book-online-admin.php', 'book-online-admin-menu', 'dashicons-clock', 6 );
}
/// function to create the DB / Options / Defaults
function book_online_options_install() {
global $wpdb;
global $bonlinedb;
$book_online_caldb = $wpdb->prefix . "booking_online_calendar";
$book_online_customersdb = $wpdb->prefix . "booking_online_customers";
// create the ECPT metabox database table
if($wpdb->get_var("show tables like '$book_online_customersdb'") != $book_online_customersdb)
{
//$bonlinedb = $wpdb->prefix . 'bookonline';
$sql = "CREATE TABLE `$book_online_customersdb` (
`id` INT() NOT NULL AUTO_INCREMENT,
`booking_date`DATE NOT NULL,
UNIQUE KEY id (id)) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
// create the ECPT metabox database table
if($wpdb->get_var("show tables like '$book_online_caldb'") != $book_online_caldb)
{
$sql = "CREATE TABLE `$book_online_caldb` (
`id` INT() NOT NULL AUTO_INCREMENT,
`bid` INT() NOT NULL,
`booking_time` TIME NOT NULL,
`status` INT() NOT NULL DEFAULT '0';
UNIQUE KEY id (id))ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
// this is the menu area
function register_my_custom_menu_page() {
add_menu_page( 'Book Online', 'Book Online <span class="update-plugins count-1"><span class="plugin-count">1</span></span>', 'manage_options', 'book-online/book-online-admin.php', 'book-online-admin-menu', 'dashicons-clock', 6 );
add_submenu_page( 'book-online-admin-menu', 'My Custom Page', 'My Custom Page',
'manage_options', 'my-top-level-slug');
add_submenu_page( 'ook-online-admin-menu', 'My Custom Submenu Page', 'My Custom Submenu Page',
'manage_options', 'my-secondary-slug');
}
add_action( 'admin_menu', 'register_my_custom_menu_page' );
/*
public function __construct() {}
public function load_blogs()
{
/**
* Using "is_network" property from $current_screen global variable.
* Run only in /wp-admin/network/plugins.php
*/
global $current_screen;
if( !$current_screen->is_network )
return;
/*
*A couple of Multisite-only filter hooks and a regular one.
*/
add_action(
'network_admin_plugin_action_links',
array( $this, 'list_plugins' ),
10, 4
);
* add_filter(
* 'views_plugins-network', // 'views_{$current_screen->id}'
* array( $this, 'inactive_views' ),
* 10, 1
*);
*add_action(
* 'admin_print_scripts',
* array( $this, 'enqueue')
* );
*/
/**
* This query is quite frequent to retrieve all blog IDs.
*/
global $wpdb;
$this->blogs = $wpdb->get_results(
" SELECT blog_id, domain
FROM {$wpdb->blogs}
WHERE site_id = '{$wpdb->siteid}'
AND spam = '0'
AND deleted = '0'
AND archived = '0' "
);
}
/**
* Enqueue script and style normally.
*/
public function enqueue()
{
wp_enqueue_script(
'ndbae-js',
$this->plugin_url . 'core/ndbae.js',
array(),
false,
true
);
wp_enqueue_style(
'main',
$this->plugin_url . 'core/main.css'
);
}
/**
* Check if plugin is active in any blog
* Using Multisite function get_blog_option
*/
private function get_network_plugins_active( $plug )
{
$active_in_blogs = array();
foreach( $this->blogs as $blog )
{
$the_plugs = get_blog_option( $blog['blog_id'], 'active_plugins' );
foreach( $the_plugs as $value )
{
if( $value == $plug )
$active_in_blogs[] = $blog['domain'];
}
}
return $active_in_blogs;
}
}
class book_online_single_site{
// function to create the DB / Options / Defaults
function book_online_options_install() {
global $wpdb;
global $bonlinedb;
$book_online_caldb = $wpdb->prefix . "booking_online_calendar";
$book_online_customersdb = $wpdb->prefix . "booking_online_customers";
// create the ECPT metabox database table
if($wpdb->get_var("show tables like '$book_online_customersdb'") != $book_online_customersdb)
{
//$bonlinedb = $wpdb->prefix . 'bookonline';
$sql = "CREATE TABLE `$book_online_customersdb` (
`id` INT() NOT NULL AUTO_INCREMENT,
`booking_date`DATE NOT NULL,
UNIQUE KEY id (id)) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
// create the ECPT metabox database table
if($wpdb->get_var("show tables like '$book_online_caldb'") != $book_online_caldb)
{
$sql = "CREATE TABLE `$book_online_caldb` (
`id` INT() NOT NULL AUTO_INCREMENT,
`bid` INT() NOT NULL,
`booking_time` TIME NOT NULL,
`status` INT() NOT NULL DEFAULT '0';
UNIQUE KEY id (id))ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
// this is the menu area
function register_my_custom_menu_page() {
add_menu_page( 'Book Online', 'Book Online <span class="update-plugins count-1"><span class="plugin-count">1</span></span>', 'manage_options', 'book-online/book-online-admin.php', 'book-online-admin-menu', 'dashicons-clock', 6 );
add_submenu_page( 'book-online-admin-menu', 'My Custom Page', 'My Custom Page',
'manage_options', 'my-top-level-slug');
add_submenu_page( 'ook-online-admin-menu', 'My Custom Submenu Page', 'My Custom Submenu Page',
'manage_options', 'my-secondary-slug');
}
add_action( 'admin_menu', 'register_my_custom_menu_page' );
add_action( 'wp_head', 'book_online' );
function book_online() {
echo 'I am in the head section';
}
add_shortcode( 'bookonline', 'book_online_live' );
function book_online_live() {
return 'this is a test';
}
// run the install scripts upon plugin activation
register_activation_hook(__FILE__,'book_online_options_install');
}
}
?>