function make_clickable_callback

Discussion forum for Extension Writers regarding Extension Development.
User avatar
Okashii
Registered User
Posts: 15
Joined: Wed Aug 24, 2016 4:07 pm

function make_clickable_callback

Post by Okashii »

I posted this on Area 51, but I'm not sure if that was the correct place to ask this question, so I'm asking it here as well.

---------

So I've always really liked the way vBulletin automatically fetches page titles when you post an http link and don't define a link title. So I'm trying to make that function work on our phpBB 3.1.9 installation.

I know the preferred method is to make an extension, but I haven't quite grokked that process yet. For now I've been editing the core code where and when I need to like I've always done.

So I made this function and put it in functions_content.php

Code: Select all

function get_title($url, $alt)
{
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

    $data = curl_exec($ch);
    curl_close($ch);

	$doc = new DOMDocument();
	@$doc->loadHTML($data);
	$nodes = $doc->getElementsByTagName('title');

	$title = $nodes->item(0)->nodeValue;
	if (isset($title))
	{
		$title = preg_replace( "/\r|\n/", "", $title );
		// in case there's nothing left after preg_replace
		if (isset($title))
		{
			return $title;
		}
		else
		{
			return $alt;
		}
	}
	else
	{
		return $alt;
	}
}
The $alt is in case title generation fails.

So then I edited a couple of cases:

Code: Select all

		case MAGIC_URL_FULL:
			$tag	= 'm';
			//$text	= $short_url;
			$text = get_title($orig_url, $short_url);
		break;

		case MAGIC_URL_WWW:
			$tag	= 'w';
			$url	= 'http://' . $url;
			//$text	= $short_url;
			$text = get_title($orig_url, $short_url);
		break;
This seems to work flawlessly (most of the time). The problem is you can no longer define a link title using

Code: Select all

[url=http://this.is.my/link.html]This is my title[/url]
I think I need to put this function somewhere else, but I haven't been able yet to figure out where. I'm only now just learning about callbacks, and from what I gather this make_clickable_callback function is called from multiple places.

Example can be seen here:

http://criticalpoliticalboard.com/viewt ... 711#p13711

all PACE had to do was post only the link itself, and my function took care of the rest.

http://www.rferl.org/content/russia-lgb ... 81994.html

But it would still be nice to have the ability to define link titles once again without losing this function.

What am I doing wrong?

Thank you in advance.

-Okashii
Last edited by JimA on Thu Sep 15, 2016 5:20 pm, edited 1 time in total.
Reason: Moved from Support Forums to Extensions Writers Discussion
User avatar
Mick
Support Team Member
Support Team Member
Posts: 26887
Joined: Fri Aug 29, 2008 9:49 am

Re: function make_clickable_callback

Post by Mick »

Okashii wrote:but I haven't quite grokked that process yet
:D thank you for 'grokked', what a word! Sorry I can't help with your question.
  • "The more connected we get the more alone we become” - Kyle Broflovski© 🇬🇧
User avatar
Okashii
Registered User
Posts: 15
Joined: Wed Aug 24, 2016 4:07 pm

Re: function make_clickable_callback

Post by Okashii »

Mick wrote::D thank you for 'grokked', what a word! Sorry I can't help with your question.
lol not many people know that word anymore
User avatar
Okashii
Registered User
Posts: 15
Joined: Wed Aug 24, 2016 4:07 pm

Re: function make_clickable_callback

Post by Okashii »

Well this is embarrassing.

Somewhere along the way I fixed it (almost) and didn't realize it.

The only links that don't convert automatically though are https links, but I can live with that (for now).

I'm a perfectionist, though, so eventually it will bug me.

Return to “Extension Writers Discussion”