Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2017-03-28 15:23:55

richtestani
Plugin Author
Registered: 2009-11-08
Posts: 128

Handling head_admin

I have a couple of plugins where I set custom CSS & JavaScript, but they are loaded no matter which plugin is loaded, and start to cause issues.

A common initialization for plugin callbacks are this:

if(@txpinterface == 'admin') {
	add_privs('rlt_checkout_upsell', '1,2');
	register_tab('extensions', 'rlt_checkout_upsell', 'Checkout Upsell');
	register_callback('rlt_checkout_upsell_main', 'rlt_checkout_upsell');
	register_callback( 'rlt_checkout_upsell_css', 'admin_side', 'head_end');
	register_callback( 'rlt_checkout_upsell_js', 'admin_side', 'body_end');
}

In this situation, head_end is called for every plugin even when you are not on the plugin page.
Is there a way to check in the call back to see if I am on ‘this’ plugin?

Thanks
Rich

Last edited by richtestani (2017-03-28 15:25:13)

Offline

#2 2017-03-28 15:57:08

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 8,785
Website

Re: Handling head_admin

It is annoying, yes. We ought to be able to target the panes more intelligently but don’t currently have enough hook parameters. For now, the most reliable method is to import global $event un your callbakc and test that value to check it’s the one that matches your plugin’s name.

EDIT: Note that to do that you usually alter the function signature to not use $event but some other variable name like $evt to avoid clashes with the global:

register_callback('rlt_checkout_upsell_css', 'admin_side', 'head_end');
...
function rlt_checkout_upsell_css($evt, $stp)
{
    global $event;

    if ($event === 'rlt_checkout') {
        echo "yay, it's for me!";
    }
}

Last edited by Bloke (2017-03-28 16:00:12)


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#3 2017-03-28 16:21:35

richtestani
Plugin Author
Registered: 2009-11-08
Posts: 128

Re: Handling head_admin

Thank you @Bloke!

I toying with a similar approach, but it kept getting overwritten on subsequent plugins, so my approach was wrong. I was defining a variable $plugin_name which changed for each plugin, and the last one loaded wins.

Offline

Board footer

Powered by FluxBB