Call to undefined function sage()

Hello. I’m attempting to follow along in the book about learning Sage, when I get to the portion that says:

You can use sage('blade')->share to pass data to all views:
add_action('the_post', function() {
    sage('blade')->share('links', [
        'facebook' => 'https://facebook.com/rootswp',
        'twitter' => 'https://twitter.com/rootswp'
    ]);
});
Now from within any Blade template you can use {{ $links['facebook'] }} and {{ $links['twitter'] }} to output the data.

But when I attempt this, I get a error on the site:

Fatal error: Uncaught Error: Call to undefined function sage() in /app/wp-content/plugins/retro-game-plugin/retro-game-plugin.php:102 Stack trace: #0 /app/wp-includes/class-wp-hook.php(305): {closure}(Object(WP_Post)) #1 /app/wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters('', Array) #2 /app/wp-includes/plugin.php(518): WP_Hook->do_action(Array) #3 /app/wp-includes/class-wp-query.php(4322): do_action_ref_array('the_post', Array) #4 /app/wp-includes/class-wp-query.php(3330): WP_Query->setup_postdata(Object(WP_Post)) #5 /app/wp-includes/query.php(979): WP_Query->the_post() #6 /app/wp-content/cache/acorn/views/a4f9acce88f0e766bfda7155ce6bdcc8679f2585.php(4): the_post() #7 /app/wp-content/themes/sage/vendor/illuminate/filesystem/Filesystem.php(107): require('/app/wp-content...') #8 /app/wp-content/themes/sage/vendor/illuminate/filesystem/Filesystem.php(108): Illuminate\Filesystem\Filesystem::Illuminate\Filesystem\{closure}() #9 /app/wp-content/themes/sage/vendor/illuminate/view/Engines/PhpEngine.php(58): Illuminate in /app/wp-content/plugins/retro-game-plugin/retro-game-plugin.php on line 102 

Is this a depreciated function or does it not work with Sage 10?

Thanks for buying the book! It hasn’t yet been updated for Sage 10, and that specific function isn’t available outside of Sage 9.

The recommended way of passing data to views with Sage 10 is by using view composers. Take a look at:

Okie dokie thanks for the answer. I bought it probably a year ago but just finally getting to it. Great timing I guess lol.

I tried out the View Composers, but receive a weird error, unless I’m missing something. So, following that blog article, I attempted to make a view composer but it doesn’t actually pass any data. Instead I receive this error:

Warning: Declaration of App\Composers\Header::with($data, $view) should be compatible with Roots\Acorn\View\Composer::with() in /app/wp-content/themes/sage/app/View/Composers/Header.php on line 13 

This is the code in my view composer:

<?php

namespace App\Composers;
use Roots\Acorn\View\Composer;

class Header extends Composer
{
    public $post;
    protected static $views = [
        'partials.header',
    ];

    public function with($data, $view)
    {
        return [
            'img_url' => "https://upload.wikimedia.org/wikipedia/commons/thumb/0/07/Dreamcast-Console-Set.png/2880px-Dreamcast-Console-Set.png",
            'class' => 'hero',
            'title' => $post->post_title,
        ];
    }
}

I changed img_url to a actual url to see if I can get that to work but no dice either. PHPStorm produces a error in the with($data, $view) function under data with:

Declaration must be compatible with Composer->with() 

and the same with $post below it. Am I missing a declaration somehweres? Thank you.

I solved this; so you can’t just make the Composer file; you have to initiate it with WP CLI, otherwise it won’t work (as seen above). You also need to remove $data and $view from the function parameters or else it won’t work. It seems like that blog post was written with a older version of Sage 10?

1 Like

Sorry, I should have linked this instead of the blog post!

This topic was automatically closed after 42 days. New replies are no longer allowed.