You shouldn’t be calling get_the_ID()
outside of a post loop, and the post ID is handed to you using the filter as a function argument:
function insert_new_table( $post_id ){
So use $post_id
Added notes
- Don’t use
mysql_connect
etc that extension was deprecated and is no longer included by default in newer versions of PHP. This code will fail on PHP 5.5+, if you must abandon the WP APIs use PDO or Mysqli - You don’t have to use the global wpdb object, you can create your own:
$mydb = new wpdb( $dbuser, $dbpassword, $dbname, $dbhost );
- You’re creating a table for each post in a remote database with a name and an ID column, this is wasteful and bad table design. Would it not make more sense to have a single table containing all names with a post ID in it? Why must it be in a separate database? What happens if your IDs change? Why not use custom post types/post meta/custom taxonomies? You realise there’ll be a significant latency cost if your database is located elsewhere? Your data will not survive a site move or import/export
- Pick an indenting method and stick to it, your editor should be capable of automatically fixing and indenting without any effort from you, if it doesn’t you should find a new one. I would recommend sublimetext or PHPStorm but others exist. WordPress standards use tabbed indenting taking up 4 spaces on the screen. PSR standards use 4 space characters.
- You’re not checking for drafts and revisions