How can I add a page’s ID to its permalink using WP_Rewrite?

You don’t need to use any code for this. You just need to:

  1. Go to your /wp-admin/options-permalink.php page
  2. Select the ‘Custom Structure’ and enter: /%postname%/%post_id%/
  3. Then press the ‘Save’ button