Forum

Thread tagged as: Api, Add-on-development, Third-party

function in admin.php breaking dashboard in custom App

Hi,

I made an Admin Styling App for Perch (GitHub), which works great except when the "dashboard" is enabled in the admin Settings.

It seems like if I add any kind of function to the admin.php in the custom App, the dashboard breaks.

Am I doing something wrong?

Here is the full code in admin.php in the Admin Style app.

<?php
    $this->register_app('jaygeorge_perch_admin_style', 'Admin Style', 1, 'An app that styles the Perch admin', 1.0);
    $this->require_version('jaygeorge_perch_admin_style', '2.8');
    $this->add_setting('jaygeorge_perch_admin_style_external_font_stylesheet', 'External Font Stylesheet', 'text', false,'','This is an optional stylesheet include for loading something like Typekit or Google Fonts e.g. https://use.typekit.net/dsl0pss.css');
    $this->add_create_page('jaygeorge_perch_admin_style', 'edit');

    $API = new PerchAPI(1.0, 'jaygeorge_perch_admin_style');

    function get_external_font_stylesheet() { 
        $API = new PerchAPI(1.0, 'jaygeorge_perch_admin_style');
        $external_font_stylesheet = $API->get('Settings')->get('jaygeorge_perch_admin_style_external_font_stylesheet')->val();
        return $external_font_stylesheet;
    }

    $Perch = Perch::fetch();
    // We need to separate variables.css out so that the login.css can make use of it (which is loaded outside the app, so it can be available before the admin is loaded)
    $Perch->add_css($API->app_path() . '/variables.css');
    $Perch->add_css($API->app_path() . '/standard-admin.css');
    $Perch->add_css('/perch/addons/plugins/ui/custom-admin.css');
    $Perch->add_css(get_external_font_stylesheet());
    $Perch->add_head_content('<link rel="shortcut icon" href="/perch/addons/plugins/ui/favicon.ico">');
Jay George

Jay George 2 points

  • 3 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

How does it break?

Hi sorry for the late reply—I didn't get a notification that you had replied; I only noticed because I randomly checked the forum.

The error I'm getting is just a 500 blank page error whenever I add a function to admin.php and enable the dashboard.

Here's a 1 min video showing this happening.

Rachel Andrew

Rachel Andrew 394 points
Perch Support

What's the actual error in your error log?

Thanks for the quick reply.

I found this in the php error log:

PHP Fatal error:  Cannot redeclare test() (previously declared in /Users/jaygeorge/Sites/jaygeorge.co.uk/perch/addons/apps/jaygeorge_perch_admin_style/admin.php:15) in /Users/jaygeorge/Sites/jaygeorge.co.uk/perch/addons/apps/jaygeorge_perch_admin_style/admin.php on line 15

…which suggests that maybe it's trying to call the admin.php file twice?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Your dashboard file needs to return a callable, which when called returns the HTML output of your widget.

I'd suggest a format like:

<?php
return function(){
    // your code here
    return '<widget html />';
};

I don't have a dashboard.php file in the app I developed :-/

Do I need one?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Throw an empty placeholder file in there.

Hmm yeah that's not making any difference unfortunately, still the same errors coming from admin.php.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Are you declaring functions in your admin.php file?

Yes I am.

So I am adding an external font stylesheet in the admin.php with a function as follows:

$this->add_setting('jaygeorge_perch_admin_style_external_font_stylesheet', 'External Font Stylesheet', 'text', false,'','This is an optional stylesheet include for loading something like Typekit or Google Fonts e.g. https://use.typekit.net/dsl0pss.css');

return function get_external_font_stylesheet() { 
    $API = new PerchAPI(1.0, 'jaygeorge_perch_admin_style');
    $external_font_stylesheet = $API->get('Settings')->get('jaygeorge_perch_admin_style_external_font_stylesheet')->val();
    return $external_font_stylesheet;
}

$Perch = Perch::fetch();
$Perch->add_css(get_external_font_stylesheet());

but even just function test() {} will break the dashboard.

It works great in general throughout the admin…except in the dashboard view, where the page breaks.

Drew McLellan

Drew McLellan 2638 points
Perch Support

I would do this:

$API   = new PerchAPI(1.0, 'jaygeorge_perch_admin_style'); 
$Perch = Perch::fetch(); 
$Perch->add_css($API->get('Settings')->get('jaygeorge_perch_admin_style_external_font_stylesheet')->val());

Unless you're using that global function elsewhere, there's no need for it. There's also no need to return the function declaration - I'm surprised that works at all.

That worked great, thank you