0

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:

enter image description here

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.

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>
  • 1
    Works just like I wanted! Thanks. – SLembas Mar 17 at 12:55
  • @SLembas, Stay blessed! – Qaisar Feroz Mar 17 at 12:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.