Ok … I have done this in PHP and it works, but when I try to do it as plugin it doesn’t … what am I doing wrong? This is just a simple version of real plugin I have intention to publish (I have it already done), but it just seems I am doing something wrong in structure of plugin. I have used one of the most simplest plugins and tried to do it in it, but it also doesn’t work.
<?php
/*
=====================================================
ExpressionEngine - by pMachine
-----------------------------------------------------
http://www.pmachine.com/
-----------------------------------------------------
Copyright (c) 2003 pMachine, Inc.
=====================================================
THIS IS COPYRIGHTED SOFTWARE
PLEASE READ THE LICENSE AGREEMENT
http://www.pmachine.com/license/
=====================================================
File: pi.firstimage.php
-----------------------------------------------------
Purpose: Shows first image from selected text
=====================================================
*/
$plugin_info = array(
'pi_name' => 'firstimage',
'pi_version' => '1.0 beta',
'pi_author' => 'Crnaovca',
'pi_author_url' => 'http://www.crnaovca.net/',
'pi_description' => 'Shows first image from selected text for displaying with headlines on frontpage',
'pi_usage' => firstimage::usage()
);
Class firstimage
{
var $return_data;
// ------------------------
// Constructor
// ------------------------
function firstimage($str = '')
{
global $TMPL;
if ($str == '')
$str = $TMPL->tagdata;
//finds start of <img >')+1;
//gets only the part with image tag
$imagetag = substr($imagetag, 0,$position);
if (strlen($imagetag)==0) {
//if there is no image tags, it displays text that there is no image
//good workaround is to set image tag to path where is dummy picture that will display
//in case there is no founded image
$imagetag = "No image";
};
$this->return_data = trim($imagetag);
}
// ------------------------
// Plugin Usage
// ------------------------
function usage()
{
ob_start();
?>
If you are in a situation where you must put image on front page and all you have is text with images, this plugin helps you.
It takes first image from selected text and displays only that image tag.
To use this plugin, wrap anything you want to be processed by it between these tag pairs:
{firstimage}
text you want processed
{/firstimage}
<?php
$buffer = ob_get_contents();
ob_end_clean();
return $buffer;
} // END
}
?>
Now I am ashamed 😊 it works … I had one comma “,” in code and just couldn’t find it 😊 It is corrected now. What do you think about it? Would it be any useful for you?
I had to create such plugin, because in my case, I have frontpage where I must put headline of a weblog entry and one image. One solution was to create new custom field in which I should put headline image, but that solution is taking too much time. Now I have created this plugin that gets first image tag from selected text and displays it.
Next version that I will publish in few days will have solution that it displays either random image from text, or specific image from text (first, second, third and more… )
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.