Last active
May 10, 2016 15:10
-
-
Save daltonrooney/1a4e25165a3cb41fd60f to your computer and use it in GitHub Desktop.
overwrite the_content() with WP template output
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /* Retrieve the rendered content generated by Advanced Custom Fields and your template and | |
| * save it to the_content field. Overwrites existing content. Useful for Yoast SEO analysis compatibility | |
| * and acts as a fallback if the user ever changes themes. | |
| */ | |
| /* Requires simplehtmldom, get it here: http://simplehtmldom.sourceforge.net */ | |
| require 'simplehtmldom.php'; | |
| add_action('acf/save_post', 'mctc_acf_save_post', 1); | |
| function mctc_acf_save_post($post_id) { | |
| if( empty($_POST['acf']) ) return; | |
| /* Works with TwentyFifteen. CHANGE THIS to work with your theme. */ | |
| $contentID = '.entry-content'; | |
| /* Get the Preview URL */ | |
| $post = get_post($post_id); | |
| $preview_link = set_url_scheme( get_permalink( $post_id ) ); | |
| $preview_link = esc_url( apply_filters( 'preview_post_link', add_query_arg( 'preview', 'true', $preview_link ) ) ); | |
| /* Use the auth cookie of the currently logged in user to retrieve the page content */ | |
| $cookies = array(); | |
| foreach ( $_COOKIE as $name => $value ) { | |
| $cookies[] = new WP_Http_Cookie( array( 'name' => $name, 'value' => $value ) ); | |
| } | |
| $request = wp_remote_get( $preview_link, array( 'cookies' => $cookies ) ); | |
| $body = wp_remote_retrieve_body( $request ); | |
| $html = str_get_html($body); | |
| $primary = $html->find($contentID,0)->innertext; | |
| $post->post_content = $primary; | |
| /* Unhook so we don't get an infinite loop */ | |
| remove_action('acf/save_post', 'mctc_acf_save_post', 1); | |
| wp_update_post( $post ); | |
| /* Rehook */ | |
| add_action('acf/save_post', 'mctc_acf_save_post', 1); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment