簡體   English   中英

插件被激活時如何調用/運行功能WordPress的PHP?

[英]How to call/run function while plugin is being activated 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:-從博客的帖子內容中獲取所有圖像。在此功能中,我嘗試獲取帖子內容的所有圖像並將其保存在會話變量'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'); 

它不起作用..插件激活方法正確嗎?

將包含內容更改為:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM