Shortcode Exec PHP
[resolved] Get variables (not push) via Shortcode (5 posts)

  1. Sebastian
    Posted 5 years ago #

    Is there a possibility to get a variable I've stored in the php-plugin? Like [shortcode arg="value"] ...but vice-versa. For example: I have a shortcode-function called [example]
    and the following php-script:

    $variable1 = "text1";
    $variable2 = "text2";
    return $variable1 ." ". $variable2;

    No problem, if I call the [example]-Shortcode, it will write "text1 text2" - BUT I'd like to have only the first variable.

    The reason of my question is simple: I do use different shortcodes for those who are guests, and those whoe are loggedin. Most of the php-script is the same and I have many (!) php-scripts, so it's better not to duplicate the whole scripts and styling them every time again. I hope you got my ugly english :-D


  2. M66B
    Plugin Contributor

    Posted 5 years ago #

    I am not sure what you want, but you can pass arguments to a shortcode, take a look in the WordPress codex for how to. It is also possible to store things, take a look at the update_option function.

  3. Sebastian
    Posted 5 years ago #

    Sorry, I'm not quite familiar with it and have problems to understand it. Hopefully you'll get it now ;) Let me do an example: In the article I write e.g. (I know, it's the wrong code):

    [shortcode getthisvariableandpublish--->>>variable1]

    In the shortcode exec php-plugin I've added the shortcode called shortcode with the variable $variable1. There are other variables, so I cannot return them. I want just ONE variable... the $variable1.

    The article should now proceed the text, which is included in the $variable1.

  4. M66B
    Plugin Contributor

    Posted 5 years ago #

    I think you want something like this:

    extract(shortcode_atts(array('text' => 'default'), $atts));
    if ($text == 'a') return 'Text A';
    else return 'Text B';

    [shortcode text="a"] -> 'Text A'

    [shortcode text="b"] -> 'Text B'

  5. Sebastian
    Posted 5 years ago #

    Wonderful! :-)

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic