Page 1 of 1

Best place to insert php code?

Posted: Fri Aug 18, 2006 12:28 pm
by Hokey
Hi!

I upgraded to S9Y 1.1beta and have the problem that my counter-codes don't work anymore. I added them in Version 1.0 to the end of the index.php but that doesn't seem to work with 1.1.

Where can I add the counter-code now and where is the best place for non-S9Y php-code in general?

Posted: Fri Aug 18, 2006 1:33 pm
by Harald Weingaertner
I'm not a real expert with Serendipity but have you tried to put the code into index.tpl in your current template folder?

Ah, your counter code is a PHP Code? So then you maybe have to use the "Smarty Parsing" Plugin. But i am not good enough to explain the usage ;)

Posted: Fri Aug 18, 2006 1:37 pm
by garvinhicking
Plus, you should tell us your counter code :-D

Try your template's config.inc.php file. Editing core serendipity files is always not so good. If your templates config.inc.php does not help, you will need to create an event plugin. That's what plugins are for, actually. For not needing to patch code. :-)

Best regards,
Garvin

Posted: Sat Aug 19, 2006 10:35 am
by Hokey
I think a plugin would be the best solution, because I wouldn't have to adapt the code after every S9Y-upgrade.

But: How? E.g. I have this code:

Code: Select all

<?php
$chCounter_visible = 0;
$chCounter_status = 'active';
include( '/home/www/doc/13460/blokey.de/www/chCounter/counter.php' );
?>

Posted: Mon Aug 21, 2006 12:58 pm
by garvinhicking
Hi!

Yeah, you could do that with a plugin listening on the "frontend_configure" hook:

Code: Select all

<?php

class serendipity_event_counter extends serendipity_event {
    function introspect(&$propbag) {
        global $serendipity;
        
        $propbag->add('name',          'counter');
        $propbag->add('description',   '');
        $propbag->add('stackable',     false);
        $propbag->add('author',        'Garvin Hicking');
        $propbag->add('version',       '1.0');
        $propbag->add('requirements',  array(
            'serendipity' => '0.8',
            'php'         => '4.1.0'
        ));
        $propbag->add('event_hooks',    array(
            'frontend_configure'   => true,
        ));
        $propbag->add('groups', array('BACKEND_FEATURES'));
    }

    function event_hook($event, &$bag, &$eventData, $addData = null) {
        global $serendipity;

        $hooks = &$bag->get('event_hooks');
        
        if (isset($hooks[$event])) {
            switch($event) {
                case 'frontend_configure':
                    $chCounter_visible = 0;
                    $chCounter_status = 'active';
                    include('/home/www/doc/13460/blokey.de/www/chCounter/counter.php');
                    return true;
                    break;

                   
                default:
                    return true;
                    break;
            }
        } else {
            return false;
        }
    }
}


Posted: Mon Aug 21, 2006 5:34 pm
by Hokey
Ah, nice! Thank you!

I really don't unserstand anything instead of the previous postet PHP-Code. Can I use this as a template for other counter codes by copy&pasting the other code? Could I also paste Javascript?

Posted: Mon Aug 21, 2006 8:12 pm
by garvinhicking
Hi!

Uh, that code above is a plugin that you should save in your plugins/serendipity_event_counter/ directory as serendipity_event_counter.php and then install it through the s9y plugin interface. :)

It is actually completely unrelated to javascript...

Best regards,
Garvin

Posted: Tue Aug 22, 2006 9:09 am
by Hokey
Hi Garvin!

When I use the above code as plugin then I get the following message in the top-lines of the backoffice:

Code: Select all

Warning: Cannot modify header information - headers already sent by (output started at /home/www/doc/13460/blokey.de/www/chCounter/counter.php:1614) in /home/www/doc/13460/serendipity/serendipity_admin.php on line 11
The output on the front is the same as usual. I tried the same plugin-code (with renamed folder and adapted code) for another counter, which seems to work without problems. The warning doesn't seem to be connected to the second plugin, because it comes also when the second plugin is deactivated.

update:
Ups, the front output is also affected. Clicking links reproduces nearly the same message:

Code: Select all

Warning: Cannot modify header information - headers already sent by (output started at /home/www/doc/13460/blokey.de/www/chCounter/counter.php:1614) in /home/www/doc/13460/serendipity/exit.php on line 29

Warning: Cannot modify header information - headers already sent by (output started at /home/www/doc/13460/blokey.de/www/chCounter/counter.php:1614) in /home/www/doc/13460/serendipity/exit.php on line 30

Posted: Tue Aug 22, 2006 10:18 am
by garvinhicking
Hi!

Well, yes. I don't know anything about your counter, and your counter.php is now making the trouble. Why is it outputting anything? Of course I can only help you with s9y code, and not foreign counter code - I just gave you an example plugin of how to include a PHP script in a plugin. :)

The error tells you that counter.php outputs HTML code, but at this point serendipity does not allow HTML output.

Regards,
Garvin

Posted: Tue Aug 22, 2006 10:30 am
by Hokey
Thanks Garvin, I couldn't classify whether the code is S9Y or not. Hmm... then I have to look in the counter.php...