Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Require more work but does the job.
- */
- class Posts {
- const CATCH_POST_PRIORITY = -32767;
- const TEXT_TO_CHANGE = 'my-plugin-text-needs-to-be-manipulated';
- /*
- * Manipulate our plugin's text\data
- * @param array $posts
- * @return array
- */
- public function manipulate($posts){
- $newPosts = array();
- foreach ($posts AS $post) {
- if (false === strpos($post->post_content,self::TEXT_TO_CHANGE)
- || !self::IsPreviewMode($post->ID)){
- $newPosts[] = $post
- continue ;
- }
- //remove new lines
- //TODO: extract your data to your HTML (real plugin uses a View object and render's the data)
- $html = str_replace("\n","",file_get_content("some.html"));
- //assign back to post content
- $post->post_content = str_replace(self::TEXT_TO_CHANGE,$html,$post->post_content);
- $newPosts[] = $post;
- }
- return $newPosts;
- }
- /**
- * Check if we are in preview mode of current post id
- * @param int $postID
- * @return bool
- */
- private static function IsPreviewMode($postID){
- return isset($_GET['preview']) && isset($_GET['p']) && $_GET['p'] != $postID;
- }
- }
- $posts = new Posts;
- add_action('the_posts' , array($posts, 'manipulate'), Posts::CATCH_POST_PRIORITY);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement