What is the right way to add a custom function?


i have a function that i would like to use globally in my site:

function word_limit($str, $limit = 100, $end_char = '…')

if i put it in functions.php it worked, i can call it from every file.

but i would like to implement it by “sage” way and put it in lib/utils.php but i couldn’t get the right syntax to make it work.

can you show me how please?


You can:
1 . add it to either extras.php or
2. you duplicate that file and rename it,then you add another line in functions.php to call it. Remember to have just the open php tag at the top of the file <? php .

Utils and other function files have different uses and are mostly used for setting up some things or hde sidebar on pages etc.

thanks for your quick reply,

when i just put the function in extra.php i got an error when i am trying to use it says the function is undefined.

what syntax am i missing? i need to add a filter or maybe to call the function in certain way?


Have you tried using a namespace?

If your function is in extras.php, then at the top of the file you’re trying to use your function in (ie the template file, type:

use Roots\Sage\Extras;

You would then write your function like:


Or if you want to use utils.php then change ‘Extras’ for ‘Utils’


cool, i totally missed the namespace issue…

thanks a lot :grinning: