WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] How do I intercept Post/Page and edit before it is viewed?

[Resolved] How do I intercept Post/Page and edit before it is viewed?

  • Hey

    I’m looking at using “the_post” action hook possibly, to intercept all pages/posts before they are viewed. The function called may change the content for a single post and so I would like to do this before the post is viewed.

    I see that “the_post()” is used within the loop but what I’m unsure about is how to get the actual post values into my function, make any required changes to the content then allow the post to be viewed with the changes.

    Is this even possible? If not I would be happy to at least do the content edit using my function and let the user see the old version of the post.

    Thanks for any help
    Ryan

Viewing 5 replies - 1 through 5 (of 5 total)
  • Depends on what you want to do/change. You can write your own custom loop, you can pass modifying query parameters to the loop, you can filter just the content of the post.

    You’d probably need to be more specific about what exactly you want to do in order to get more specific help.

    I’m coding a plugin so it must be an action hook/filter much like this…

    // intercept all posts and review content, do any editing to data and current values
    function eci_updatethepost( $posts )
    {
    global $wpdb;

    $allposts = array();

    //loop through all the post objects
    foreach( $posts as $post )
    {
    //var_dump($post);
    //echo $post->ID.’Post ID’;

    // get post meta – custom fields
    $postupdated = get_post_meta($post->ID, ‘eci_lastupdate’, true);
    $recordid = get_post_meta($post->ID, ‘eci_recordid’, true);
    $tablename = get_post_meta($post->ID, ‘eci_tablename’, true);

    // get record date from database table
    $table_name = $wpdb->prefix . $tablename;// build full tablename
    $query = “SELECT updated FROM " . $table_name . " WHERE postid = ". $post->ID ."“;
    $recordupdated = $wpdb->get_var(‘query’);

    //echo $postupdated;
    //echo $recordupdated;

    }

    return $
    }

    Problem I have is that using it results in no posts on the page and I think it may be because I need to return the posts that go into this function.

    Anyway thats how far I got since yesterday but still not perfect.

    Thanks

    I can’t do something like this, its not just the content I need to update its custom fields/meta also.

    http://www.fahlstad.se/2006/08/using-wordpress-filters-a-tutorial/

    Whatever I do I’ll need the post ID so I think I need the entire post object.

    I done it, I think. I must be tired, I know well that if you edit an array it’s no different and can just return it, I was thinking I had to make my own array and re-build it after looking at someones example.

    function eci_updatethepost( $posts )
    {
    global $wpdb;

    //loop through all the post objects
    foreach( $posts as $post )
    {
    // get post meta – custom fields
    $postupdated = get_post_meta($post->ID, ‘eci_lastupdate’, true);
    $recordid = get_post_meta($post->ID, ‘eci_recordid’, true);
    $tablename = get_post_meta($post->ID, ‘eci_tablename’, true);

    // get record date from database table
    $table_name = $wpdb->prefix . $tablename;// build full tablename
    $query = “SELECT updated FROM " . $table_name . " WHERE postid = ". $post->ID ."“;
    $recordupdated = $wpdb->get_var(‘query’);
    }

    return $posts;
    }

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘[Resolved] How do I intercept Post/Page and edit before it is viewed?’ is closed to new replies.