Вопрос

I'm trying to build my first plugin for a platform called Ushahidi. Ushahidi is a PHP based platform that uses the Kohana framework.

I was looking at all the hooks available to me here: https://wiki.ushahidi.com/display/WIKI/Plugin+Actions

My goal is to add meta tags into the header of certain pages to help make the website more searchable and shareable. These tags will be dynamic based upon the content of the page but for now I just want to get "Hello World" into the right place.

The closest hook I could find takes me to the correct page but not the right place. If you visit http://advance.trashswag.com/reports/view/1 I've managed to get the string "Hello World" to appear on the page. Step 1 done - great. Step 2 for me is to get hello world to appear in the header of the page so only viewable using "view page source". Is there a way that I can step back up the DOM based on my function:

<?php

class SearchShare{

    public function __construct(){
        //hook into routing
        Event::add('system.pre_controller', array($this, 'SearchShare'));
    }

    public function SearchShare(){
        // This seems to be the part that tells the platform where to place the change. Presumably this is the part I'd need to edit to step up the DOM into the head section
        Event::add('ushahidi_action.report_meta', array($this, 'AddMetaTags'));
    }

    public function AddMetaTags(){
        // just seeing if I can get any code to run
        echo '<h1 style="font-size:70px;">Hello World</h1>';
    }
}
new SearchShare;

?>
Это было полезно?

Решение

You need to use a different event to get your code in the right place. There are a couple of places you can hook in:

  1. Use the ushahidi_action.header_scripts event:

    Event::add('ushahidi_action.header_scripts', array($this, 'AddMetaTags'));

    See header.php to see where that hooks in.

  2. Use the ushahidi_filter.header_block event:

    public function SearchShare(){
      Event::add('ushahidi_filter.header_block', array($this, 'AddMetaTags'));
    }
    public function AddMetaTags(){
      $header = Event::$data;
      $header .= "Hello World";
      Event::$data = $header;
    }
    
    See Themes.php for where that hooks in.

Neither of these is better/worse than the other, so use whichever you prefer.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top