skip to Main Content

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.

user_id (post_author)
operation_type (add/update/delete etc),
table_name (which table add/update/delete),
row_id (post r page own id),
operation_at (that time when admin add/update/delete),
created_at (operations table current time),
updated_at (operations current time),

how can implement this code?

Thanks

2

Answers


  1. 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/

    Login or Signup to reply.
  2. /* 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/

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search