У меня есть три функции для активации моей плагины.Как вызвать/запустить функцию во время активации плагина wordpress php?
Функция 1: -Для создания пользовательской таблицы в базе данных wordpress.
function create_table(){
//Get the table name with the WP database prefix
global $wpdb;
echo "create_table";
$table_name = $wpdb->prefix . "custom";
$installed_ver = get_option("db_table_custom_version");
//Check if the table already exists and if the table is up to date, if not create it
if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name
|| $installed_ver != $db_table_custom_version) {
$sql = "CREATE TABLE " . $table_name . " (
id mediumint(9) NOT NULL AUTO_INCREMENT,
date bigint(11) DEFAULT '0' NOT NULL,
site tinytext NOT NULL,
description text NOT NULL,
max_depth mediumint(9) NOT NULL,
time mediumint(9) NOT NULL,
UNIQUE KEY id (id)
);";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
update_option("db_table_custom_version", $db_table_custom_version);
}
//Add database table versions to options
add_option("db_table_custom_version", $db_table_custom_version);
}
Функция 2: -Чтобы получить все изображения из почтового содержания blog.In этой функции я стараюсь, чтобы получить все изображения после содержания и сохранения их в переменной сеанса «arrayImg»
function get_all_post_images(){
if(is_single() || is_page() || is_home()){
global $post;
global $wpdb;
$query_images_args = array(
'post_type' => 'attachment' , 'post_mime_type' =>'image','post_status' => 'published', 'posts_per_page' => -1,'numberposts' => 1
);
$query_images = new WP_Query($query_images_args);
$images = array();
foreach ($query_images->posts as $image) {
$images[]= wp_get_attachment_url($image->ID);
}
$abc=($images);
$count = count($images);
for ($i = 0; $i < $count; $i++)
{
$final[]= $images[$i];
}
$_SESSION['arrayImg']=$abc;
$noofpics= count($image);
}
}
Функция 3: -Получить все изображения, хранящиеся в сессии, и отправить его на сервер или удаленный компьютер с помощью завивки функции
global $ch;
function send_image(){
if(!session_id())
session_start();
$abc = $_SESSION['arrayImg'];
global $ch;
$post_data = array('data' => serialize($abc));
$url="http://serverpath";
$ch = curl_init();
set_time_limit (1000);
curl_setopt($ch, CURLOPT_POST,$abc);
curl_setopt($ch, CURLOPT_POSTFIELDS, ($post_data));
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
if(curl_exec($ch) === false) {
echo $ch;
die("Curl failed: " . curl_error($ch));
} else {
$output= curl_exec($ch);
}
$_SESSION['match_response']=$output;
curl_close ($ch);
}
и я написал код для register_activation_hook в моей странице плагина следующим образом
include_once dirname(__FILE__).'\database.php'; //Function 1 is in this file
include_once dirname(__FILE__).'\ajax.php'; //Function 3 is in this file
register_activation_hook(__FILE__, array('YourAdditionalClass','on_activate_function'));
register_activation_hook(__FILE__, array($this, 'get_all_post_images')); //function is in this file only
register_activation_hook(__FILE__, array('YourAdditionalClass', 'send_image'));
register_activation_hook(__FILE__,'create_table');
Не работает. Является ли метод правильным для активации плагина?
Хм - вы пытаетесь вызвать ваши 3 функции 3 различными способами , Являются ли ваши функции в классе (а не отдельным файлом, но [классом PHP] (http://www.php.net/manual/en/language.oop5.basic.php))? Если это так, все ваши 'register_activation_hook', вероятно, нуждаются в такой форме:' register_activation_hook (__FILE__, array ($ this, 'get_all_post_images')); '. Если нет, вам, вероятно, понадобится эта форма 'register_activation_hook (__FILE __, 'create_table');'. И полагаться на '$ _SESSION', вероятно, не будет работать в WordPress - см. [Этот ответ] (http://stackoverflow.com/questions/1441240/wordpress-session-management) – Hobo