Dynamic Buttons

Content

There are several ways of importing information from WordPress into a MaxButtons. We usually call those ‘variables’. Some can be done directly into the plugin and require little knowledge, other are intended for advanced users.

Note: MaxButtons already offers many shortcode options to create dynamic buttons. It’s always better to use those if possible.

MaxButtons uses the same mechanism for WordPress options, query parameters and global and will look for them in this order. This means if the plugin will find a URL parameter before your global, it will use the first and ignore the latter.

The variable option in MaxButtons

You can add variables to the text, text2 and URL fields. A variable is denoted by being between two sets of pipe symbols ( || ) . In this example, the variable is called permalink and is linked to the WordPress get_permalink function.

There are a few predefined options you can directly use. These values mostly come from WordPress. For most variable it’s important where in your content the button is loaded, since the content of the information can be different. Please read about the WordPress Loop to prevent any confusion.

  • pageurl – will get the URL of the main page.
  • permalink – will get the URL of the current loop item. This can be the same as pageurl
  • nextpost – will get the URL of the next post of the same category / post type
  • prevpost – will get the URL of the previous post of the same category / post type
  • Using Information from the URL (query parameter)

    The query parameters part of the URL is what comes after the domain and page. We will take this example:

    https://example.com/page/?my-title=Hello

    In the MaxButtons editor you can use the variable mechanism to target these values by simply including them in the pipe symbols : ||my-title||

    If the query parameter is not present and there is no variable defined by that name, the result will just be left blank.

    Using a defined variable from your page (global)

    Thirdly, the variable mechanism looks at the defined variables on your current scope. This sounds a bit technical, but simply means any variable defined when the button shortcode is being executed.

    Make sure that the variable name is not in used by any of the previous steps. Be aware that referencing the wrong variables can lead to page crashes or potential unsafe behavior.

    Order and double use of variables

    The order here is important. MaxButtons wil first look for the reserved functions : pageurl, permalink, nextpo, prevpost. If those are not found, then it will look for the variable in the Query Parameters ( described up ). If nothing is found there, it will check if the value can be found in your page scope.

    If nothing is found, the value will remain empty.

    Using a variable name that is present in one or more of the options might result in some unexpected behavior, so be sure to test all the page well when using variables.

    Using a WordPress filter hook

    All variable content goes through a filter hook called ‘maxbuttons/parse/variable’. This allows even more freedom like custom functions to use variables in MaxButtons.

    Example: Content is the name of the variable, result is what will be included on the button output.

    <?php add_filter('maxbuttons/parse/variable', function($result, $content) {
        if ($content == 'myvariable')
        {
            $result = get_the_title(); 
        }
        return $result; 
    }, 10,2 ); 
    ?>

    Using a PHP snippet to load MaxButtons

    You can load MaxButtons via PHP. This can be useful for complicated functions, or when including MaxButtons in your theme template.

    <?php
     echo do_shortcode(''); 
    ?>

    Using the button name will make it easier to understand your code, or switch the button design without having to update the ID, both can be used however.

    You can create dynamic content by using other shortcode attributes like text, url and combining them with your own code / variables.

    What you should not do

    Using variables can pose a risk to your site security. MaxButtons validates the output to be safe to print, but can’t guarantee any end results, especially when using shortcodes, or other functions. Using hooks, snippets etc is at own risk

  • Do not use functions that return objects, classes, or other complicated types.
  • Do not use function that return HTML code. This will most likely break the button .
  • Do not use unknown variables, unchecked variables.
  • Do not use Query Parameters for anything important. It’s easy to change by any visitor.
  • Other Documentation Topics