1

I'm trying to create a box with Google Plus comments in my WordPress blog. My blog migrated from http to https in jan/2016 so I want to call the comment box with different permalink if the post date in before the migration day.

This is the original G+ comment box:

<div class="g-comments"
    data-href="<?php the_permalink(); ?>"
    data-width="700"
    data-first_party_property="BLOGGER"
    data-view_type="FILTERED_POSTMOD">
</div>

I'm using Studiopress Genesis, the G+ code is placed in the loop before original WP comments with Genesis Simple Hooks. And this is what I wrote and it appears before content. How can I move from echo to return? Any precious help?

<?php
$permalink = get_permalink();
$now = time();
$compare_time = mktime(0, 0, 0, 1, 1, 2016);
$post_time = get_post_time('U');
$url03 = str_replace('https://', 'http://', $permalink );

if ($post_time < $compare_time) {
echo '<div class="g-comments" data-href="';
echo $url03 . '"';
echo ' data-width="700" ';
echo 'data-first_party_property="BLOGGER" ';
echo 'data-view_type="FILTERED_POSTMOD">';
echo '</div> ';
}
else {

echo '<div class="g-comments" data-href="';
echo the_permalink() . '"';
echo ' data-width="700" ';
echo 'data-first_party_property="BLOGGER" ';
echo 'data-view_type="FILTERED_POSTMOD">';
echo '</div> ';
}
?>
EVE Milano
  • 109
  • 2
  • 12
  • So your converting the url from http to https? I think your str_replace may be backwards. – jlemm45 Jun 22 '16 at 14:29
  • now the blog uses HTTPS, before 1/1/2016 it used HTTP. So I want to call the right permalink to use in the G+ comment box --> $url3 – EVE Milano Jun 22 '16 at 14:38
  • why would you want to do that? surely the content you posted before that date is reachable via https now? – Gerald Schneider Jun 22 '16 at 16:11
  • I want to do that because after migration the blog losts all its comments (because G+ search for comments on HTTPS URL). Changing the old post URL inside the call for G+ comments, I am able to call back all the old comments. the plugin works, but it execute the shortcode before any content, this is the problem. i red i should use return instead of echo but i'm not an expert and I ask some help :) – EVE Milano Jun 22 '16 at 16:32

2 Answers2

1

You just need to save the HTML string to a variable and return that variable. Something like this should work.

<?php
function google_comments_html() {
  $permalink = get_permalink();
  $compare_time = mktime(0, 0, 0, 1, 1, 2016);
  $post_time = get_post_time('U');

  if ($post_time < $compare_time) {
    $permalink = str_replace('https://', 'http://', $permalink );
  }

  $html = <<<HTML
    <div class="g-comments"
      data-href="$permalink"
      data-width="700"
      data-first_party_property="BLOGGER"
      data-view_type="FILTERED_POSTMOD">
    </div>
HTML;

  return $html;
}

// Example calling the method
echo google_comments_html();

Note that the end HTML; can't have any spaces to the left of it.

abraham
  • 46,583
  • 10
  • 100
  • 152
  • Thank You Abraham. I need to change from echo to return because in WordPress, echo prints stuff before everything else, and it is not correct. Your code works in the same way mine works. If i create a shortcode with the code you provided, and I put in the loop text + shortcode: "this is the plugin [shortcode]", than the shortcode content is printed before "this is the plugin". I hope I have explained it correctly. – EVE Milano Jun 28 '16 at 12:19
  • I don't really understand your comment. The method in my answer won't echo anything unless you change it to. – abraham Jul 04 '16 at 20:50
0

Just use ob_get_contents: http://php.net/manual/en/function.ob-get-contents.php

ob_start();

$permalink = get_permalink();
$now = time();
$compare_time = mktime(0, 0, 0, 1, 1, 2016);
$post_time = get_post_time('U');
$url03 = str_replace('https://', 'http://', $permalink );

if ($post_time < $compare_time) {
echo '<div class="g-comments" data-href="';
echo $url03 . '"';
echo ' data-width="700" ';
echo 'data-first_party_property="BLOGGER" ';
echo 'data-view_type="FILTERED_POSTMOD">';
echo '</div> ';
}
else {

echo '<div class="g-comments" data-href="';
echo the_permalink() . '"';
echo ' data-width="700" ';
echo 'data-first_party_property="BLOGGER" ';
echo 'data-view_type="FILTERED_POSTMOD">';
echo '</div> ';
}

$out = ob_get_contents();

ob_end_clean();

return $out;
Mr. Hugo
  • 11,887
  • 3
  • 42
  • 60