Extracting the post_id via the wp_insert_post action (external db query)

i'm writing a little action plugin. everytime a new post is created, a new table should be created in an external db. The name of the table sholud reflect the post_id of the newly created post, but i can0t figure how to pass it.

Here's my code so far (for php purists, forgive me for using old mysql_connect (but adding the flag TRUE to the new_link parameter, i don't have to bother with wpdb globals etc)

function insert_new_table($post_id){
    $usernamel = "secret";
    $passwordl    = "secret";
    $hostl    = "localhost";
    $databasel    ="secret";
    $connect = mysql_connect($hostl, $usernamel, $passwordl, TRUE) or die("Error");
    mysql_select_db($databasel) or die("Error, Cannot locate the database!");
    $table_name = 'tablename4';  //with this variable works
    $postidl = get_the_ID();     // i'm outside the loop .. so what?
    $sql = "CREATE TABLE $postidl (
      id int(11) NOT NULL AUTO_INCREMENT,
      name varchar(255) DEFAULT NULL,
      UNIQUE KEY id (id)
      );";
    $query = mysql_query($sql);
    return $post_id;    
}
add_action( 'wp_insert_post', 'insert_new_table' );

everything is going smooth using the var $table_name in my $sql query; but i can't pass the post_id of the post that is currently being saved (don't know how to get it!)

Any hint?

Topic get-the-id actions wpdb database Wordpress

Category Web


The hook is called with the post ID as the first (and only) argument, you don't need to fetch/retrieve it, it's being sent to your function:

function insert_new_table( $post_id ) {
  //                 here  ^^^^^^^^
  # ... your code ...
  $sql = "CREATE TABLE {$post_id} (
    id int(11) NOT NULL AUTO_INCREMENT,
    name varchar(255) DEFAULT NULL,
    UNIQUE KEY id (id)
  );";
  # ... more of your code ...
}

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

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.