[HOWTO] Add layout data to Yoast analysis tools in page edit

Add the following as a plugin, and enjoy!

Please note, Classic Editor is needed for this to work, it will NOT work with Gutenburg.

<?php
/**
 * Plugin Name: Beaver Yoast Example
 * Description: Adds the layout content into Yoast tools in page edit.
 * Version: 0.1
 * Author: Simon
 * Copyright: (c) 2014 Beaver Builder
 * License: GNU General Public License v2.0
 * License URI: http://www.gnu.org/licenses/gpl-2.0.html
 */
class BeaverYoastExample {
	static public function init() {
		add_action('admin_footer-post.php', function() {
			$id = $_GET['post'];
			if ( ! get_post_meta( $id, '_fl_builder_enabled', true ) || ! defined( 'WPSEO_VERSION' ) ) {
				return false;
			}
			ob_start();
			echo do_shortcode( "[fl_builder_insert_layout id=$id]" );
			$data = ob_get_clean();
			ob_end_flush();
			$data = str_replace( PHP_EOL, '', $data );
			?>
		<script>
		(function($){
			$(document).ready(function(){

				BeaverPlugin = function() {
					window.YoastSEO.app.registerPlugin( 'beaverPlugin', {status: 'ready'} );
					window.YoastSEO.app.registerModification( 'content', this.myContentModification, 'beaverPlugin', 5 );
				}
				BeaverPlugin.prototype.myContentModification = function(data) {
					return '<?php echo $data; ?>';
				};
				new BeaverPlugin();
			});
		})(jQuery);
		</script>
			<?php
		});
	}
}
BeaverYoastExample::init();
1 Like