Custom settings item?

Welcome to the WonderCMS community. Anything goes, except for support requests.
Post Reply
User avatar
StephanStanisic
Posts: 37
Joined: Fri Jul 05, 2019 8:51 pm

Custom settings item?

Post by StephanStanisic »

Just found out about WonderCMS, and I am in already in love with it. It is simple, small and just wonderful.

So I am trying to create a little theme for it, just to get known with WonderCMS (don't expect too much, I am more a backend kind of guy).
I now have a scenario where I need a way to add a custom settings item to add some customization to this theme.

Is this possible with WonderCMS? Or do I need to think of something myself?

Current ideas if not possible native:
  • Load the wCMS::settings() in to a DOMDocument and alter HTML there.
  • Just add it with javascript after pageload (not nice, but really easy).
  • Regex the wCMS::settings() and inject that way (the most ugly and version specific version)
Also, I've read on the WonderCMS website that it is supposed to stay 'light' and 'simple'. So that there are not going to be a large amounts of plugins/themes going to be approved. Are there plans for a theme/ plugin website where non-approved plugins could get listed? Now they are a little everywhere. (maybe a little like wordpress, but not as large or bloated)

Thanks everyone here for making such an awesome little CMS!
User avatar
wiz
Posts: 754
Joined: Sat Oct 30, 2010 12:23 am

Re: Custom settings item?

Post by wiz »

Hi Stephan, a warm welcome to the WonderCMS community - glad you've dropped a visit.

WonderCMS supports listeners and hooks and you could include certain functions of your own and apply them to the settings panel or the menu.

For example:
Here's a theme called "Paper-L" and it uses adding a listener to the menu function: https://github.com/prakai/wcms-paper-l/ ... /theme.php and adds those colorSelectors so the visitor of a website can change the color.

For example, the modern settings plugin you've linked to is partially deprecated for the latest version of WonderCMS, but the logic and solution that this plugin is using is the same as in the example theme above - simply add a listener and insert your function in any of the available ones for WCMS:
- adding a listener to the main css or js (in your theme:

Code: Select all

wCMS::addListener('css', 'yourFunctionName');
or for js

Code: Select all

CMS::addListener('js', 'yourFunctionName');
- footer

Code: Select all

wCMS::addListener('footer', 'yourFunctionName');
- settings panel

Code: Select all

wCMS::addListener('settings', 'yourFunctionName');
- menu

Code: Select all

wCMS::addListener('menu', 'yourFunctionName');
We're still adding themes and plugins to the official repository, we just slowed down because of the upcoming WCMS 3.0.0 release, which will break backwards compatibility. All of the themes and plugins are already migrated and prepared to be released when WCMS 3.0.0 is done.

For now, every plugin and theme that is submitted ends up on the official website. For the future, I guess there could be a dedicated repository where contributors could share their code/plugins/themes that won't be included as an official theme/plugin.

Hope this clears any questions you had. In regards to your theme, you can explain more details of what you're trying to achieve, I could try to help out.
Possibly share any snippets or steps where you're stuck, it would definitely make it easier. :)

P.S. Glad you're enjoying WonderCMS:
- you can also check out the upcoming 3.0.0 version, which will be released in the upcoming months: https://github.com/robiso/wondercms/tre ... ental3.0.0
- also, all of the already approved themes and plugins will be merged into one repository with the upcoming 3.0.0 WonderCMS version (so the approved themes/plugins won't be all over the place anymore
User avatar
StephanStanisic
Posts: 37
Joined: Fri Jul 05, 2019 8:51 pm

Re: Custom settings item?

Post by StephanStanisic »

Hi Wiz,

I've read about the hooks in the documentation. Take this plugin.php for example:

Code: Select all

<?php
function example($args) {
    if(!wCMS::$loggedIn) return $args;
    $args[0] .= "<div>Just a test</div>";
    return $args;
}
wCMS::addListener('settings', 'example');
It just get's appended to the whole settings dialog's HTML. I can't easily add a input field.

Code: Select all

<div id="save"><h2>Saving...</h2></div><div id="adminPanel" class="container-fluid"><div class="text-right padding20"><a data-toggle="modal" class="padding20" href="#settingsModal"><b>Settings</b></a><a href="https://www.wonder.stanisic.nl/logout&token=">Logout</a></div><div class="modal" id="settingsModal"><div class="modal-dialog modal-xl"><div class="modal-content"><div class="modal-header">[...]Themes</a> &nbsp; <a href="https://wondercms.com/plugins" target="_blank">Plugins</a> &nbsp; <a href="https://wondercms.com/community" target="_blank">Community</a> &nbsp; <a href="https://github.com/robiso/wondercms/wiki#wondercms-documentation" target="_blank">Docs</a> &nbsp; <a href="https://wondercms.com/donate" target="_blank">Donate</a></b></p></div></div></div></div></div><div>Just a test</div>
I can't really see other methods to insert HTML somewhere in the middle, other than to load the $args[0] into a DOMDocument and alter the HTML from there.

Some time later:
Just took the time to load the $args[0] into a DOMDocument and tried to add a element to one of the menus. This works, it is just not that pretty and requires the whole #adminPanel to be read into a DOMDocument for each page load. (And it uses a regex to remove the DOCTYPE, html and body. That also not ideal.)

Code: Select all

<?php
function example($args) {
    if(!wCMS::$loggedIn) return $args;

    $doc = new DOMDocument();
    $doc->loadHTML($args[0]);

    $label = $doc->createElement("p");
    $label->setAttribute("class", "subTitle");
    $label->nodeValue = "Custom setting";

    $doc->getElementById("currentPage")->appendChild($label);

    $wrapper = $doc->createElement("div");
    $wrapper->setAttribute("class", "change");

    $input = $doc->createElement("div");
    $input->setAttribute("class", "editText");
    $input->setAttribute("data-target", "pages");
    $input->setAttribute("id", "customSetting");
    $input->nodeValue = "Default value";

    $wrapper->appendChild($input);

    $doc->getElementById("currentPage")->appendChild($wrapper);

    $args[0] = preg_replace('~<(?:!DOCTYPE|/?(?:html|body))[^>]*>\s*~i', '', $doc->saveHTML());
    return $args;
}
wCMS::addListener('settings', 'example');
I am worried what will happen when someone includes two themes that both add custom fields with their own methods. I was hoping that there would be some way to add more admin settings via some hook.

Again thank you for your time.
Post Reply