Title: Edit Post Programaticly in post.php
Last modified: August 20, 2016

---

# Edit Post Programaticly in post.php

 *  [TIves](https://wordpress.org/support/users/tives/)
 * (@tives)
 * [13 years, 3 months ago](https://wordpress.org/support/topic/edit-post-programaticly-in-postphp/)
 * Hello all!
 * I’m a bit over my head and I am hoping someone can throw me a life preserver 
   🙂
 * Via a plugin I have added a simple form to post.php (the post editor page) as
   follows:
 *     ```
       echo "<br><br><div class='alignright' style='padding:5px;'><form method='post'><input type='hidden' name='post_id' value='" . $post_id . "'><input type='text' name='search_terms' size='200'><input type='submit' value='Generate Search Results'></form></div><p><br><br>";
       ```
   
 * When the page reloads, I look at the variable in the $_POST and then execute 
   a function as follows:
 *     ```
       if(isset($_POST['search_terms'])){
           do_google_seach();
       }
       ```
   
 * In the function, I modify the post, check everything via a file_put_contents 
   statement (everything is perfect) and then try to write back to the post. This
   is where I fail! Here’s the code:
 *     ```
       function do_google_seach(){
   
            $search_term = $_POST["search_terms"];  //wpseo_get_value( 'google_search' ); // get the requested seach term
            $post_id = (int) $_POST["post_id"];
   
            if (isset($search_term))
                    $results=trueGS($search_term,10,"com",0,"",dirname(__FILE__).'/cache',600); // execute the search
   
            $post = get_post($post_id, ARRAY_A);
            $post_title = $post['post_title'];
   
            $post_content = "<strong>".$post_title."</strong><br>";
            $post_content .= $post['post_content'];
            $post_content .= "<br><h5>".$post_title."</h5><br>";
   
            foreach($results['results'] as $result)
            {
                    $post_content .= "<p><a href='".$result["url"]."' target='_blank'>".$result["title"]."</a></p>";
            }
   
            $post['ID'] = (int)$post_id;
            $post['post_content'] = $post_content;
            wp_update_post($post);
            file_put_contents("/blog/wp-content/plugins/wordpress-seo/test-        output.txt", print_r($post, true)."\n", FILE_APPEND);
       //everything is perfect at this point 
   
            wp_update_post($post);
            save_post;
            $_SERVER['PHP_SELF'];
         }
       ```
   
 * Everything here works just as I want it to, until I hit wp_update_post. What 
   fails to happen is that the post, as shown in the editing textarea does not update
   despite numerous efforts to reload the page.
 * Is the file locked for editing?
    Is my approach totally wrong? How would you 
   all programaticly alter ‘post_content’ in post.php?
 * Thanks so much!
 * Trip

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Thread Starter [TIves](https://wordpress.org/support/users/tives/)
 * (@tives)
 * [13 years, 3 months ago](https://wordpress.org/support/topic/edit-post-programaticly-in-postphp/#post-3506279)
 * Sorry for the double post, I had initially posted in “Plugins and Hacks” and 
   then tried to move. Seems like someone else moved it as I did too. I don’t know
   how to delete the blank one 🙂 Sorry!
 *  [Patrick Johanneson](https://wordpress.org/support/users/pjohanneson/)
 * (@pjohanneson)
 * [13 years, 3 months ago](https://wordpress.org/support/topic/edit-post-programaticly-in-postphp/#post-3506364)
 * If I’m reading the [wp_update_post()](http://codex.wordpress.org/Function_Reference/wp_update_post)
   page right, you need to make sure that there’s a post ID being delivered. Maybe
   double-check that `$post['ID']` is set correctly right before you do your `wp_update_post()`.
 * Also, if I’m reading the Codex page right, `save_post()` is fired by `wp_update_post()`,
   so you may be duplicating effort there. (I’m not 100% sure of that, though.)
 *  Thread Starter [TIves](https://wordpress.org/support/users/tives/)
 * (@tives)
 * [13 years, 3 months ago](https://wordpress.org/support/topic/edit-post-programaticly-in-postphp/#post-3506397)
 * Thank you Patrick!
 * What I am finding in my ongoing learnings of WordPress is that the most appropriate
   way to deal with something like this is to use an add_filter option, which, to
   my surprise, is more robust than I had thought.
 * For example, a filter (function) built to apply formatting to a post’s content
   will actually be passed the post’s content when the filter is called. You simply
   apply your desired formatting and then pass-back (return) the content at the 
   end of the function. WordPress takes care of the rest.
 * Currently, I am playing around with content_edit_pre and content_save_pre as 
   the potential triggers.
 * [http://codex.wordpress.org/Plugin_API/Filter_Reference](http://codex.wordpress.org/Plugin_API/Filter_Reference)
 * All the best!
 * Trip

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Edit Post Programaticly in post.php’ is closed to new replies.

## Tags

 * [edit-post](https://wordpress.org/support/topic-tag/edit-post/)
 * [wp_update_post](https://wordpress.org/support/topic-tag/wp_update_post/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 3 replies
 * 2 participants
 * Last reply from: [TIves](https://wordpress.org/support/users/tives/)
 * Last activity: [13 years, 3 months ago](https://wordpress.org/support/topic/edit-post-programaticly-in-postphp/#post-3506397)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
