Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save n7studios/0e279818a88a4a596cbaecf57ac9a0c5 to your computer and use it in GitHub Desktop.
Save n7studios/0e279818a88a4a596cbaecf57ac9a0c5 to your computer and use it in GitHub Desktop.
WordPress to Buffer Pro: Convert Paragraphs to Breaklines
<?php
/**
* Plugin Name: WP to Buffer Pro: Convert Paragraphs to Breaklines in Statuses
* Plugin URI: http://www.wpzinc.com/
* Version: 0.0.1
* Author: WP Zinc
* Author URI: http://www.wpzinc.com
* Description: Modify the {content} output by converting paragraphs to breaklines
*/
/**
* Modifies the Post Content generated from using the {content} status tag,
* immediately before the status is sent to Buffer.
*
* @since 0.0.1
*
* @param string $content Post Content
* @param WP_Post $post WordPress Post
* @return string Post Content
*/
function wp_to_buffer_publish_modify_content( $content, $post ) {
// Start over with the Post Content
$content = $post->post_content;
// Strip shortcodes
$content = strip_shortcodes( $content );
// Apply filters to get true output
// This will convert newlines to paragraphs
$content = apply_filters( 'the_content', $content );
// Replace paragraphs with double breaklines from the original Post Content
$content = preg_replace( '/<p(.*?)>((.*?)+)\<\/p>/', '${2}' . "\n\n", $post->post_content );
// If the content originates from Gutenberg, remove double newlines and convert breaklines
// into newlines
if ( strpos( $post->post_content, '<!-- wp:' ) !== false ) {
// Remove double newlines, which may occur due to using Gutenberg blocks
// (blocks are separated with HTML comments, stripped using apply_filters( 'the_content' ), which results in double, or even triple, breaklines)
$content = preg_replace('/(?:(?:\r\n|\r|\n)\s*){2}/s', "\n\n", $content );
// Convert <br> and <br /> into newlines
$content = preg_replace( '/<br(\s+)?\/?>/i', "\n", $content );
}
// Strip HTML Tags
$content = strip_tags( $content );
// Decode content to avoid encoding issues on status output
$content = html_entity_decode( $content );
// Finally, trim the output
$content = trim( $content );
// Return content
return $content;
}
// Filter for Pro version of Plugin
add_filter( 'wp_to_buffer_pro_publish_get_content', 'wp_to_buffer_publish_modify_content', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment