Dynamic button link with changing url

Hello,
I have a button with a url redirection to a page, this url will change soon for seo reasons, is it possible to have something dynamic for that button link that will make it work even if the link of that page will change ?

1 Like

Following.

Hi @Lyser,

Thanks for reaching out.
It is not very clear what exactly you are expecting here. If the URL changes, it is not possible to recognize the newer URL from any dynamic variable. If you are expecting something else, please explain a bit more for our understanding.

Thanks

That’s what i wanted to know @tristup, thank you for clarifying things.

Hi @Lyser,

You are most welcome.

@Lyser Isn’t it possible to use a self-created shortcode and then using it to link to the page using page ID?

With this code in functions.php file:

<?php
/**
 * Plugin Name: Dynamic Link Shortcode
 * Plugin URI: https://yourwebsite.com/
 * Description: Adds a shortcode to generate dynamic links based on page ID.
 * Version: 1.0
 * Author: Your Name
 * Author URI: https://yourwebsite.com/
 */

function dynamic_link_by_id_shortcode($atts) {
    $atts = shortcode_atts(array('id' => '0'), $atts, 'dynamic-link-by-id');

    $link = get_permalink($atts['id']);
    
    if(!$link) return "";

    return esc_url($link);
}
add_shortcode('dynamic-link-by-id', 'dynamic_link_by_id_shortcode');

?>

Replace https://yourwebsite.com/ with your website URL and Your Name with your name or your organization’s name.

Activate the Plugin

Once activated, the shortcode [dynamic-link-by-id id="123"] should be available for use anywhere in your WordPress installation.

Hi @powerlogo,

What you are explaining, you can use the {{dc:post:permalink post=“12”}} where Post Id is 12. So you can get the permalink of specific posts.

Thanks

Thanks. I got it working for buttons. But what about creating link on some words in a text? This doesn’t seems to work on text links.

Hey @powerlogo,

You can use something like this when adding the link in a word <a href="{{dc:post:permalink post="12"}}"> Your Text here</a>. Theoretically, it should be working.

Hope that helps and let us know how it goes.

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.