skip to Main Content

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


  1. Chosen as BEST ANSWER
    function insert_data_into_page_setting(){
        global $wpdb;
        echo "<script> alert('Row Inserted')</script>";
        $lastID = $wpdb->insert_id;
        $newRes = $wpdb->get_results("SELECT * from wp_posts WHERE ID=$lastID", ARRAY_A);
        foreach ($newRes as $values) {
            $wpdb->insert(
                'wp_page_settings',
                array('ID' => $lastID, 'post_title' => $values['post_title']),
                array('%d', '%s'));
        }
    }
    add_action('publish_post', 'insert_data_into_page_setting');
    

  2. 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

    function post_published_notification( $ID, $post ) {
        $author = $post->post_author; /* Post author ID. */
        $name = get_the_author_meta( 'display_name', $author );
        $email = get_the_author_meta( 'user_email', $author );
        $title = $post->post_title;
        $permalink = get_permalink( $ID );
        $edit = get_edit_post_link( $ID, '' );
        $to[] = sprintf( '%s <%s>', $name, $email );
        $subject = sprintf( 'Published: %s', $title );
        $message = sprintf ('Congratulations, %s! Your article "%s" has been published.' . "nn", $name, $title );
        $message .= sprintf( 'View: %s', $permalink );
        $headers[] = '';
        wp_mail( $to, $subject, $message, $headers );enter code here
    }
    add_action( 'publish_post', 'post_published_notification', 10, 2 );
    

    now assuming your new table for insertion is nancy_table

    you can insert as follows

    // initialize database connections globals
    global $wpdb;
    $name = "nancy";
    $post_new = "my post is here";
    
    $wpdb->query( $wpdb->prepare( 
        "
            INSERT INTO nancy_table
            ( post_id, name, post_new )
            VALUES ( %d, %s, %s )
        ", 
            array(
            10, 
            $name, 
            $post_new
        ) 
    ) );
    

    so finally you will have

        function post_published_notification( $ID, $post ) {
            $author = $post->post_author; /* Post author ID. */
            $name = get_the_author_meta( 'display_name', $author );
            $email = get_the_author_meta( 'user_email', $author );
            $title = $post->post_title;
            $permalink = get_permalink( $ID );
            $edit = get_edit_post_link( $ID, '' );
            $to[] = sprintf( '%s <%s>', $name, $email );
            $subject = sprintf( 'Published: %s', $title );
    
    
    // your new post
    
    global $wpdb;
    $name = "nancy";
    $post_new = "my post is here";
    
    $wpdb->query( $wpdb->prepare( 
        "
            INSERT INTO nancy_table
            ( post_id, name, post_new )
            VALUES ( %d, %s, %s )
        ", 
            array(
            10, 
            $name, 
            $post_new
        ) 
    ) );
    
    
    
            $message = sprintf ('Congratulations, %s! Your article "%s" has been published.' . "nn", $name, $title );
            $message .= sprintf( 'View: %s', $permalink );
            $headers[] = '';
            wp_mail( $to, $subject, $message, $headers );enter code here
        }
        add_action( 'publish_post', 'post_published_notification', 10, 2 );
    

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

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