When using tokens, I would like to be able to use a format similar to the following notation so I can provide the needed flexibility in how a content author can invoke these modals with minimal code experience.


An example of this token in use would be:

For more information, [modal:view:33:"click here"].

What should happen from here is that I can grab the <term_id> and <text_string> and incorporate them in the output of the token replacement.

An example for the expected output would be something along the lines of:

For more information, <a href="#" data-modal-id="33">click here</a>.

Is this possible, and if so, how would I construct the hook_tokens function to handle these dynamic values?

  • 1
    Yep that's definitely possible, the token system can be extended to do whatever you need it to. It wouldn't be trivial though, unless you can find an existing solution for your exact use cases – Clive May 31 '17 at 14:53
  • Given a token like [modal:view:33:"click here"], what would the output you want? – apaderno May 31 '17 at 16:06
  • @kiamlaluno The output would be something like <a href="#" data-modal-id="33">click here</a> as a simple example. – MLK.DEV May 31 '17 at 16:26

Might have worked myself into the solution via trial-and-error.

function hook_tokens( $type, $tokens, array $data = array(), array $options = array() ) {

    // Returnable markup...
    $replacements = array();

    // Handle Tokens...
    if( $type == 'modal' ) :
        foreach( $tokens as $name => $original ) :

            $name_parts = explode( ':', $name );
            switch( $name_parts[ 0 ] ) {

                case 'view':

                    // Handle modal:view tokens...
                    if( !empty( $name_parts[ 1 ] ) && !empty( $name_parts[ 2 ] ) ) :
                        $value =
                        '<a href="#" data-modal-id="'.$name_parts[ 1 ].'">'.
                            '<span>'.trim( trim( $name_parts[ 2 ], '"' ), "'" ).'</span>'.
                        $replacements[ $original ] = $value;




    return $replacements;


There's a lot of fail-safes missing from this code, but I just want to make sure this falls under best practice and that I'm not doing anything that would be considered taboo by community standards.

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.