Back to General Articles

Learn to HELP YOURSELF
by rake 02 Sep 2004

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
LEARN to HELP YOURSELF
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

I decided to write this after observing the types of questions asked in the General Modification Discussion and PHP/MySQL/JS/HTML forums. If people would know a few simple things, i'm sure the number of questions would be reduced by at least 40-50%.

First of all, they need to know that asking on a forum is NOT an efficient way of getting an aswer, and by no means a fast one. It could be days before a member will give you the right solution to your problem. Secondly, you waste BOTH YOUR TIME, and the TIME OF OTHER MEMBERS.

With that in mind, the best way to find an answer is to search for it yourself...

Golden Rule 1: Use Forum Search - i know this has been said over and over, but i just felt the need to repeat it
Golden Rule 2: Use Google

1. EditPlus2 - It's a program. What special about it is the "Find in Files" feature. It will search for a string in multiple files. Use google to find it. I'll give an example of using this later.

2. When you need to find a certain template, keep in mind there's a vBulletin option which will add the names of templates in HTML comments in the HTML source of the page. Turn it on, refresh your vBulletin page, right click on the page > View Source, and then you can just read the name of your template. Also, don't forget there is a search in templates function within vBulletin.

3. If something is wrong in your code, DEBUG, and then post for help. Let's take an example for this. Let's say you have an if statement, which sets a variable to true/false, and based on this you want to allow a user access to a page or not - and it's just not working...

PHP Code:
if($something == 'x')
{
    
$myvar TRUE;
}
else
{
    
$myvar FALSE;
}

if(
$myvar == FALSE)
{
    
print_no_permission();

Let's say you KNOW the variable should be FALSE, but for some reason it's true... First thing to do, is to check the value of the variable: After the first if, add the following code: echo $myvar; exit; This simple echo-ing method can be used in most cases. I've seen at least 3-4 questions like this in the past couple of days.

4. If something isn't working, and you just can't figure out why, try using alternate methods of getting the same effect.

5. If you need help with a certain php function, don't ask on the forums. I recommend downloading the php manual in Windows Help Format (chm) from php.net. It's very useful as a function reference. If you don't want to download it - again, use google.

6 If you want to find a function that does something, use the manual. Let's say you want to find a function that converts a string to uppercase. You open the manual, click the Search tab, and search for uppercase. There'll be a few items on the list, but the 4th one is what you're looking for: strtoupper.

7. Similarly, if you need to know what a vBulletin function does, first thing to do is to look at it's name, which will in most cases give a pretty big hint on what the function is supposed to do. Look if there are any comments on that function in the code, and finally, try looking the code and figuring out what it's supposed to do.

8. If you want to find a vbulletin function, EditPlus is your friend. Use Find in Files, and you'll have your function.

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
PRACTICAL EXAMPLE OF USING THE ABOVE TIPS
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

What we want

As an example of using the above tips, i'll use this thread: How do I: Have Links From a Specific Domain Open in Same Window

What we're trying to do is have links (in posts) from certain domains open in the same window. By default, vbulletin opens a new window for all links.

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

How We do it

First, let's open EditPlus, Search > Find In Files. We'll want to search in our forum folder, for files of type *.*, and the string we'll search for is [url since we know that is the bbcode tag used to generate html links.

There's be some results, but you can quickly see which are the important ones. In includes/functions_bbcodeparse.php there'll be some regular expressions to match the [url tag, and some very useful comments ( // [URL] ). Now you know you've found what you were searching for. Go to that page.

PHP Code:
        // [URL]
        
$bbcodes['standard']['find']['[url]'] = '#\[url\](.*)\[/url\]#esiU';
        
$bbcodes['standard']['replace']['[url]'] = "handle_bbcode_url('\\1', '', 'url')";
        
$bbcodes['standard']['recurse']['url'][0] = array('handler' => 'handle_bbcode_url');
        
// [URL=XXX]
        
$bbcodes['standard']['find']['[url='] = '#\[url=("|"|\'|)(.*)\\1\](.*)\[/url\]#esiU';
        
$bbcodes['standard']['replace']['[url='] = "handle_bbcode_url('\\3', '\\2', 'url')";
        
$bbcodes['standard']['recurse']['url'][1] = array('handler' => 'handle_bbcode_url'); 
By taking a look at the above code, the first thing your eyes should fall on is the "handle_bbcode_url" function...

Use the find in files function again, to see where that function is... Well, what do you know? It's also in the functions_bbcodeparse file. You won't need to examine the entire function to find what's essential, since it's easy to spot:

PHP Code:
        // standard URL hyperlink
        
return "<a href=\"$rightlink\" target=\"_blank\">$text</a>"
Now you know this is where you have to hack the function. So what you want to do is to see if $rightlink contains the domain you want, mydomain.com, so you open the php manual and take a look at string functions... voila!
string stristr ( string haystack, string needle)

Returns all of haystack from the first occurrence of needle to the end. needle and haystack are examined in a case-insensitive manner.

If needle is not found, returns FALSE.
So you transform the portion of code you found into this:

PHP Code:
        if(stristr($rightlink,"domain.com"))
        {
            
// standard URL hyperlink
            
return "<a href=\"$rightlink\" target=\"_self\">$text</a>";
        }
        else
        {
            
// standard URL hyperlink
            
return "<a href=\"$rightlink\" target=\"_blank\">$text</a>";
        } 
So, we check if rightlink contains domain.com, and if it does, we change the target="_blank" portion, with target="self" so the link will be opened in the same window.

So what if we want to do the above for more domains? Here's a demonstration of point 4, alternate methods of doing the same thing...

PHP Code:
        if(stristr($rightlink,"domain.com"))
        {
            
// standard URL hyperlink
            
return "<a href=\"$rightlink\" target=\"_self\">$text</a>";
        }
        elseif(
stristr($rightlink,"seconddomain.com"))
        {
            
// standard URL hyperlink
            
return "<a href=\"$rightlink\" target=\"_self\">$text</a>";
        }
        else
        {
            
// standard URL hyperlink
            
return "<a href=\"$rightlink\" target=\"_blank\">$text</a>";
        } 

PHP Code:
    $domains = array( 
            
'domain.com'
            
'seconddomain.com' 
        
); 

        foreach(
$domains as $domain
        { 
            if(
stristr($rightlink,$domain)) 
            { 
                
// standard URL hyperlink 
                
return "<a href=\"$rightlink\" target=\"_self\">$text</a>"
            } 
            else 
            { 
                
// standard URL hyperlink 
                
return "<a href=\"$rightlink\" target=\"_blank\">$text</a>"
            } 
        } 
To make it easier and less repetitive, we made an array of domains, and then looped through them to compare each of them to $rightlink.

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

That's it! It's a lot easier than it seems, believe me. Then again, i'm not that good at explaining things. I hope this helps some of you.

Rake.

vblts.ru supports vBulletin®, 2022-2024