Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PHP Script to check permalink structure in WP

Posted on 2009-05-11
2
Medium Priority
?
413 Views
Last Modified: 2012-05-06
Those of you who have used Wordpress know that you can set your permalink structure with custom formats. What I want to know is if there is a way to run a php script within a plugin that will check how the user has their permalinks set up?

The reason I ask is because I have a plugin that generates a "Subscribe to comments" link for each post/page. This works 85% of the time because most people set up their permalinks like this:

/%category%/%postname%/

However, the other 15% of the time that it doesn't work is because someone either HASN'T set up their permalinks and is still using http://yoursite.com?p=40 - OR, they set up their permalinks like this:

/%category%/%postname%

The absence of the trailing slash at the end of the permalink completely ruins the subscribe to comments link that my plugin generates because it relies on that trailing slash being there so that the link will turn out like this:

http://yoursite.com/stuff/post-1/feed

Without it, the link turns out being output like this:

http://yoursite.com/stuff/post-1feed

So, I want to try to come up with a way that I can check how the user has their permalinks set up and then take the appropriate actions to always output the right link based on their permalink structure.
0
Comment
Question by:blatantwaste
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 7

Accepted Solution

by:
ycTIN earned 2000 total points
ID: 24355720
use parse_url() or try below
<?php
detectUrlStyle("http://yoursite.com/stuff/post-1/");
detectUrlStyle("http://yoursite.com/stuff/post-1");
detectUrlStyle("http://yoursite.com/?p=40");
detectUrlStyle("http://yoursite.com/post.php?p=40");
detectUrlStyle("http://yoursite.com/post.php");
 
function detectUrlStyle($url){
	if (strlen($url) < 4) {
		echo 'invalid url';
	}
	
	$url = strtolower($url);
	
	if (false !== strpos($url,'?') || false !== strpos($url,'.php',strlen($url) - 4)) {
		echo 'default url style';
	} else {
		if ('/' == $url[strlen($url) - 1]) {
			echo 'permalink, end with /';
		} else {
			echo 'permalink, end without /';
		}
	}
	
	echo "\n";
}
?>

Open in new window

0
 
LVL 3

Author Closing Comment

by:blatantwaste
ID: 31580175
With a little modification that worked perfectly! Thanks!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
This video teaches users how to migrate an existing Wordpress website to a new domain.
The purpose of this video is to demonstrate how to Import and export files in WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Click on Too…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question