Best place to insert php code?

Random stuff about serendipity. Discussion, Questions, Paraphernalia.
Post Reply
Hokey
Regular
Posts: 141
Joined: Wed Dec 14, 2005 3:36 pm
Location: Germany
Contact:

Best place to insert php code?

Post 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?
Harald Weingaertner
Regular
Posts: 474
Joined: Mon Mar 27, 2006 12:32 am

Post 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 ;)
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post 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
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
Hokey
Regular
Posts: 141
Joined: Wed Dec 14, 2005 3:36 pm
Location: Germany
Contact:

Post 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' );
?>
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post 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;
        }
    }
}

# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
Hokey
Regular
Posts: 141
Joined: Wed Dec 14, 2005 3:36 pm
Location: Germany
Contact:

Post 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?
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post 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
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
Hokey
Regular
Posts: 141
Joined: Wed Dec 14, 2005 3:36 pm
Location: Germany
Contact:

Post 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
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post 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
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
Hokey
Regular
Posts: 141
Joined: Wed Dec 14, 2005 3:36 pm
Location: Germany
Contact:

Post 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...
Post Reply