Use PHP in entries

This is the right place to report general bugs, make suggestions or ask for help.
Tongara
Posts: 58
Joined: Wed Jan 22, 2020 11:09 am
Location: Birmingham, UK
Contact:

Use PHP in entries

Post by Tongara » Sat Jan 23, 2021 11:10 am

Edit Arvid: This used to be an answer on the topic "include other content". Since this is a very interesting question for itself, I felt free to create this new topic with it.

A question... is it actually possible to use PHP in flatpress entries and static pages? There are certain things I'd love to be able to do, but after testing I'm assuming it's simply not possible given the pages are rendered using .TXT files.

Any words of wisdom on such? :mrgreen:

User avatar
Arvid
FlatPress Coder
Posts: 166
Joined: Sat Jan 26, 2019 7:40 pm
Contact:

Re: Use PHP in entries

Post by Arvid » Sat Jan 23, 2021 11:53 am

This is a very interesting question.

I would not bother searching for a direct answer, though. Because this is exactly what the plugins are for :)

Once familiar with their neccessary programming interfaces, you can freely create any PHP logic, let's say, a random number generator. Make sure your plugin also adds a new BBCode element ("randomnumber"). Use this element to display your plugin output in entries and static pages:

Code: Select all

[randomnumber]
...this may be displayed as "23" on your page, and after refreshing, as "87".
And if you need it more dynamically (say, you want to specify the range the generated number has to be in), use BBCode parameters:

Code: Select all

[randomnumber min=10 max=30]
... to make sure the displayed number is always between 10 and 30.
Of course, the plugin code must read from this parameters and use their values in its logic. But this is not difficult to do, you can learn that quickly from existing plugins that have own BBCode elements. Take a look into my audio and video player plugin for example! :)

All the best,
Arvid
🔥 Please help us beta-testing FlatPress 1.2 "Legato"! 🔥


Helpful FlatPress links: Project blog 📝 - FlatPress wiki 📖 - FlatPress@Twitter 🐦
Stay up to date with our feeds: Forum RSS feed 📜 - Project blog RSS feed 📜

Tongara
Posts: 58
Joined: Wed Jan 22, 2020 11:09 am
Location: Birmingham, UK
Contact:

Re: Use PHP in entries

Post by Tongara » Sat Jan 23, 2021 4:01 pm

Arvid wrote: Sat Jan 23, 2021 11:53 am This is a very interesting question.

I would not bother searching for a direct answer, though. Because this is exactly what the plugins are for :)

Once familiar with their neccessary programming interfaces, you can freely create any PHP logic, let's say, a random number generator. Make sure your plugin also adds a new BBCode element ("randomnumber"). Use this element to display your plugin output in entries and static pages:

Code: Select all

[randomnumber]
...this may be displayed as "23" on your page, and after refreshing, as "87".
And if you need it more dynamically (say, you want to specify the range the generated number has to be in), use BBCode parameters:

Code: Select all

[randomnumber min=10 max=30]
... to make sure the displayed number is always between 10 and 30.
Of course, the plugin code must read from this parameters and use their values in its logic. But this is not difficult to do, you can learn that quickly from existing plugins that have own BBCode elements. Take a look into my audio and video player plugin for example! :)

All the best,
Arvid
Ah yes, If you remember correctly, I've actually made a plugin or two before, so it should have occurred to me right away that this is probably the path I should have chosen, although I do believe I tried this once before to add the Disqus comments system dynamically to my Static pages without much luck.

I will experiment a bit and see if I can do what I want to with this method. Thanks for answering this one so thoroughly, as it's something that could absolutely help many others in the future I think! Very much worth it's own thread~ :)

User avatar
Arvid
FlatPress Coder
Posts: 166
Joined: Sat Jan 26, 2019 7:40 pm
Contact:

Re: Use PHP in entries

Post by Arvid » Sat Feb 13, 2021 7:28 pm

Tongara wrote: Sat Jan 23, 2021 4:01 pm Ah yes, If you remember correctly, I've actually made a plugin or two before
I, err, was totally aware of that when I wrote my answer ;) :lol:

But indeed, this topic might be useful for future readers.
Please feel free to ask questions that occur to your on your plugin journey!
🔥 Please help us beta-testing FlatPress 1.2 "Legato"! 🔥


Helpful FlatPress links: Project blog 📝 - FlatPress wiki 📖 - FlatPress@Twitter 🐦
Stay up to date with our feeds: Forum RSS feed 📜 - Project blog RSS feed 📜

Tongara
Posts: 58
Joined: Wed Jan 22, 2020 11:09 am
Location: Birmingham, UK
Contact:

Re: Use PHP in entries

Post by Tongara » Mon Feb 15, 2021 9:03 am

No problem at all, you can't be expected to remember every person and everything! hehe

So, I did try this, but it sadly didn't turn out how I wanted it to. I'll explain the basic idea before sharing any files or code...

With how my website is set up, I wanted the option of adding a custom "Back" link to Static pages much like you see on the main news post pages and index page of flatpress. The idea would be that the static.tpl would contain a line of code for the back button, and it would somehow grab it's code from elsewhere on a static page using a PHP echo so that I could define the target URL on the fly.

So, on the static template the "a href" would work something like this:

Code: Select all

<a href="<?php echo $link;?>">Back</a>
Then of course comes the plugin code, which can be seen in the following file:
https://mega.nz/file/8lgXDSSL#dKqbPuJYp ... v8vjcLJgYY

Then based on the code above, I define the actual link on a Static page in the following way:

Code: Select all

[pagelinks]pagename[/pagelinks]
Sadly, the URL never gets defined, and as such the URL ends up as "mysite.com/<?php%20echo%20$link;?>" instead of "mysite.com/pagename".

I've made sure the plugin is enabled, and I can see in the site's header that the plugin is loaded on the page. I'm sure I'm just missing something really small and I'm going to feel like an idiot when it's pointed out what I'm doing wrong, but the forums are here to help support us, so I'm looking forward to feel foolish! haha

Maybe there is a better way than using PHP for this task? I'm open to ideas!

Lubomír Ludvík
Posts: 22
Joined: Wed Feb 20, 2019 7:44 pm
Location: Rouchovany
Contact:

Re: Use PHP in entries

Post by Lubomír Ludvík » Mon Feb 15, 2021 2:09 pm

plugin inlinephp
http://flatpress.cz/flatpress/fp-plugin ... inephp.zip

Code: Select all

[exec]echo(rand(10,100));
echo PHP_VERSION;[/exec]

Tongara
Posts: 58
Joined: Wed Jan 22, 2020 11:09 am
Location: Birmingham, UK
Contact:

Re: Use PHP in entries

Post by Tongara » Mon Feb 15, 2021 8:54 pm

Lubomír Ludvík wrote: Mon Feb 15, 2021 2:09 pm plugin inlinephp
http://flatpress.cz/flatpress/fp-plugin ... inephp.zip

Code: Select all

[exec]echo(rand(10,100));
echo PHP_VERSION;[/exec]
Sadly this doesn't fix my issue, but it's an absolute game changer when it comes to using PHP in all kinds of entries!

Lubomír Ludvík
Posts: 22
Joined: Wed Feb 20, 2019 7:44 pm
Location: Rouchovany
Contact:

Re: Use PHP in entries

Post by Lubomír Ludvík » Tue Feb 16, 2021 5:24 am

YES, you can run file /fp-plugins/locale.php

Code: Select all

[exec]require_once('../locale.php');[/exec]

Tongara
Posts: 58
Joined: Wed Jan 22, 2020 11:09 am
Location: Birmingham, UK
Contact:

Re: Use PHP in entries

Post by Tongara » Tue Feb 16, 2021 5:53 am

Lubomír Ludvík wrote: Tue Feb 16, 2021 5:24 am YES, you can run file /fp-plugins/locale.php

Code: Select all

[exec]require_once('../locale.php');[/exec]
Okay, good to know.

Anyway, still trying to fix my issue, so if anyone has any idea as requested in one of my last posts, I'd appreciate it. Like I said, I'm open to ideas, and I'm sure I'm just missing something small here. I've tried to be as detailed as possible. :)

dsteuer
Posts: 29
Joined: Sun May 24, 2020 11:21 am

Re: Use PHP in entries

Post by dsteuer » Tue Feb 16, 2021 9:36 am

Lubomír Ludvík wrote: Mon Feb 15, 2021 2:09 pm plugin inlinephp
http://flatpress.cz/flatpress/fp-plugin ... inephp.zip

Code: Select all

[exec]echo(rand(10,100));
echo PHP_VERSION;[/exec]
Hi, I`ve put it in the wiki. I hope that's is ok? https://wiki.flatpress.org/res:plugins:inlinephp

Arvid: There is a superfluous copy in res:plugins. Looks like only admin can remove files after upload.

Detlef

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests