$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Turning variable into a value set in shortcode|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Turning variable into a value set in shortcode

matteradmin9PV0评论

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>

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far