• Resolved fpmtinc

    (@fpmtinc)


    We publish entire articles to Facebook: Post Title, Post Content, and Image. I noticed that the content on Facebook treats paragraphs like line breaks, rather than having spacing between paragraphs. I know the fix is to have \n\n inserted after every paragraph, but I need help in the function to override the plugin defaults. I wrote the following, but it isn’t working. Am I using the wrong hook?

    function rop_change_share_content( $content, $post_id ){
    	//remove empty paragraphs
    	$content = str_replace('<p>&nbsp;</p>', '', $content);
    	$content = str_replace('<p></p>', '', $content);
    	//add spacing for facebook
    	$content = str_replace('</p>', "</p>\n\n", $content);   
    
    	return $content;
    }
    add_filter('rop_content', 'rop_change_share_content', 10, 2 );
    • This topic was modified 3 years, 6 months ago by fpmtinc.
    • This topic was modified 3 years, 6 months ago by fpmtinc.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Contributor Uriahs Victor

    (@uriahs-victor)

    Posting this here as well incase someone else is trying to accomplish the same thing:

    With the filter you used, ‘rop_content’ receives the content that’s already been stripped of HTML tags so the replace code would not find any tags.

    But in that filter we also pass it the Post ID. So you can use the Post ID to get the stripped code and perform your own changes on the code such as adding a custom share length etc. But that filter takes 3 arguments, not 2.

    So your code would looks something like this:

    function rop_change_share_content( $content, $post_id, $account_id, $service ){
     
    $post_object = get_post($post_id);
     
    $post_content = $post_object->post_content;
     
    // Do something with the $post_content...
     
    return $post_content; //return whatever changes you made to it back to the filter.
    }
    add_filter('rop_content', 'rop_change_share_content', 10, 3 );
    Thread Starter fpmtinc

    (@fpmtinc)

    Thank you so much for your time! The solution works great.

    Plugin Contributor Uriahs Victor

    (@uriahs-victor)

    There was a slight bug in the code I posted above. Below is has the correct filter and number of arguments;

    function rop_change_share_content( $content, $post_id, $account_id, $service ){
     
    $post_object = get_post($post_id);
     
    $post_content = $post_object->post_content;
     
    // Do something with the $post_content...
     
    return $post_content; //return whatever changes you've made back to the filter.
    }
    add_filter('rop_content_filter', 'rop_change_share_content', 10, 4 );
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Function to Fix Paragraph Spacing for Facebook’ is closed to new replies.