Turning variable into a value set in shortcode

admin2025-06-02  4

I have a site on WordPress, and first of all I have this PHP shortcode that helps me to make customizable links in WP post editor without turning my CSS files into a dump of classes with basically the same parameters.

function custom_link($atts, $custom_title)
    {
    $url = $atts[url];
    $color = $atts[color];
    $icon = $atts[icon];
    return '<a class="button-round custom-link" style="background-color: #' . $color . ' !important" href="' . $url . '" target="_blank">' . $icon . '<span>' . $custom_title . '</span></a>';
    }

add_shortcode('custom-link', 'custom_link');

It lets me to put custom $url, background color of the button $color and icon $icon. It supposed to turn out like this, for example:

The problem is with $icon. Basically I have a separate PHP file full of variables with respectable SVG codes of icons set for them, let's get one of them, for example, $icon_paypal. I want the $icon variable to become the value of icon attribute of custom-link I set:

[custom-link url="/someurl" color="#092F87" icon="paypal_icon"]Donate[/custom-link]

So after that the return should be like this:

<a class="button-round custom-link" style="background-color: #' . $color . ' !important" href="' . $url . '" target="_blank">' . $icon_paypal . '<span>' . $custom_title . '</span></a>

...and then executed into a proper HTML code.

I'm a beginner in PHP, hopefully I explained my issue as exhaustively as I could.

I have a site on WordPress, and first of all I have this PHP shortcode that helps me to make customizable links in WP post editor without turning my CSS files into a dump of classes with basically the same parameters.

function custom_link($atts, $custom_title)
    {
    $url = $atts[url];
    $color = $atts[color];
    $icon = $atts[icon];
    return '<a class="button-round custom-link" style="background-color: #' . $color . ' !important" href="' . $url . '" target="_blank">' . $icon . '<span>' . $custom_title . '</span></a>';
    }

add_shortcode('custom-link', 'custom_link');

It lets me to put custom $url, background color of the button $color and icon $icon. It supposed to turn out like this, for example:

The problem is with $icon. Basically I have a separate PHP file full of variables with respectable SVG codes of icons set for them, let's get one of them, for example, $icon_paypal. I want the $icon variable to become the value of icon attribute of custom-link I set:

[custom-link url="/someurl" color="#092F87" icon="paypal_icon"]Donate[/custom-link]

So after that the return should be like this:

<a class="button-round custom-link" style="background-color: #' . $color . ' !important" href="' . $url . '" target="_blank">' . $icon_paypal . '<span>' . $custom_title . '</span></a>

...and then executed into a proper HTML code.

I'm a beginner in PHP, hopefully I explained my issue as exhaustively as I could.

Share Improve this question asked Mar 17, 2019 at 12:08 SLembasSLembas 358 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 1

I want the $icon variable to become the value of icon attribute of custom-link I set:

If I understood your question correctly, this is what you want,

function custom_link($atts, $custom_title)
    {
    include "PATH_TO_YOUR_PHP_FILE_CONTAINING_ICON_VARS";
    $url = $atts['url'];
    $color = $atts['color'];
    $icon = $atts['icon'];
    $icon = $$icon; // $$icon is $icon_paypal 
        return '<a class="button-round custom-link" style="background-color: ' . $color . ' !important;" href="' . $url . '" target="_blank">' . $icon . '<span>' . $custom_title . '</span></a>';

    }

add_shortcode('custom-link', 'custom_link');

So a shortcode

[custom-link url="/someurl" color="#092F87" icon="icon_paypal"]Donate[/custom-link]

will generate

<a class="button-round custom-link" style="background-color: ' . $color . ' !important" href="' . $url . '" target="_blank">' . $icon_paypal . '<span>' . $custom_title . '</span></a>
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748799011a313812.html

最新回复(0)