0

I have three functions to do on my plugin activation ..

Function 1 :-To create a custom-table in wordpress database .

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

Function 2 :-To get all images from post content of a blog.In this function i try to get all images of post content and save them in a session variable '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);
            }   
}

Function 3 :-Get all the images stored in session and send it to server or remote pc using curl function

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

And i have written a code for register_activation_hook in my plugin page as follows

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

It is not working ..Is the method right to do on plugin activation ??

Vaibs_Cool
  • 6,126
  • 5
  • 28
  • 61
  • 1
    Hmm - you're trying to call your 3 functions 3 different ways. Are your functions in a class (not a separate file, but a [PHP class](http://www.php.net/manual/en/language.oop5.basic.php))? If so, all your `register_activation_hook` probably need this form: `register_activation_hook( __FILE__, array( $this, 'get_all_post_images' ) );`. If not, you probably need this form `register_activation_hook( __FILE__,'create_table');`. And relying on `$_SESSION` probably won't work in WordPress - see [this answer](http://stackoverflow.com/questions/1441240/wordpress-session-management) – Hobo Apr 23 '13 at 09:33

1 Answers1

2

Change your includes to:

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

You can create a function to be run when your activate the plugin:

function run_at_activation(){
  create_table();
  get_all_post_images();
  send_image();
}
register_activation_hook( __FILE__, 'run_at_activation' );
RRikesh
  • 14,112
  • 5
  • 49
  • 70
  • You're using a class? If you're not sure what you're doing, I recommend you not to use classes and read the introduction to [writing WordPress plugins](https://codex.wordpress.org/Writing_a_Plugin) – RRikesh Apr 23 '13 at 10:23