Reputation: 11
I'm trying to do this and having a lot of problems figuring it out. I know I'm a complete php noob, but I really need to figure it out and so I'd appreciate your help.
I'm trying to pass a "corrected" url to a widget on my website so if people click a button they will download the page they are on in pdf.
My website uses friendly urls http://www.sample.com/friendly/url/this/thing.html
What i need to do is grab the current url and change friendly to pdf and put the result into the button.
What I've come up with is #failsauce drizzled on #filetofpain in a #shitsandwich
<?php
function curPageURL() {
$isHTTPS = (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on");
$port = (isset($_SERVER["SERVER_PORT"]) && ((!$isHTTPS && $_SERVER["SERVER_PORT"] != "80") || ($isHTTPS && $_SERVER["SERVER_PORT"] != "443")));
$port = ($port) ? ':'.$_SERVER["SERVER_PORT"] : '';
$url = ($isHTTPS ? 'https://' : 'http://').$_SERVER["SERVER_NAME"].$port.$_SERVER["REQUEST_URI"];
return $url;
}
echo curPageURL();
?>
which results my url.
I have not even gotten to the replace the portion of the url part because I can't figure out how to get the url or break it up. It's sad.
So like a beggar with a tin cup and rags on I plead for help :)
What I need to do is break up the url returned into pieces...
if it returns "www.mysite.com/01/05/2013/9013/your-mom-wears-combat-boots/" i need to change it to: "www.mysite.com/pdf/9013/your-mom-wears-combat-boots.pdf"
(likely with http:// in front of it as well the posting mechanism here things I'm posting links and I have no reputation so it won't let me put the full http:// www mysite com in the examples)
Upvotes: 1
Views: 196
Reputation: 621
i haven't tried your code but it already seems to have a double echo
line..
Anyway i would like to give you a pretty simple method i like to use:
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
}
else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
what this function does is everytime you call curPageURL()
you get your current page url, as it seems in the url bar. than you can work with it as you want.
i hope this solves your problem :)
To use this for getting the url and than changin the ending from .html
to .pdf
all you will need to do is:
$url = curPageURL(); // gets back www.sample.com/page.html
$newurl = str_replace(".html", ".pdf", $url); // changes url to www.sample.com/page.pdf
this code uses the function from the top part of the answer
If it returns you :www.mysite.com/01/05/2013/9013/your-mom-wears-combat-boots/
than you will need to do:
$url = curPageURL(); // gets back www.sample.com/page/
$newurl = substr_replace($url, "", -1) // changes url to www.sample.com/page
$newurl .= ".pdf" // adds the .pdf to the url so you get www.sample.com/page.pdf
First of all - glad you use wordpress :)
now, the wordpress codex makes it easier, so much easier actually to do: for this we need to make some changes, also change the curPageURL function.
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"];
}
else {
$pageURL .= $_SERVER["SERVER_NAME"];
}
return $pageURL;
}
and i will explain: $_SERVER["SERVER_NAME"]
gives us the domain name, like domain.com
.
$_SERVER["REQUEST_URI"]
returns us everything after the domain, the path.
which in our case is for example: 01/03/2013/9013/page
.
so now the curPageURL
function returns just the domain name.
and now lets get to work about what's next:
$url = curPageURL(); // gets back http://www.sample.com
$url .= "/pdf"; // we add the /pdf to the url so it is now: http://www.sample.com/pdf
$postid = get_the_ID(); // gives us the post ID. for example 9013
$url .= "/" . $postid; // now we take the post ID and add it to the url: http://www.sample.com/pdf/9013
$the_post = get_post($postid); // get the post by the ID
$post_slug = $the_post['post_name']; // get the slug of the post
$url .= "/" . $post_slug; // adds the slug to the url: http://www.sample.com/pdf/9013/your_mom_wears_combat_boots
$url .= ".pdf"; // finally add the pdf to the url so now you get: http://www.sample.com/pdf/9013/your_mom_wears_combat_boots.pdf
and here it is without the comments:
$url = curPageURL();
$url .= "/pdf";
$postid = get_the_ID();
$url .= "/" . $postid;
$the_post = get_post($postid);
$post_slug = $the_post['post_name'];
$url .= "/" . $post_slug;
$url .= ".pdf";
and if you want to minimize the lines:
$url = curPageURL() . "/pdf/" . get_the_ID(); // url is now: www.domain.com/pdf/postid
$the_post = get_post(get_the_ID());
$url .= "/" . $the_post['post_name'] . ".pdf"; //url is now: www.domain.com/pdf/postid/post_slug.pdf
now you can echo
the url by echo $url;
I have made a mistake, about getting the slug.
i used: $the_post = get_post(get_the_ID());
and than took the slug, name: $the_post['post_name']
this wasn't right to do.
the best way to take the slug out is by running the code:
$post_slug = basename(get_permalink());
so this chagnes a little bit the way it supposd to look:
$url = curPageURL() . "/pdf/" . get_the_ID(); // url is now: www.domain.com/pdf/postid
$post_slug = basename(get_permalink()); //gets the post slug
$url .= "/" . $post_slug . ".pdf"; //url is now: www.domain.com/pdf/postid/post_slug.pdf
and we can make it ever more minimized:
$url = curPageURL() ."/pdf/". get_the_ID() ."/". basename(get_permalink()) .".pdf";
and now you can just echo $url;
don't forget: this function needs to be in the page.php
or single.php
for it to know what is the slug of the post and the id of the post.
the line: $url = curPageURL() ."/pdf/". get_the_ID() ."/". basename(get_permalink()) .".pdf";
needs to be in the loop: <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
or any other loop you use.
this should do the job :)
Upvotes: 0
Reputation: 815
You can use $_SERVER["HTTP_HOST"]
and $_SERVER["REQUEST_URI"]
to get the requested URL.
Upvotes: 0
Reputation: 7475
You are using double echo echo $current_url;
change to echo $current_url;
Upvotes: 3