Home > Net >  i created a custom table for wordpress website that when i will create or update or delete post or p
i created a custom table for wordpress website that when i will create or update or delete post or p

Time:11-01

I want to store some information with my new custom table in Wordpress website, whenever admin create update or delete any post or page that time some information store my custom table. Below my custom table operations

Operations: id,User_id,operation_type,table_name,row_id,operation_at,created_at,updated_at.

Thanks

CodePudding user response:

You can use the following hooks:

  1. For new post / page creation: wp_insert_post
  2. For post update: post_updated
  3. For post delete: before_delete_post or delete_post

I don't know what do you want to do exactly, but you can insert data with https://developer.wordpress.org/reference/classes/wpdb/insert/

CodePudding user response:

/* You can use these 3 action hooks to have a functionality after update, insert and delete a post respectively. */

add_action('post_updated', 'after_update_post', 10, 3 );
add_action('wp_insert_post', 'after_insert_post', 10, 3 );
add_action('wp_trash_post', 'after_delete_post');

function after_update_post($post_ID, $post_after, $post_before){
    global $wpdb;
    $date= current_time('mysql');
    $table = $wpdb->prefix.'your_table_name';
    $data = array('date_modified' => $date, 'timestamp' => time(), 'operation_type' => 'update', 'post_id' => $post_ID );
    $format = array( '%s', '%s', '%s', '%d');
    $wpdb->insert($table,$data,$format);
}

/* This function is only for update a post. You have all the hooks for each action just call a function after any specific action */

I hope it will solve your problem.

If you have any problem regarding inserting into a table you might need to look into this. https://developer.wordpress.org/reference/classes/wpdb/insert/

  • Related