OJS OCS OMP OHS

You are viewing the PKP Support Forum | PKP Home Wiki



Plugin Development - Inserting code into the HEAD tag.

OJS development discussion, enhancement requests, third-party patches and plug-ins.

Moderators: jmacgreg, btbell, michael, bdgregg, barbarah, asmecher

Forum rules
Developer Resources:

Documentation: The OJS Technical Reference and the OJS API Reference are both available from the OJS Documentation page.

Git: You can access our public Git Repository here. Comprehensive Git usage instructions are available on the wiki.

Bugzilla: You can access our Bugzilla report tracker here.

Search: You can use our Google Custom Search to search across our main website, the support forum, and Bugzilla.

Questions and discussion are welcome, but if you have a workflow or usability question you should probably post to the OJS Editorial Support and Discussion subforum; if you have a technical support question, try the OJS Technical Support subforum.

Plugin Development - Inserting code into the HEAD tag.

Postby jheckman » Tue Nov 22, 2011 1:20 pm

Greetings PKP Dev Forum.

I'm working on a plugin for OJS in which I need to add a call to a JavaScript file in between the head tags.

I''ve seen some other plugins have some apparent success with this action, but I'm unable to achieve it.

I did something like this:
Code: Select all

$additionalHeadData = $templateManager->get_template_vars('additionalHeadData');

$scriptUrl1 = '<script type="text/javascript" src="http://$remoteServerPath/folderpath/file.php></script>';
         
$templateManager->assign('additionalHeadData', $additionalHeadData."\n\t".$scriptUrl1);



Alas, it does not work, Would anyone with more OJS dev experience be able to point me in the proper direction?

Thanks.
jheckman
 
Posts: 12
Joined: Thu Nov 17, 2011 8:10 am
Location: Pittsburgh PA, USA

Re: Plugin Development - Inserting code into the HEAD tag.

Postby ramon » Thu Nov 24, 2011 4:33 am

Hello jheckman,

Check your PHP code.
Single quotes DO NOT render PHP variables. They will convert whatever is between them to strings.
You'l have to escape correctly the value of $scriptURl1.
You are missing double-quotes.

Apache/PHP error log should register those errors.

One solution would be to change your code to (fixing the other paths to whatever they should be: folderpath and file.php):
Code: Select all
$scriptUrl1 = '<script type="text/javascript" src="http://'.$remoteServerPath.'/folderpath/file.php"></script>';


OR

Code: Select all
$scriptUrl1 = "<script type=\"text/javascript\" src=\"http://$remoteServerPath/folderpath/file.php\"></script>";
ramon
 
Posts: 926
Joined: Wed Oct 15, 2003 6:15 am
Location: Brasí­lia/DF - Brasil

Re: Plugin Development - Inserting code into the HEAD tag.

Postby jheckman » Mon Nov 28, 2011 7:42 am

Hey Ramon,

Thanks for the input, I still can't seem to get it to append that code to the head.
I would love for it to append anything.

I get no PHP errors with this, it just does nothing.

Ideally, I need the variable called $scriptUrl1 to put the script tags into the head of the template. Much like the web-feed URLS get injected.

here's the snip of the function I am using.

Code: Select all
    function register() {
         HookRegistry::register('TemplateManager::display',array(&$this, 'AddHeadScript'));
      }
      
   function AddHeadScript($hookName, $args) {
         $templateManager =& $args[0];
         $currentJournal =& $templateManager->get_template_vars('currentJournal');
         $displayPage = $currentJournal ? $this->getSetting($currentJournal->getId(), 'displayPage') : null;
         $requestedPage = Request::getRequestedPage();
                     // append into the header
            $additionalHeadData = $templateManager->get_template_vars('additionalHeadData');

            $scriptUrl1 = '<script type="text/javascript" src="http://'.$installPath.'/'.$folderPath.'/file.php;target=_blank"></script>';
         
            $templateManager->assign('additionalHeadData', $additionalHeadData."\n\t".$scriptUrl1);
         }



No php errors, just no resulting code in the head.

Again, thanks for the extra sets of eyes on this.

~Jason
jheckman
 
Posts: 12
Joined: Thu Nov 17, 2011 8:10 am
Location: Pittsburgh PA, USA

Re: Plugin Development - Inserting code into the HEAD tag.

Postby ramon » Mon Nov 28, 2011 9:11 am

Hello Jason,

Without the full code of your plugin is hard to tell what's going on.
I would suggest either posting a tar.gz file here so other can look at what your doing.
The error maybe somewhere else.

I would throw some echo statements around to see where what the code is really doing, and where it's going.

Have you checked the output of your html header?
Are you sure your variables are being loaded?
It doesn't seem to me that $installPath will ever be retrieved, as you haven't called the required $this->installPath...
The only plugin that has that is the OpenAds Plugin (grep in a stock 2.3.6.0 install).
You might want to check that plugin for how it does it or the webFeed Plugin for other methods...
ramon
 
Posts: 926
Joined: Wed Oct 15, 2003 6:15 am
Location: Brasí­lia/DF - Brasil

Re: Plugin Development - Inserting code into the HEAD tag.

Postby jheckman » Mon Nov 28, 2011 10:01 am

Hey Ramon,

I'm actually modifying the openAds plugin to be a more functional openX plugin, and the new openX delivery API I'm using needs a script added to the head.

that this->installPath thing will be set in the database when i'm done.

at this point, its still doing what openAds did, and its in the config.inc.php script.

I would love for it to even put the wrong path in the head, as I can't get it to echo anything up there.
jheckman
 
Posts: 12
Joined: Thu Nov 17, 2011 8:10 am
Location: Pittsburgh PA, USA

Re: Plugin Development - Inserting code into the HEAD tag.

Postby ramon » Tue Nov 29, 2011 4:05 am

Hello Jason,

PKP has added a few configuration variables that may help you pinpoint where the problem is.
There are [debug] section in ojs2/config.inc.php, and a display_errors variable that you may set to On and see if it outputs anything.
Have you checked your server logs?
If there are no errors being output, then, the code is not being executed...
ramon
 
Posts: 926
Joined: Wed Oct 15, 2003 6:15 am
Location: Brasí­lia/DF - Brasil

Re: Plugin Development - Inserting code into the HEAD tag.

Postby jheckman » Wed Nov 30, 2011 12:06 pm

I figured it out!

I looked at the way my plugin was calling registry hooks, and added my own function.

in my plugin.inc.php I added to:

function register
Code: Select all
function register($category, $path) {
      if (parent::register($category, $path)) {
         if ($this->getEnabled()) {
            HookRegistry::register('TemplateManager::display', array(&$this, 'mainCallback'));
/** This next line calls the head scripts */
            HookRegistry::register('TemplateManager::display', array(&$this, 'openxAddHeadScript'));
            HookRegistry::register('PluginRegistry::loadCategory', array(&$this, 'callbackLoadCategory'));
         }

         return true;
      }
      return false;


And then wrote the function as such:

Code: Select all
function openxAddHeadScript($hookName, $args, $openXPath) {
   $templateManager =& $args[0];

   $additionalHeadData = $templateManager->get_template_vars('additionalHeadData');

                        $script1 = '<script language="javascript" type="text/javascript" src="'.$this->getInstallationPath().'/www/delivery/spcjs.php?id=2&amp;target=_blank"></script>';
                       
                        $templateManager->assign('additionalHeadData', $additionalHeadData."\n\t".$script1);
}



It works nicely, if anyone else needs it for a project!
...............................
Jason Heckman
Web Developer
University of Pittsburgh
University Library System
jheckman
 
Posts: 12
Joined: Thu Nov 17, 2011 8:10 am
Location: Pittsburgh PA, USA

Re: Plugin Development - Inserting code into the HEAD tag.

Postby solty » Sat Jul 28, 2012 3:09 am

Dear Friends,
I was obtained a sharing java script php code from http://sharethis.com for OJS
But I was not find any php "head tag" code in the config.inc.php file.
Where is the OJS's "head tag" point?
Could you suggest appropriate points for especially code 3. Copy the script tags and place them inside and at the end of your "head" tag.?
Best,

Sultan

1. Copy and place where you want your egg to show.
<div id='shareThisShareEgg' class='shareEgg'></div>

2. Copy and place anywhere after the code from step 1.
<script type='text/javascript'>stlib.shareEgg.createEgg('shareThisShareEgg', ['sharethis','facebook','twitter','linkedin','email'], {title:'ShareThis Rocks!!!',url:'http://www.sharethis.com',theme:'shareegg'});</script>

3. Copy the script tags and place them inside and at the end of your "head" tag.
<script type="text/javascript" src="http://w.sharethis.com/gallery/shareegg/shareegg.js"></script>
<script type="text/javascript" src="http://w.sharethis.com/button/buttons.js"></script>
<script type="text/javascript">stLight.options({publisher: "6d233bf5-9c7d-478c-820d-cf5e937b9af3", onhover:false}); </script>
<link media="screen" type="text/css" rel="stylesheet" href="http://w.sharethis.com/gallery/shareegg/shareegg.css"></link>
Attachments
Clipboard01.jpg
Clipboard01.jpg (59.11 KiB) Viewed 1718 times
solty
 
Posts: 140
Joined: Mon Mar 16, 2009 12:47 am
Location: Turkiye, İzmir


Return to OJS Development

Who is online

Users browsing this forum: No registered users and 1 guest