Create/Use shortcodes in php

Asked

Viewed 603 times

5

I wonder how I can create shortcodes in pure php (I will not use wordpress and your plugins).

I don’t intend to use php functions for this purpose. I’d like to create a structure where I can name a code snippet in php and when I called him on the page he would return all the previously defined content.

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

Name of shortcode: en-so

Contents of the abovementioned 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 welcome paragraph. 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

  • 1

    +1 for not using wordpress, you can do with includes/requires or functions

  • I was even guessing this, but what I want to understand is how that shortcode call on my page will call code definition back in the system.

  • 2

    summarizing, you want to create your own template engine?

  • Look, I think that’s it. Does it get really hard? because I could change, delete such shortcode if I wanted, and of course, I could add as many as I wanted tbm

  • 2

    is a good question, I wouldn’t know how to make php scan all html by looking for the specific shortcotes to carry out the process.

3 answers

4


It’s not very clear whether this could be done with a PHP parser, but if it is, here’s a simple example.

Example

Filing cabinet php test.

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 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 php test..

Mocking the template

Try using 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>

Note that PHP will not be interpreted. Because everything is treated as plain text.

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

Note that the above scripts are purely didactic.

1

From what I understand, both your "en-so" shortcode and the file that calls the shortcode are php files from your system. In view of this, what you are asking for is just a way to paste the codes of your files that represent shortcodes (for example the "en-so"), inside other php files.

Here’s an example of how you could do it:

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

where "en-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 call to the shortcode, IE, is my version of [pt-so] wordpress.

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

-1

Create a case structure in a file called shortcodes.php and include via include in 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>
  • This does not do what I asked. I want the call "[en-so]" to call the php content a defined shortcode

  • I made an example based on need. To my knowledge, shortcode is a defined Wordpress function.

  • so Denis, I use wordpress and know that there are used shortcodes. However this does not sound like "property of wordpress" even because it is possible to do in other languages. As wordpress is done in php and there are used the shortcodes, I chose to use the shortcodes without using wordpress. In this example you are using php commands like "echo" inside the page and that is not what I want.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.