I fear I am going to have to write a bunch of code to get what I need, which is fine, but I want to check to see if there’s anything in-built before I embark on that journey.
I am working on a plugin and I need to get the relative path for a given permalink_structure. Currently, the permalink structure option returns /%postname%/%post_id%/. I have the post (page) id e.g. 100.
From what the permalink is set up as, I know the full relative path should be /iamapost/100. However, it seems the best I can do is the following:
get_permalink($post_id) === "https://example.com/iamapost/"; parse_url(get_permalink($post_id), PHP_URL_PATH) === "/iamapost/"; get_post_permalink($post_id) === "https://example.com/?post_type=page&p=100"; get_site_uri === "https://example.com";
Given that:
get_option('permalink_structure') === "/%postname%/%post_id%/";
Can the relative path be taken from this or any permalink structure set up on the permalink settings page – whatever that might be…
Please, please do not assume this is a duplicate, because I really have searched and no question and answer is specific to reacting to whatever the permalink structure is at the time. If such an answer exists, then cool.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Is there anything built into the WordPress core to enable me to get the relative path from the “permalink structure” option
No, because it isn’t necessary, PHP provides this via parse_url. Using php -a we can launch an interactive PHP shell and test the code:
php > echo parse_url( 'http://example.com/iamapost/100', PHP_URL_PATH ); /iamapost/100 php >
If we read the PHP docs we see that it also handles query strings, etc, e.g.:
php > echo parse_url( 'http://example.com/iamapost/100?foo=bar#test', PHP_URL_PATH );
/iamapost/100
php >
Your problem is not retrieving the relative URL.
Your results do not contain the /100 because your permalinks do not contain it. Run echo get_permalink(); and it will become obvious. You are assuming that your permalinks contain the post ID. They do not.
Given that:
get_option('permalink_structure') === "/%postname%/%post_id%/";Can the relative path be taken from this or any permalink structure set up on the permalink settings page – whatever that might be…
Yes, via get_permalink() and parse_url.
I decided to spin up a local site of WP v5.6.1, and gave it the permalink structure you used:
I then fired up wp shell and ran the code and got the expected results:
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="addbcccadfccc3d9eddbdbdb">[email protected]</a>:/srv/www/wordpress-one/public_html$ wp shell
wp> echo parse_url(get_permalink(168), PHP_URL_PATH);
/test-2/168/
wp> echo get_option('permalink_structure');
/%postname%/%post_id%/
wp>
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

