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:
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
- The topic ‘Edit Post Programaticly in post.php’ is closed to new replies.