[SOLVED] Functions.php example

Post Reply
coldheartg3
Posts: 3
Joined: Wed Aug 09, 2017 9:09 am

[SOLVED] Functions.php example

Post by coldheartg3 »

Hi everyone,
Just started to learn WonderCMS, and very happy so far.
I'm building a one-page website, and I need multiple editable block.
On the Github page:https://github.com/robiso/wondercms/wik ... s.php-file functions.php is mentioned as the preferred way to add more editable areas. But that's all information there is.
Does someone have an example functions.php that i can study to see how this is used ? None of the available themes has a functions.php, so I really do not know how to get started on this.
Thanks in advance !
User avatar
wiz
Posts: 749
Joined: Sat Oct 30, 2010 12:23 am

Re: Functions.php example

Post by wiz »

Hi coldheartg3, welcome to the community.

To save yourself some trouble, you can use a plugin for creating additional contents:
https://github.com/robiso/wondercms-plu ... l-contents

If you would like to do it manually, this requires creating functions.php in your theme folder, here's an older example of how we used to do it (might need some editing).

Code: Select all

// REMOVED - CHECK NEXT POST FOR CORRECT CODE
I reckon it would be easier to use a plugin (as seen in our demo https://wondercms.com/demo).
coldheartg3
Posts: 3
Joined: Wed Aug 09, 2017 9:09 am

Re: Functions.php example

Post by coldheartg3 »

It seems with the plugin I can only add new blocks within the 'content' section defined by <?=wCMS::page('content')?>.
Or am I missing something ?

I have different sections in my one-pager, i should be able to edit them all.
Isn't working with functions.php not easier in that case ?

I now added functions.php with the code you supplied to my theme folder, and added <?=wCMS::block('newEditableArea')?> to my theme.php, but there is no editable area where <?=wCMS::block('newEditableArea')?> is placed...
User avatar
wiz
Posts: 749
Joined: Sat Oct 30, 2010 12:23 am

Re: Functions.php example

Post by wiz »

You are correct.

Got this working again, here's the revamped code:

Code: Select all

function newEditableArea() {
    // Check if the newEditableArea area is already exists, if not, create it.
    if (empty(wCMS::get('blocks','newEditableArea'))) {
        wCMS::set('blocks','newEditableArea', 'content', '<h4>test!</h4><p>This is an example of another editable subside area.</p>');
    }

    // Fetch the value of the newEditableArea from the database.
    $value = wCMS::get('blocks','newEditableArea','content');
    // If value is empty, let's put something in it by default. (IMPORTANT)
    if (empty($value)) {
        $value = 'Empty content.';
    }
    // It's necessary to pass the editable method to the fetched value/content
    // to make the area editable ONLY if admin is logged in.
    if (wCMS::$loggedIn) {
        // If logged in, it must be editable...
        return wCMS::block('newEditableArea');
    }
    // If not logged in, don't make it editable!
    return $value;
}
In your theme.php, you are not going to call <?=wCMS::block('newEditableArea')?>, as we just created a function. So in your theme.php, call the function we've created:

Code: Select all

 <?=newEditablearea()?>
Or with some styling:

Code: Select all

	<div class="container-fluid blueBackground whiteFont">
		<div class="row">
			<div class="col-lg-12 text-center padding40">
                                <?=newEditablearea()?>

			</div>
		</div>
	</div>
Let me know how this works for you.
coldheartg3
Posts: 3
Joined: Wed Aug 09, 2017 9:09 am

Re: Functions.php example

Post by coldheartg3 »

Yes, this works for me. Thanks for the advice and the help!

If some developer with lots of time on his hands would like tot dig into this: it would be very nice if there was a plugin that would just index all <?=wCMS::block('...')?> blocks in theme.php and make them editable on the fly ;) ;)
User avatar
wiz
Posts: 749
Joined: Sat Oct 30, 2010 12:23 am

Re: [SOLVED] Functions.php example

Post by wiz »

You're welcome, marking thread as solved. Will add this as an example to our wiki. (Edit: Added to our wiki: https://github.com/robiso/wondercms/wik ... able-areas)
Good suggestion and thank you for participating.

Stay tuned for an awesome upcoming update!
bmherard
Posts: 6
Joined: Thu Jan 16, 2020 8:57 pm

Re: Functions.php example

Post by bmherard »

wiz wrote: Wed Aug 09, 2017 3:41 pmLet me know how this works for you.
I have a working instance of WonderCMS 3.0.4 but can't get the functions example for this version (that is in Docs GitHub pages) to work. I don't want to use the plugin for additional content as I want my editable area to be somewhere other than under the usual content areas.

The editable blocks function will show at the very top of all pages if I omit opening and closing tags <?php ... ?> from the functions.php file (see below). It's wrong but I at least know the theme is recognizing the file. When I include <?php ... ?> the editable area doesn't display.

Code: Select all

function newEditableArea() { // Check if the newEditableArea area is already exists, if not, create it if (empty($Wcms->get('blocks','newEditableArea'))) { $Wcms->set('blocks','newEditableArea', 'content', 'Your content here.'); } // Fetch the value of the newEditableArea from database $value = $Wcms->get('blocks','newEditableArea','content'); // If value is empty, let's put something in it by default if (empty($value)) { $value = 'Empty content'; } if ($Wcms->loggedIn) { // If logged in, return block in editable mode return $Wcms->block('newEditableArea'); } // If not logged in, return block in non-editable mode return $value; }
Using <?=newEditableArea()?> in the theme disables editing anything and removes the remaining content below it entirely.

Using <?= $Wcms->block('newEditableArea')?> just doesn't display the editable area but the page works otherwise.

Any insight into this is appreciated.
User avatar
wiz
Posts: 749
Joined: Sat Oct 30, 2010 12:23 am

Re: [SOLVED] Functions.php example

Post by wiz »

I'll try this out today. Honestly, I haven't tested this with 3.0.x yet

Will report back shortly.
User avatar
wiz
Posts: 749
Joined: Sat Oct 30, 2010 12:23 am

Re: [SOLVED] Functions.php example

Post by wiz »

I've updated the documentation with clearer instructions and the missing global variable: https://github.com/robiso/wondercms/wik ... ble-blocks

The issue was missing

Code: Select all

 global $Wcms
, which was introduced with WonderCMS 3.0.0.

Let me know if this works for you.
Post Reply