Support » Developing with WordPress » Button with Fixed prefix and dynamic permalink
Button with Fixed prefix and dynamic permalink
-
Hello,
can anybody help me with this? I want to make a button where the domain and prefix will be fixed but the permalink will be dynamic.
Example: “https://domain.com/download/(dynamic permalink)” the domain and download prefix(domain.com/download/) is fixed for all articles but the permalink will be dynamic for the current post.
For example, if I am on this post “https://domain.com/self-motivation-pdf” then the button on this post should be like “https://domain.com/download/self-motivation-pdf”, the same goes for all posts, also I will be using this button on a post template so the button will appear auto.
-
This topic was modified 4 months, 3 weeks ago by
peterturner849.
-
This topic was modified 4 months, 3 weeks ago by
-
Hi,
in Settings -> Permalinks just choose “Custom Structure” and set
/download/%postname%/
excuse me, I think you did not get my point here. it’s not about the post permalink, I am talking about a button in a post,
In my site, when I publish a post, a child page auto generated with the same permalink and with extra slug before the permalink like: When I publish a post (domain.com/post-url-1/) an auto page created like (domain.com/download/post-url-1).
I want a default button in every post that will link to its child page. so i need a button with fixed “domain.com/download/” + “dynamic permalink/”.
Hi, I understand now, I have made you a plugin that does that.
Just create a file dynButton.php in /wp-content/plugins
In this file add this:
<?php /** * Plugin Name: dynamicPostUrlButton * Description: Return a button with a dynamic url [dynamic_post_url_button dynb_link="Add to Link" dynb_text="Link Text"] * Version: 0.1 * Author: your-name **/ function dynamicPostUrlButton( $addToUrl = []) { $dynButton = '<a class="button" href="'.get_permalink().$addToUrl['dynb_link'].'">'.$addToUrl['dynb_text'].'</a>'; return $dynButton; } add_shortcode('dynamic_post_url_button', 'dynamicPostUrlButton');
When you activate your plugin you can call the button with a shortcode like this
[dynamic_post_url_button dynb_link="Add To Url" dynb_text="Link Text"]
Add To Url is the part that is added to the url, Link Text is the text that the button then has.
You can of course customize the button by editing the plugin script
get_permalink() function calling full permalink with root domain, the url was building like this “domain.com/post-url/domain.com/post-url/” then after little bit edit it looks like this,
function get_relative_permalink( $url ) { return str_replace( home_url(), "", $url ); } function dynamicPostUrlButton( $addToUrl = []) { $dynButton = '<a class="button" href="/download'.wp_make_link_relative(get_permalink($post->ID)).$addToUrl[''].'">'.$addToUrl['dynb_text'].'</a>'; return $dynButton; } add_shortcode('dynamic_post_url_button', 'dynamicPostUrlButton');
I added download so I could get download slug after the root domain then the next function wp_make_link_relative(get_permalink($post->ID)) I got on the Internet. working for only the post URL without the domain. can you check if this is ok, cuz This solved the issue.
Hi, works great but you removed the variable that was supposed to be hooked behind the realtive url.
I have fixed the function dynamicPostUrlButton for you:
function dynamicPostUrlButton( $addToUrl = []) { $dynButton = '<a class="button" href="/download'.wp_make_link_relative(get_permalink($post->ID)).$addToUrl[''].'">'.$addToUrl['dynb_text'].'</a>'; return $dynButton; }
Keep in mind that you have to style the css class “button” right now it probably looks like a normal link (could be that your theme has a button class and that it already looks good like a button)
Sorry I didn’t notice this:
you added /download at the beginning of the url so if your current post has this permalink:
https://example.com/category/mypost
Your function will return
/download/category/mypost/ + your dynb_link variable
Is this what you wanted?
-
This reply was modified 4 months, 3 weeks ago by
Benni.
I will use this button on the post (post permalink: domain.com/post-name) so there will not be any extra slugs like categories or tags. so there should not be any issue. Thank you so much for helping me in creating this button
by the way, will you help me in this.. https://wordpress.org/support/topic/wp-function-for-a-specific-post-type-only/
Hi, if you change your permalink structure in the future it won’t work, we need to fix that.
Please explain the full structure of the link url to me again so I can help you with that.
And sorry no clue for the other post that you made, could you add comments to your code that I understand what you are doing?
let me explain when I create a post, another post type is also created with the same post permalink, for example, when I publish a post “domain.com/post-1” I also create a download page “domain.com/download/post-1” here you can see the post permalink is same. so I want a button to put in every post that will link to its download page.
I use GP premium, using Elements (Block) I designed main post structure also added a button, but I dont want to add button manually and change url in every post. It should be dynamic.
Use this for the link’s href value:
str_replace('domain.com/', 'domain.com/download/', get_permalink())
Of course use your actual domain name. This wouldn’t be portable to other domains. Instead of hard coding the domain, you could usesite_url()
for better portability.thanks a lot. its Done.
-
This reply was modified 4 months, 3 weeks ago by
- You must be logged in to reply to this topic.