Create own plugin with shortcode

Hi folks,

i created a simple php script which reads a json file.
I searched now for a way to implement my code in a grav page (quark-theme used).

If i read it correctly its not possible to run own php code for security reasons - instead i should create a own plugin.
is it really necessary to write an own plugin to display a simple php-script?

isn’t there an easier way to include the php-script and (if possible) to include it with a shortcode-variable in the sidebar?

kind regards
keks

@keks, I see no reason to doubt the answer given by the lead designer of Grav… :wink:

  • You will have to create a plugin to execute your own PHP logic to read the json file.
  • If I understand you correctly, to show the content of the json file in the sidebar of a page, the following is needed:
    • The plugin also needs to make the data available to Twig.
    • In the Twig template that lays out the sidebar, the data can be formatted and added to the sidebar of the page.

Hope this helps…

Only extensions can run PHP; you’ll need to bootstrap the script for a theme or plugin. With the DevTools-plugin and CLI, it takes less than 30 seconds to create one. The default plugin-template creates a variable sent to Twig, you can change that to load your JSON, so you can iterate over it.

okay, get the plugin now and has modified the twig-template.
but iam unable to test it, because i can’t include the plugin-content in a site.

therefore i must create a shortcode or is there any other way?

@OleVik, Correct me if I’m wrong, but I’m afraid the functionality of a plugin created by the ‘devtools’ plugin does not provide the functionality as you describe.

It also has been restructured quite a bit recently. This is the new PHP being created: https://github.com/getgrav/grav-plugin-devtools/blob/develop/components/plugin/blank/plugin.php.twig

@keks, Would you mind sharing your relevant coding skills (e.g. php, twig, cms)? That will allow the community to adjust the answer to your level of coding experience.

Also, would you mind sharing what you have got and done so far?

To show what you have created, you could run the OS command $ tree | clip (Windows) or $ tree | clip.exe (Linux) inside your plugin folder and paste it in your answer between two lines containing triple back-ticks (```).

1 Like

In regards to the quickest way to “display a simple php-script”, using the DevTools-template is pretty quick. If the sidebar is rendered with Twig, which is almost certain, handling the JSON that way is likely easier than creating a shortcode.

As pamtbaau writes though, it’ll be easier to write a pertinent answer if you share your experience-level. Shortcodes aren’t hard to create, but they require more handling than just manipulating data in PHP, then passing and rendering it in Twig.