如何在插件被激活wordpress php时调用/运行功能?

时间:2013-04-23 06:59:14

标签: php wordpress-plugin wordpress

我的插件激活有三个功能..

功能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: - 从博客的帖子内容中获取所有图像。在此功能中,我尝试获取帖子内容的所有图像并将其保存在会话变量'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: - 获取会话中存储的所有图像,并使用curl函数将其发送到服务器或远程PC

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

它无法正常工作..这是在插件激活方面做的权利吗?

1 个答案:

答案 0 :(得分: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' );