2

I have a function like bellow

if(!function_exists('generate_share_urls')) {
  function generate_share_urls() {
      $title = the_title();
      $content = the_content();
    $share_urls = array (
      'facebook'      => 'http://www.facebook.com/sharer/sharer.php?u=',
      'twitter'       => 'http://twitter.com/share?url=',
      'google_plus'   => 'https://plus.google.com/share?url=',
      'linkedin'      => 'http://www.linkedin.com/shareArticle?mini=true&url=',
      'pinterest'     => 'http://pinterest.com/pin/create/button/?url=',
      'email'         => 'mailto:?subject={'.$title.'}&body='.$content.',
      'permalink'     => ''
   );
   return $share_urls;
  }
}

Now when run the function i cant find the $title and the $content of the page. Is there any fault with my code.

1 Answer 1

2

the_title() and the_content() will print the title/content, not return it. What you need instead are the get_ functions:

if(!function_exists('generate_share_urls')) {
  function generate_share_urls() {
      $title = get_the_title();
      $content = get_the_content();
    $share_urls = array (
      'facebook'      => 'http://www.facebook.com/sharer/sharer.php?u=',
      'twitter'       => 'http://twitter.com/share?url=',
      'google_plus'   => 'https://plus.google.com/share?url=',
      'linkedin'      => 'http://www.linkedin.com/shareArticle?mini=true&url=',
      'pinterest'     => 'http://pinterest.com/pin/create/button/?url=',
      'email'         => 'mailto:?subject={'.$title.'}&body='.$content.',
      'permalink'     => ''
   );
   return $share_urls;
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

from the syntax highlighting, he may have an unclosed quote in there as well, not sure if he just copied it wrong or whether it's really an error...
Thank you. I have found what i want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.