My First Nucleus Plugin - NP_PopUpLink
Posted on 19/12/07 14:16
I've written my first Nucleus plugin - NP_PopUpLink!
Well, I say I wrote it, in fairness it was more a case of creating it. The main body is heavily based on NP_AdminPreview, with a healthy dose of NP_TechnoratiTags, and some of the core Nucleus HTML & javascript. And I say it's my first, but I'm not sure I'll be writing a second.
So why did I do it, and what does it do? Well, as you may have noticed, I like all external links on the site (eg to my Flickr account) to open in a new window, and all internal links (eg to my photo albums and older posts) to stay in the same window. Maybe that's bad practise, but that's how I like it. Unfortunately, the javascript toolbar in the admin area for adding/editing posts doesn't provide this functionality - I have to manually add a target="..." attribute after the fact. After two years of doing this manually, I finally decided to do something about it. I've been interested in how plugins work (having customised one or two to meet my needs), and this seemed a good excuse to write one.
A look round the Nucleus plugins turned up NP_AdminPreview which adds functionality to the Add and Edit pages in the Admin area, so this seemed like a good place to start. This gave me a way to add functionality into the "Extra Plugins Options" subsection of those pages. The actual button code (and graphic) were based on the same used for the "Make Link" button in the standard javascript tool bar, but tweaked to add target="_blank" to the <a href="..."> element. NP_TechnoratiTags gave me a way to add this code into a javascript include file, linked from the page header, rather than writing the script out with the button. (For now that probably doesn't matter, but if I wanted to add new buttons in the future, it's a more flexible approach.)
The good news is (after one aborted attempt caused by leaving some redundant code in the re-purposed NP_TechnoratiTags function) it works. The bad news is it's not exactly usable, as I can't have both the standard javascript toolbar and my new button on screen at the same time. Further research (which I should have done earlier...) shows a FAQ entry "How do I add custom buttons to my Add/Edit Item page?". This says, in summary, you can't do it with a plugin! It can be done, but you need to edit the core Nucleus code, so it gets wiped out whenever you upgrade your version of Nucleus.
It wasn't a completely wasted exercise. I've learnt a bit about Nucleus plugins, and I do have my new button (just in the wrong place). I shan't be publishing it on the Nucleus plugin site, but if anyone desperately wants a copy, it's here: NP_PopUpLink. extract the zip file into your Nucleus plugins directory, ensuring you've got both the PHP file and the popuplink directory containing the button graphic and javascript file. If you want to see the code, read the full version of this article (which should be nicely highlighted by NP_GeSHi2).
Well, I say I wrote it, in fairness it was more a case of creating it. The main body is heavily based on NP_AdminPreview, with a healthy dose of NP_TechnoratiTags, and some of the core Nucleus HTML & javascript. And I say it's my first, but I'm not sure I'll be writing a second.
So why did I do it, and what does it do? Well, as you may have noticed, I like all external links on the site (eg to my Flickr account) to open in a new window, and all internal links (eg to my photo albums and older posts) to stay in the same window. Maybe that's bad practise, but that's how I like it. Unfortunately, the javascript toolbar in the admin area for adding/editing posts doesn't provide this functionality - I have to manually add a target="..." attribute after the fact. After two years of doing this manually, I finally decided to do something about it. I've been interested in how plugins work (having customised one or two to meet my needs), and this seemed a good excuse to write one.
A look round the Nucleus plugins turned up NP_AdminPreview which adds functionality to the Add and Edit pages in the Admin area, so this seemed like a good place to start. This gave me a way to add functionality into the "Extra Plugins Options" subsection of those pages. The actual button code (and graphic) were based on the same used for the "Make Link" button in the standard javascript tool bar, but tweaked to add target="_blank" to the <a href="..."> element. NP_TechnoratiTags gave me a way to add this code into a javascript include file, linked from the page header, rather than writing the script out with the button. (For now that probably doesn't matter, but if I wanted to add new buttons in the future, it's a more flexible approach.)
The good news is (after one aborted attempt caused by leaving some redundant code in the re-purposed NP_TechnoratiTags function) it works. The bad news is it's not exactly usable, as I can't have both the standard javascript toolbar and my new button on screen at the same time. Further research (which I should have done earlier...) shows a FAQ entry "How do I add custom buttons to my Add/Edit Item page?". This says, in summary, you can't do it with a plugin! It can be done, but you need to edit the core Nucleus code, so it gets wiped out whenever you upgrade your version of Nucleus.
It wasn't a completely wasted exercise. I've learnt a bit about Nucleus plugins, and I do have my new button (just in the wrong place). I shan't be publishing it on the Nucleus plugin site, but if anyone desperately wants a copy, it's here: NP_PopUpLink. extract the zip file into your Nucleus plugins directory, ensuring you've got both the PHP file and the popuplink directory containing the button graphic and javascript file. If you want to see the code, read the full version of this article (which should be nicely highlighted by NP_GeSHi2).
<br />
<?php<br />
<br />
/*************************************************************************<br />
NP_PopUpLink - Nucleus plugin to create a link that opens in a new <br />
browser window.<br />
<br />
Created because I link to keep links within my site in the same broswer <br />
window, and links to external sites in a new window. <br />
<br />
To install <br />
- Add NP_PopUpLink.php to your Nucleus plugins directory<br />
- Add the popuplink directory containing popuplink.js and <br />
button-popuplink.gif to your Nucleus plugins directory<br />
- Install the plugin via Nucleus Admin<br />
- Away you go!<br />
<br />
Based on (ie plagiarised from):<br />
- NP_AdminPreview v0.2 by Kai Greve (http://kgblog.de/)<br />
- NP_TechnoratiTags v0.9.5 by admun<br />
- Nucleus v3.24 existing add link capability<br />
<br />
To do:<br />
- Learn PHP<br />
- Learn more about Nucleus<br />
- Work out how to create the button in a more useful location! <br />
(Like alongside the existing editing buttons)<br />
- Look into the WYSIWYG editor plugins for inspriation<br />
- Improve the button icon<br />
<br />
About:<br />
- Version 0.1 <br />
- 18/12/2007<br />
- By Kevin Whitworth (http://www.jacurutu.org.uk/)<br />
*************************************************************************/<br />
<br />
class NP_PopUpLink extends NucleusPlugin {<br />
<br />
function getName() { return 'PopUpLink'; }<br />
function getAuthor() { return 'Kevin Whitworth'; }<br />
function getURL() { return 'http://www.jacurutu.org.uk/'; }<br />
function getVersion() { return '0.1'; }<br />
function getDescription() { return 'Adds a button on the add and edit pages to create a link that opens in a new browser window.'; }<br />
<br />
function install() {<br />
}<br />
<br />
function getEventList() {<br />
return array('AddItemFormExtras', 'EditItemFormExtras', 'AdminPrePageHead');<br />
}<br />
<br />
function unInstall() {<br />
}<br />
<br />
// This creates the code for the new PopUp Link button, using the button image in ~/plugins/popuplink<br />
function buttonCode($_data) {<br />
global $CONF;<br />
<br />
$output = '<span class="jsbutton" onmouseover="BtnHighlight(this);" onmouseout="BtnNormal(this);" onclick="ahrefPopUpThis()" ><img src="'.$CONF['AdminURL'].'plugins/popuplink/button-popuplink.gif" alt="" width="16" height="16"/></span>';<br />
return $output;<br />
}<br />
<br />
// When a new item is created, add the PopUp Link button into the "Extra Plugins Options" section<br />
function event_AddItemFormExtras($_data) {<br />
echo $this->buttonCode($_data);<br />
}<br />
<br />
// When an existing item is edited, add the PopUp Link button into the "Extra Plugins Options" section <br />
function event_EditItemFormExtras($_data) {<br />
echo $this->buttonCode($_data);<br />
}<br />
<br />
// The javascript code for inserting the link into the text lives in a .js file (~/plugins/popuplink/popuplink.js)<br />
// so insert a link to that script in the admin page headers for add and edit pages.<br />
function event_AdminPrePageHead(&$data) {<br />
global $CONF;<br />
<br />
if (($data['action'] != 'itemedit') && ($data['action'] != 'createitem'))<br />
return;<br />
<br />
$data['extrahead'] .= '<script type="text/javascript" src="'.$CONF['AdminURL'].'plugins/popuplink/popuplink.js"></script>';<br />
}<br />
<br />
<br />
function supportsFeature ($what)<br />
{<br />
switch ($what)<br />
{<br />
case 'SqlTablePrefix':<br />
return 1;<br />
default:<br />
return 0;<br />
}<br />
}<br />
}<br />
?><br />
<?php<br />
<br />
/*************************************************************************<br />
NP_PopUpLink - Nucleus plugin to create a link that opens in a new <br />
browser window.<br />
<br />
Created because I link to keep links within my site in the same broswer <br />
window, and links to external sites in a new window. <br />
<br />
To install <br />
- Add NP_PopUpLink.php to your Nucleus plugins directory<br />
- Add the popuplink directory containing popuplink.js and <br />
button-popuplink.gif to your Nucleus plugins directory<br />
- Install the plugin via Nucleus Admin<br />
- Away you go!<br />
<br />
Based on (ie plagiarised from):<br />
- NP_AdminPreview v0.2 by Kai Greve (http://kgblog.de/)<br />
- NP_TechnoratiTags v0.9.5 by admun<br />
- Nucleus v3.24 existing add link capability<br />
<br />
To do:<br />
- Learn PHP<br />
- Learn more about Nucleus<br />
- Work out how to create the button in a more useful location! <br />
(Like alongside the existing editing buttons)<br />
- Look into the WYSIWYG editor plugins for inspriation<br />
- Improve the button icon<br />
<br />
About:<br />
- Version 0.1 <br />
- 18/12/2007<br />
- By Kevin Whitworth (http://www.jacurutu.org.uk/)<br />
*************************************************************************/<br />
<br />
class NP_PopUpLink extends NucleusPlugin {<br />
<br />
function getName() { return 'PopUpLink'; }<br />
function getAuthor() { return 'Kevin Whitworth'; }<br />
function getURL() { return 'http://www.jacurutu.org.uk/'; }<br />
function getVersion() { return '0.1'; }<br />
function getDescription() { return 'Adds a button on the add and edit pages to create a link that opens in a new browser window.'; }<br />
<br />
function install() {<br />
}<br />
<br />
function getEventList() {<br />
return array('AddItemFormExtras', 'EditItemFormExtras', 'AdminPrePageHead');<br />
}<br />
<br />
function unInstall() {<br />
}<br />
<br />
// This creates the code for the new PopUp Link button, using the button image in ~/plugins/popuplink<br />
function buttonCode($_data) {<br />
global $CONF;<br />
<br />
$output = '<span class="jsbutton" onmouseover="BtnHighlight(this);" onmouseout="BtnNormal(this);" onclick="ahrefPopUpThis()" ><img src="'.$CONF['AdminURL'].'plugins/popuplink/button-popuplink.gif" alt="" width="16" height="16"/></span>';<br />
return $output;<br />
}<br />
<br />
// When a new item is created, add the PopUp Link button into the "Extra Plugins Options" section<br />
function event_AddItemFormExtras($_data) {<br />
echo $this->buttonCode($_data);<br />
}<br />
<br />
// When an existing item is edited, add the PopUp Link button into the "Extra Plugins Options" section <br />
function event_EditItemFormExtras($_data) {<br />
echo $this->buttonCode($_data);<br />
}<br />
<br />
// The javascript code for inserting the link into the text lives in a .js file (~/plugins/popuplink/popuplink.js)<br />
// so insert a link to that script in the admin page headers for add and edit pages.<br />
function event_AdminPrePageHead(&$data) {<br />
global $CONF;<br />
<br />
if (($data['action'] != 'itemedit') && ($data['action'] != 'createitem'))<br />
return;<br />
<br />
$data['extrahead'] .= '<script type="text/javascript" src="'.$CONF['AdminURL'].'plugins/popuplink/popuplink.js"></script>';<br />
}<br />
<br />
<br />
function supportsFeature ($what)<br />
{<br />
switch ($what)<br />
{<br />
case 'SqlTablePrefix':<br />
return 1;<br />
default:<br />
return 0;<br />
}<br />
}<br />
}<br />
?><br />

