MyBB Community Forums

Full Version: {$thread['whatever']} variables
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I've got a plugin in development which ends up creating a variable such as {$thread['subject']}. The problem is, when I insert the variable into the template, it doesn't work. Here is my hook:

//Hooks
$plugins->add_hook("showthread_start", "myhook");

function myhook(&$thread)
{
global $mybb;
if ($mybb->settings['myplugin_enable'] == 1)
{
    $thread['thisvar'] = $existingvar;
    $thread['thatvar'] = $secondvar;
    $thread['someothervar'] = $lastvar;
}

I found a post by euantor explaining that if you were hooking into the postbit, you needed to use &$post as an argument for your function, so I assumed that you needed to do that for this as well.

I have a feeling that I need to globalize thread at the the start of myhook(), but I've tried that and it didn't work.

Any ideas?

Okay, so I switched it to a {$thisvar} format, and it started appearing. Now all I have to do is figure out how to finish my plugin and make it work (currently it's reacting on my else statement, returning an error).

On a similar note, I have this if statement:

$two = "four"
if (!$somesetting)
{
    eval("\$one = \"".$two."\";");
}
else
{
	eval("\$one= \""."three"."\";");
}

Everytime the script is executed, "three" is always being printed, never "four".

Any ideas on how to properly execute this?

I've been trying to fix this plugin for about 8 hours now. Angry Can someone please tell me where I've gone wrong?

<?php

//Disallow direct access for security reasons
if (!defined("IN_MYBB"))
{
die("You cannot access this file directly.<br />Please make sure IN_MYBB is defined.");
}

//Hooks
$plugins->add_hook("global_start", "shorturl_showthread");

//Information
function shorturl_info()
{
return array(
        "name"  => "WDTN URL Shortener",
        "description"=> "Allow the shortening of URLs using the WDTN URL Shortener API",
        "website"        => "http://wedesignthe.net",
        "author"        => "Seabody",
        "authorsite"    => "http://seabtech.empirehostings.net",
        "version"        => "1.0",
        "guid"             => "",
        "compatibility" => "16*"
    );
}

//Activation Routines
function shorturl_activate() {
global $db;

$shorturl_group = array(
        'gid'    => 'NULL',
        'name'  => 'shorturl',
        'title'      => 'WDTN Short URL',
        'description'    => 'Settings for the URL Shortener plugin',
        'disporder'    => "1",
        'isdefault'  => "0",
    );
	$db->insert_query('settinggroups', $shorturl_group);
 $gid = $db->insert_id(); 
 
$shorturl_setting = array(
        'sid'            => 'NULL',
        'name'        => 'shorturl_enable',
        'title'            => 'Do you want to enable the URL shortener plugin?',
        'description'    => 'If you set this option to yes, this plugin will be active on your board.',
        'optionscode'    => 'yesno',
        'value'        => '1',
        'disporder'        => 1,
        'gid'            => intval($gid),
    );
	
$db->insert_query('settings', $shorturl_setting);
  rebuild_settings();
}

//Deactivation Routines
function shorturl_deactivate()
  {
  global $db;
 $db->query("DELETE FROM ".TABLE_PREFIX."settings WHERE name IN ('shorturl_enable')");
 $db->query("DELETE FROM ".TABLE_PREFIX."settinggroups WHERE name='shorturl'");
rebuild_settings();
}

function shorturl_showthread()
{
//Get our globals	
global $mybb, $shorturl, $result, $templates;
if ($mybb->settings['shorturl_enable'] == 1)
{
//Set up the curl connection
$curl = curl_init('http://wdtn.co/api/shortener/create');
//Get our options downpat
curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER    =>    1,
    CURLOPT_TIMEOUT         =>    30,
    CURLOPT_POST            =>    1,
    CURLOPT_POSTFIELDS        =>    array('url' => 'http://www.{$mybb->settings[\'bburl\']}/showthread.php&tid={$tid}'),
    )
);
//Shorten it
$result = curl_exec($curl);
//Close curl
curl_close($curl);
//Parsing using json_decode
$result = json_decode($result);
//Checking for errors
if (!$result->error)
{
	$shortened = "Shortlink: <input type=\"text\" readonly value=\"\$result->slug\" />";
}
else
{
	$shortened = "No shortlink exists";
}
}
	eval("\$shorturl = \""."$shortened"."\";");
}
?>

All I get in this is "No shortlink exists" (I insert the variable manually because me and the Changing Template system don't get along well. Toungue )

I've tried replacing {$tid} with {$thread['tid']}, but nothing's worked. Angry I've also escaped virtually everything you can think of.
I only had a quick look but try this:

<?php

//Disallow direct access for security reasons
if (!defined("IN_MYBB"))
{
die("You cannot access this file directly.<br />Please make sure IN_MYBB is defined.");
}

//Hooks
$plugins->add_hook("global_start", "shorturl_showthread");

//Information
function shorturl_info()
{
return array(
        "name"  => "WDTN URL Shortener",
        "description"=> "Allow the shortening of URLs using the WDTN URL Shortener API",
        "website"        => "http://wedesignthe.net",
        "author"        => "Seabody",
        "authorsite"    => "http://seabtech.empirehostings.net",
        "version"        => "1.0",
        "guid"             => "",
        "compatibility" => "16*"
    );
}

//Activation Routines
function shorturl_activate() {
global $db;

$shorturl_group = array(
        'gid'    => 'NULL',
        'name'  => 'shorturl',
        'title'      => 'WDTN Short URL',
        'description'    => 'Settings for the URL Shortener plugin',
        'disporder'    => "1",
        'isdefault'  => "0",
    );
	$db->insert_query('settinggroups', $shorturl_group);
 $gid = $db->insert_id(); 
 
$shorturl_setting = array(
        'sid'            => 'NULL',
        'name'        => 'shorturl_enable',
        'title'            => 'Do you want to enable the URL shortener plugin?',
        'description'    => 'If you set this option to yes, this plugin will be active on your board.',
        'optionscode'    => 'yesno',
        'value'        => '1',
        'disporder'        => 1,
        'gid'            => intval($gid),
    );
	
$db->insert_query('settings', $shorturl_setting);
  rebuild_settings();
}

//Deactivation Routines
function shorturl_deactivate()
  {
  global $db;
 $db->query("DELETE FROM ".TABLE_PREFIX."settings WHERE name IN ('shorturl_enable')");
 $db->query("DELETE FROM ".TABLE_PREFIX."settinggroups WHERE name='shorturl'");
rebuild_settings();
}

function shorturl_showthread(&$showthread)
{
//Get our globals	
global $mybb, $shorturl, $result, $templates;
if ($mybb->settings['shorturl_enable'] == 1)
{
//Set up the curl connection
$curl = curl_init('http://wdtn.co/api/shortener/create');
//Get our options downpat
curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER    =>    1,
    CURLOPT_TIMEOUT         =>    30,
    CURLOPT_POST            =>    1,
    CURLOPT_POSTFIELDS        =>    array('url' => 'http://www.{$mybb->settings[\'bburl\']}/showthread.php&tid={$tid}'),
    )
);
//Shorten it
$result = curl_exec($curl);
//Close curl
curl_close($curl);
//Parsing using json_decode
$result = json_decode($result);
//Checking for errors
if (!$result->error)
{
	$shortened = "Shortlink: <input type=\"text\" readonly value=\"\$result->slug\" />";
}
else
{
	$shortened = "No shortlink exists";
}
}
	eval("\$shorturl = \""."$shortened"."\";");
}
?>
The $thread variable is not passed by refference at showthread_start.
$plugins->add_hook("showthread_start", "myhook"); 
function myhook()
{
	global $mybb;

	if($mybb->settings['myplugin_enable'] == 1)
	{
		global $thread;

		$thread['thisvar'] = $existingvar;
		$thread['thatvar'] = $secondvar;
		$thread['someothervar'] = $lastvar;
	}
}

You need to add it to the global scope inside the function.
Why are you using the global_start hook? The code I provided you will just work if you use the hook I advised. The shortlink can then be accessed within the showthread template using the $thread variable...
(2012-11-18, 10:26 PM)euantor Wrote: [ -> ]Why are you using the global_start hook? The code I provided you will just work if you use the hook I advised. The shortlink can then be accessed within the showthread template using the $thread variable...

I was using global_start before I used your code (thanks for that, btw). Originally I was using a showthread hook, but when that was repeatedly failing, I started trying other hooks.

Thanks Omar and Frank.
My code is only intended to work with the thread insert hook as it relies on the $url variable.
I started using newthread_do_newthread_end when you provided your code. The problem now is the install/uninstall functions. Since I was getting MyBB internal errors referencing the shortlink (plus I noticed your query referenced the db), I tried adding a new column. Problem - I still get MyBB internal errors when running the plugin (will get a screenshot soon).

Install:
function shorturl_install() {
global $db;
	$db->query("ALTER TABLE ".TABLE_PREFIX." threads ADD shortlink TEXT NOT NULL");
}

Uninstall:
function shorturl_uninstall() {
global $db;
	$db->query("ALTER TABLE ".TABLE_PREFIX." threads DROP shortlink");
}

[attachment=27812]
Personally I would use this:

if (!$db->field_exists('threads', 'shortlink')) {
    $db->add_column('threads', 'shortlink', 'TEXT NULL');
}