2013-04-23 71 views
0

我有三个功能可以在我的插件上激活..如何在插件激活时调用/运行函数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的帖子内容全部图像此功能我试图让帖子内容的所有图像,并将它们保存在session变量“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:-GET存储在会话中的所有图像,并使用curl功能

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); 
} 

其发送到服务器或远程PC和我有我的插件页书面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'); 

这是行不通的..是插件激活的方法吗?

+1

嗯 - 你想打电话给你的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 2013-04-23 09:33:43

回答

0

更改包括了:

include_once('database.php'); 
include_once('ajax.php'); 

您可以创建一个函数来运行,当你激活插件:

function run_at_activation(){ 
    create_table(); 
    get_all_post_images(); 
    send_image(); 
} 
register_activation_hook(__FILE__, 'run_at_activation'); 
+0

这是写在_Construct或任何地方我的插件主文件.. – 2013-04-23 10:06:21

+0

你正在使用一个类?如果你不确定你在做什么,我建议你不要使用类并阅读[编写WordPress插件]的介绍(https://codex.wordpress.org/Writing_a_Plugin) – RRikesh 2013-04-23 10:23:24

+0

是的,我正在使用一个类.. – 2013-04-23 10:40:21

相关问题