WebsiteBaker Community Forum

WebsiteBaker Support (2.8.x) => Droplets & Snippets => Topic started by: crnogorac081 on April 16, 2011, 04:53:57 PM

Title: Does snippets work in backend ?
Post by: crnogorac081 on April 16, 2011, 04:53:57 PM
Hi,

are snippets loaded to backend or is there a way to load it manualy? I get error: Call to undefined function ...

cheers
Title: Re: Does snippets work in backend ?
Post by: Stefek on April 16, 2011, 05:00:36 PM
Hello Ivan,

no they don't.

If you want to use a specific function of a snippet in Backend, you will need to include it into your php-script.

Like
Code: [Select]
<?php
$my_snippet 
WB_PATH.&#39;/modules/my_snippets_directory/include.php&#39;;
if(file_exists($my_snippet){
 require_once(
$my_snippet);
}

Kind regards,
Stefek
Title: Re: Does snippets work in backend ?
Post by: crnogorac081 on April 21, 2011, 02:56:04 PM
Cool, but have another problem..

For example if I have this lame function:
Code: [Select]
function thisFunction($user) {
global $database, $wb;
...
$to_user = $wb->add_slashes($user);
return $to_user;
}
In snippet I have $wb->add_slashes($something); and when I use it in backend I get: Fatal error: Call to a member function add_slashes() on a non-object in ---- line where I use $wb->add_slashes

How can I redeclare snippet to use $admin in backend instead $wb ???

Or is there another way that same function works in FE and BE

cheers
Title: Re: Does snippets work in backend ?
Post by: Stefek on April 21, 2011, 04:02:03 PM
Hello Ivan,

I only have an Idea on how you could make it, but I am sure that there is a better approach to this than mine.

What about writing a second function for the backend instead of using the same in both places (FE/BE)?

Regards,
Stefek


Title: Re: Does snippets work in backend ?
Post by: BlackBird on April 21, 2011, 04:07:10 PM
Before including the snippet, try to add this to your code:

Code: [Select]
global $wb;
if ( ! is_object($wb) ) {
  $wb =& $admin;
}

If this doesn't work, instantiate $wb yourself.

Code: [Select]
if ( ! is_object($wb) ) {
  require_once(WB_PATH.'/framework/class.frontend.php');
  $wb = new frontend();
}

Title: Re: Does snippets work in backend ?
Post by: crnogorac081 on April 21, 2011, 09:03:44 PM
It works fine in frontend, but backend is the problem.. I use $wb->add_slashes($something);  in frontend to sanitize values, but problem appears when I use function in BE.. tbecause then all $wb->SOMETHING needs to be replaced with $admin->SOMETHING
Title: Re: Does snippets work in backend ?
Post by: BlackBird on April 23, 2011, 02:35:32 PM
The code I posted should solve this.
Title: Re: Does snippets work in backend ?
Post by: crnogorac081 on April 24, 2011, 09:17:27 PM
whohoo it works :)

thanks BB !!