Hi All,
in administer>>blocks, I added a new block, and gave it the following PHP code:

CreateImageLink('home', 'Home');
CreateImageLink('briefbio', 'Brief Bio');
CreateImageLink('history', 'History');
CreateImageLink('storysofar', 'The Story So Far');
CreateImageLink('bicentenary', 'The Bicentenary Concert');
CreateImageLink('plaque', 'Plaque');
CreateImageLink('trust', 'The Emidy Trust');
CreateImageLink('archive', 'The Emidy Archive');
CreateImageLink('links', 'Links');

CreateImageLink is a PHP function I defined at the top of my page.tpl.php

function CreateImageLink($url, $alt) {
	print "<a href='/$url' onmouseout='MM_swapImgRestore();' onmouseover='MM_swapImage('$url','','$url"."_f2.jpg',1);'><img name='$url' src='$url.jpg' id='$url' alt='$alt' /></a>";
}

?>

I thought this should work. Now when I goto my site I get
Fatal error: Call to undefined function: createimagelink() in /home/josephan/www/www/includes/common.inc(1158) : eval()'d code on line 2

So I cant even goto the blocks section to remove the code! Is there any way to fix this? Or do I have to manually go and edit the database to get rid of it?
So Can we not call custom PHP functions from a block? if so what is the procedure?

Comments

anner’s picture

you might want to try defining the function in a .inc file instead and including that file in to your template instead. I've heard of this fixing the same issue for folk who tried to define functions in the body of the page.

If you make this change, you shouldn't need to make any db changes.

Heine’s picture

The block code is executed before the first call to theme (and certainly before the call to theme('page')). Whether the function is defined in page.tpl.php or in an include file doesn't matter; it won't be loaded at the time the function is called.

--
The Manual | Troubleshooting FAQ | Tips for posting | Make Backups! | Consider creating a Test site.

narres’s picture

in settings.php

Thomas Narres
Keep the sunny side up

anner’s picture

Then she could put the include at the top of the block

Heine’s picture

To repair the current installation, either edit the block in the table 'boxes' or set its status to 0 in the blocks table (you have to do this for the theme you use).

Put the function in a module, enable it and it will be available to your blocks & nodes. You can also make a module that defines the block
--
The Manual | Troubleshooting FAQ | Tips for posting | Make Backups! | Consider creating a Test site.

high1memo’s picture

Actually, immediately after posting this, I hada look at the database, and found the blocks table, and set the status of my block to 0. So now I can at least visit the site.

And just now I tried defining the function in settings.php as suggested and it works perfectly!

Thanks all!

high1memo’s picture

When I said it works perfectly, I meant the function is defined and the block is displaying correctly - according to the PHP function. It just so happens that that PHP function is buggy and the rollover code doesnt work... so if a noncoder stumbles upon this thread and wanted to use the same function he will have troubles. So here is the correct version:

(replace /themes/bluemarine/ with the path to your button images)

function CreateImageLink($id, $alt) {
	$file1 = "/themes/bluemarine/$id.gif";
	$file2 = "/themes/bluemarine/$id"."_f2.gif";
	print "\n<a href='/$id' onmouseout=\"MM_swapImgRestore();\" onmouseover=\"MM_swapImage('$id','','$file2',1);\">\n";
	print "   <img name='$id' src='$file1' id='$id' alt='$alt' /></a>\n";
}

and in your page.tpl.php add this in the head:

<script language="JavaScript1.2" type="text/javascript">
<!--
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}



//-->
</script>