2

Wordpress provides a function called "the_permalink()" that returns, you guessed it!, the permalink to a given post while in a loop of posts.

I am trying to URL encode that permalink and when I execute this code:

<?php
print(the_permalink());
$permalink = the_permalink();
print($permalink);
print(urlencode(the_permalink()));
print(urlencode($permalink));
$url = 'http://wpmu.local/graphjam/2008/11/06/test4/';
print($url);
print(urlencode($url));
?>

it produces these results in HTML:

http://wpmu.local/graphjam/2008/11/06/test4/
http://wpmu.local/graphjam/2008/11/06/test4/
http://wpmu.local/graphjam/2008/11/06/test4/
http://wpmu.local/graphjam/2008/11/06/test4/
http%3A%2F%2Fwpmu.local%2Fgraphjam%2F2008%2F11%2F06%2Ftest4%2F

I would expect lines 2, 3 and 5 of the output to be URL encoded, but only line 5 is so. Thoughts?

1
  • i count 6 print statements but only 5 lines of output..? Commented Nov 8, 2008 at 1:42

3 Answers 3

11

According to the docs, the_permalink prints the permalink vs returns it. So, urlencode isn't getting anything to encode.

Try get_permalink.


[EDIT]

A little late for an edit, but I didn't realize the print counts were such an issue.

Here's where they're all coming from:

<?php
print(the_permalink());                                // prints (1)
$permalink = the_permalink();                          // prints (2)
print($permalink);                                     // nothing
print(urlencode(the_permalink()));                     // prints (3)
print(urlencode($permalink));                          // nothing
$url = 'http://wpmu.local/graphjam/2008/11/06/test4/'; 
print($url);                                           // prints (4)
print(urlencode($url));                                // prints (5)
?>
Sign up to request clarification or add additional context in comments.

3 Comments

how come $permalink is being populated as expected then?
it isn't. You only get 5 rows, but you obviously call "print" 6 times. That means that print($permalink); doesn't output anything.
The first time get's 'printed' though, by proxy before the call to print is even made: when he tries to assign $permalink. The print doesn't print anything, but it's already been printed, hence the count of 5 lines printed, not 4.
7

the_permalink() echoes the permalink

get_the_permalink() returns the permalink so it can be assigned to a variable.

(same goes with most functions in WordPress: the_something() has a get_the_something() to return value instead of echoing it)

2 Comments

Except, in this case get_the_permalink() doesn't exist - it's get_permalink(), and it requires a post ID. Seems ass backwards to me, compared to other functions.
@Artem, get_permalink doesn't require a post ID, it is optional. codex.wordpress.org/Function_Reference/get_permalink
5

@Jonathan has the reason why, and the way you should deal with it in WordPress (ie. use the right function for the job).

Here is how to fix it when there isn't a function that returns a string:

ob_start();
the_permalink();
$permalink = ob_get_clean();
print(urlencode($permalink));

Comments

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.