0
votes

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> ';
}
?>
2
So your converting the url from http to https? I think your str_replace may be backwards. - jlemm45
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
why would you want to do that? surely the content you posted before that date is reachable via https now? - Gerald Schneider
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

2 Answers

0
votes

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.

0
votes

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;