How to add custom term in wordpress post slug? - php

I want to build an URL in WordPress site with a pattern which looks like this:
mydomain.com/[category-name]/[slug]/[photo-{0-9}]/
Tried re-writing URLs and added "photo" param to query var to catch the photo number, but couldn't succeed.
Am already using a plugin to remove /category/ from URLs.
Any idea to achieve it?
Thanks in advance!

You can use it like this
yourdomain.com/%category%/%postname%/photo-%post_id%/
it is not clear that what are [slug] and {0-9} are. But I think you can manage with them with this answer

Was able to get this working
Adding the solution here so someone can make use or tune it further
I did this adding following rewrite key value pair to $wp_rewrite->rules array:
'([^)]+)/([^)]+)/(photo-\d+)?$' => 'index.php?category_name=$matches[1]&name=$matches[2]&photo=$matches[3]'
This regex groups (inside parenthesis()) url to $matches array which is turn assigned to WP query string.

Related

Using PHP, Why can I not use $_GET to get a value from a URL if there is a _ in the URL?

I have this in my URL;
?utm_source=John%27s%20Source
I want to get the value using this;
echo rawurlencode(stripslashes($_GET['utm_source']));
That does not return anything. However, if I get rid of the _ on 'utmsource' in both the URL and the _GET, it will return this;
John%27s%20Source
Which is exactly what I am looking for. Why can I not do the same with the underscore?
I solved it.
So it turns out that by defualt WPEngine will not let you use '$GET' with something that starts with "utm". I guess this has to do with it being a Google search term and by default it is turned off. I found this thread and contacted WPEngine like the person in the thread, and now it works just fine.
Thanks for the help guys.
Having "utm_" in the URL string breaks the $_GET variable in Wordpress

Redirecting to a url by appending query strings to it

I have to capture a query string at the starting of my survey, which i was able to do using php syntax. I now have the query string sitting in php.
My next task is to redirect to another URL at the end of my survey, BUT by appending the same query string I captured before.
For example,
If I have a php field $user='abcdefgh';
And
I need to redirect to another link at the end, say https://en.wikipedia.org/wiki/Stack_Overflow
By adding the query string $user. So the URL now becomes
https://en.wikipedia.org/wiki/Stack_Overflow?id=abcdefgh
Can someone guide me how to accomplish this.
I tried to do it using header in php, but it doesn't seem to work.
Any assistance will be greatly appreciated.
Neeraj
You can try something like this:
$url = https://en.wikipedia.org/wiki/Stack_Overflow;
$user='abcdefgh';
header("location:".$url."?id=".$user);

PHP check if href is a file

I am using a script to check links on a given page. I am using simple html DOM to parse the information into an array. I have to check the href of all the a tags to find if they contain a file or something like # or JS.
I tried the following without success.
if(preg_match("|^(.*)|iU", $href)){
save_link();
}
I dont know it my pattern is wrong or if there is a better method to complete this function.
I want to be able to detect if $href contains .com .php .file extensions. This way it will filter out items like # "function()" and other items used in the href attribute.
EDIT:
parse_url will not work stop posting it. The value # returns as a valid url like I stated above I am trying to look for any string followed by .* with no more than 4 chars following the .
I believe that the function you're looking for is parse_url().
This function will take a URL string, and return an array of components, which will allow you to work out what kind of URL it is.
However note that it has issues with incomplete URLs in PHP versions prior to 5.4.7, so you need to have the very latest PHP to get the best out of it.
Hope that helps.
See http://php.net/manual/en/function.parse-url.php
I'm assuming you don't want to match fragments (#) because you are not concerned with following internal anchors.
parse_url breaks up the different parts of the url into an array. You can see the path component of the URL in this array and run your check against that.
You can use parse_url() , like this :
$res = parse_url($href);
if ( $res['scheme'] == 'http' || $res['scheme'] == 'https'){
//valid url
save_link();
}
UPDATE:
I've added code to filter only http and https urls, thanks to Baba for spotting this.

Change url variable using php

Say I have a url like this in a php variable:
$url = "http://mywebsite.extension/names/level/etc/page/x";
how would I automatically remove everything after the .com (or other extension) and before /page/2?
Basically I would like every url that could be in $url to become http://mywebsite.extension/page/x
Is there a way to do this in php? :s
thanks for your help guys!
I think parse_url() is the function you're looking for. You can use it to break down an URL into it's component parts, and then put it back together however you want, adding in your own things as needed.
As PeeHaa noted, explode() will be useful for dividing up the path.

what is the easiest way of getting a number from a url?

I am hoping someone can help me, I have created url's like this
/this/is/a/test/index.asp
/this/is/a/test/index-1.asp
/this/is/a/test/index-2.asp
/this/is/a/test/index-3.asp
What is the easiest way to strip the number from the URL?
Instead of using variables like this:
/this/is/a/test/index.asp?no=1
/this/is/a/test/index.asp?no=2
/this/is/a/test/index.asp?no=3
to create variables I am using the number in the URL to dynamically call the content on the page.
If the url is: /this/is/a/test/index-3.asp it should use the 3 and match the content according to it, similar as if I were to call
?no=3
I am using PHP for this...
The url structure will always have the variable define as match the last '-' [the-number] '.asp'
Thanks
Gerald Ferreira
You could use mod_rewrite to map URL patterns to actual URLs. You could achieve what you want with something similar to the following:
RewriteRule ^index-([0-9]+).asp$ index.asp?no=$1
You should be able to match it out with:
if (preg_match('/\-(\d+)\.asp$/', $_SERVER['REQUEST_URI'], $a)) {
$pageNumber = $a[1];
} else {
// failed to match number from URL
}

Categories