Create / Use shortcodes in php

5

I wanted to know how I can create shortcodes in pure php (I will not use wordpress and its plugins).

I do not intend to use functions in php for this purpose. I would like to create a structure where I could name a code in php and when call on the page it would return all content previously defined.

For example, in the administrative area of the site it would look like this:

Shortcode name: pt-so

Content of the above mentioned shortcode:

<?php 
   // Aqui poderia usar qualquer coisa no php, usar while, functions, etc.
   echo "<p>Bem-vindo ao Stack Overflow em Português!</p>";
?>

php file

<html>
<head>
    <title>Teste</title>
</head>
<body>
    <!-- Chamando o shortcode -->
    [pt-so]
</body>
</html>

In the php code where the shortcode call is located, it will print the paragraph with welcome. As I will make a system with administrative area, this would be one of the resources that the same system would have. I just want to understand how I can make this call a shortcode on my system / site page that will be visible to the public.

  

Note: I tried to extract the answer in this question, but I could not.

     

Randomize results with PHP

    
asked by anonymous 12.02.2016 / 14:31

3 answers

4

It's not very clear about whether this could be done with a parser in PHP, but if so, here's a simple example.

Example

test.php file

function CompileTemplate($file, $parse)
{
    return str_replace(array_keys($parse), $parse, file_get_contents($file));
}

/*
Nesse array, o relacionamento entre o short code e o seu par.
*/
$parse = array(
    '[pt-so]' => 'Foo bar '.time();
);

echo CompileTemplate('compile-template.html', $parse);

The HTML file. Let's call it compile-template.html .

Note that in this file, there is nothing in PHP as it is elucidated in the question.

<html>
<head>
    <title>Teste</title>
</head>
<body>
    <!-- Chamando o shortcode -->
    [pt-so]
</body>
</html>

To test, just create the two files in the same directory and run teste.php .

Scamming the template

Try to use a PHP code in the template ( compile-template.html ).

<html>
<head>
    <title>Teste</title>
</head>
<body>
    <!-- Chamando o shortcode -->
    [pt-so]
    PHP: <?php echo time();?>
</body>
</html>

See that PHP will not be interpreted. For everything is treated as pure text.

All runs in PHP would be called within the CompileTemplate() function.

Note that the scripts above are purely didactic.

    
23.02.2016 / 12:41
1

As far as I understand, both your "pt-so" shortcode and the file that calls the shortcode are php files from your system. With this in mind, what you are asking is just a way of pasting the codes of your files representing shortcodes (for example "pt-so") into other php files.

Here is an example of how you could do this:

<html>
<head>
    <title>Teste</title>
</head>
<body>
    <!-- Chamando o shortcode -->
    <?php require_once "pt-so.php";?>
</body>
</html>

where "pt-so.php" is the php file that contains your shortcode:

<?php
    // Aqui poderia usar qualquer coisa no php, usar while, functions, etc.
    echo "<p>Bem-vindo ao Stack Overflow em Português!</p>";
?>

In the code above, <?php require_once "pt-so.php";?> corresponds to the shortcode call, that is, it is my version of [pt-so] of Wordpress.

I recognize that compared to how Wordpress does the shortcodes, this seems rather unpleasant, but this is because Wordpress performs a new level of interpretation above its code: it reads its PHP code and generates a new PHP code , which will be used to respond to your users.

    
21.02.2016 / 00:33
-1

Create a case structure in a file called shortcodes.php and include via include at the system / site header.

Example:

<?php
    //sem linguagem é PT
    if (!isset($lang)){
        $lang="pt";
    }

    switch($lang){
        case "pt":

        $langSo = "<p>Bem-vindo ao Stack Overflow em Português!</p>";

    break;

        case "es":

        $langSo = "<p>Bem-vindo ao Stack Overflow em Spanish!</p>";

    break;

    }
?>



<html>
<head>
    <title>Teste</title>

    <?php include('shortcodes.php'); ?>
</head>
<body>
    <!-- Chamando o shortcode -->


    <?php 
    $lang = 'es';

    echo $langSo;  //Bem-vindo ao Stack Overflow em Spanish!

    ?>


</body>
</html>
    
12.02.2016 / 15:08