Forums

[plugin development]How to modify post content after post is saved? (2 posts)

  1. mtymek
    Member
    Posted 1 year ago #

    Hello!

    I'm developing WP plugin that needs to modify post content AFTER post is saved (so I'm sure it has and ID and permalink). I want it to fire only when users presses "publish" or "update" button (not for autosave).
    My idea was to hook to "save post" action, change what I need and use $wpdb->update() to modify row in wp_post table. I came up with something similar to following code:

    function myplugin_parse_post( $postId )
    {
        global $post, $wpdb;
    
        $post->post_content = $post->post_content . '<div>my addition</div>';
    
        $wpdb->update( $wpdb->posts, array( 'post_content' => $post->post_content ), array( 'ID' => $postId ) );
    }
    add_action( 'save_post', 'myplugin_parse_post' );

    After trying out my plugin I realized that this doesn't work. I checked revisions and saw that they were modified, however changes weren't visible in editor.
    I tried a few different approaches (like using wp_is_post_revision()) but none of them worked.
    Can you help me with this? WP plugin development is new topic for me and it is a little difficult to understand how exactly saving post works.
    Thanks!

  2. mtymek
    Member
    Posted 1 year ago #

    Sorry, I published it here and realized that I should have done it in "Hacks". Can somebody move my post?

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags