Viewing 11 replies - 1 through 11 (of 11 total)
  • Hi,

    in Settings -> Permalinks just choose “Custom Structure” and set

    /download/%postname%/
    Thread Starter HI

    (@peterturner849)

    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

    Thread Starter HI

    (@peterturner849)

    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 1 year, 10 months ago by !Benni.
    Thread Starter HI

    (@peterturner849)

    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://www.ads-software.com/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?

    • This reply was modified 1 year, 10 months ago by !Benni.
    • This reply was modified 1 year, 10 months ago by !Benni.
    Thread Starter HI

    (@peterturner849)

    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.

    Moderator bcworkz

    (@bcworkz)

    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 use site_url() for better portability.

    Thread Starter HI

    (@peterturner849)

    thanks a lot. its Done.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Button with Fixed prefix and dynamic permalink’ is closed to new replies.