views:

38

answers:

2

Hello,

How to Get page permalink and title outside the loop in wordpress.

I have a function like

function get_post_info(){
  $post;
  $permalink = get_permalink($post->ID);
  $title = get_the_title($post->ID);
  return $post_info('url' => $permalink, 'title' => $title);
}

when this function called within the loop, it returns the post's title and url.

When it is called outside the loop. It is not returning the current page's title and url. When called in home page it should return the home page's title and url

How to get like this ? instead this function returns the latest posts title and url

+1  A: 

After research i found the ans myself

wp_title() will return the page title and

$url = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']; to return the page url

Aakash Chakravarthy
+1  A: 

You're opening yourself up to a possible XSS attack by printing an unescaped REQUEST_URI.

Your function is fine, you're just missing the global keyword. Change $post; to global $post;, and you're sailing!

TheDeadMedic
Is there any problem with the super global variables? those are unsafe ? please explain me because, i am developing a plugin and these are used that plugin
Aakash Chakravarthy
This isn't really something I can cover in a comment - Google 'PHP security', read about XSS, and always sanitize and escape data from sources that the end user can manipulate.
TheDeadMedic
oh yes thank you ! i just forgot it !
Aakash Chakravarthy