Support » Plugins » Hacks » Edit Post Programaticly in post.php

  • 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:


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

    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!


Viewing 3 replies - 1 through 3 (of 3 total)
  • 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!

    If I’m reading the 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.)

    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.

    All the best!


Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Edit Post Programaticly in post.php’ is closed to new replies.