I am creating a plugin for my client website. When I activate my plugin then a table is created in phpMyAdmin database by using register_activation_hook()
. Now I want to add a new record in that custom table when wp_post
is updated.
In other words, As I know that when a new post is published then this post data is inserted in wp_post
table but is it possible to use any action()
to insert that same post/page data in my custom table?
I have searched many actions that are called when a post is published or updated but these actions didn’t work for me.
Any type of help will be appreciated.
2
Answers
What you are looking for is in codex as per codex.wordpress.org. The link below will point you to all the function needed for your wordpress post. Go through it and access the post functions that has what you need.
https://codex.wordpress.org/Plugin_API/Action_Reference#Post.2C_Page.2C_Attachment.2C_and_Category_Actions_.28Admin.29
updates:
You can hook up this function to insert into your custom table when publishing a post
now assuming your new table for insertion is nancy_table
you can insert as follows
so finally you will have
You can read up this link to see how to work with wordpress database and tables via secured prepared statement.
db queries wordpress
https://codex.wordpress.org/Class_Reference/wpdb
https://codex.wordpress.org/Class_Reference/wpdb#Protect_Queries_Against_SQL_Injection_Attacks
Each time a post is published, a new post is sent to your custom table. Please try other functions as well..