Got a moment to help with a php hack?

4 replies
Hi there

I'm looking for some help with a php hack.

I'm using the Random Ads Rotator plugin from datafeedr.com

There is a single line include that you can put anywhere in your template which looks like this:

Code:
<?php if (function_exists('dfrad')) { echo dfrad('ad_468x60'); } ?>
The 'ad_468x60' is a default value that calls ad_468x60.txt

You can change that to whatever you like.

Let's say I have three categories on my WP blog: "tomatoes", "oranges", and "ice cream".

I'd like to place that php-include, above, in the template and then call ads depending on the category of the post.

So, for example...

If a post is about tomatoes, then it returns the 468x60 banners about things related to tomatoes (and rotates them).

If a post is about oranges, then it returns...

...and so on.

But I'm not a coder. The way I understand that code, above, is that it asks the script to check if the "dfrad" function exists and if it does, to serve whatever I've put into the 'ad_xxx' value.

So this is my completely ignorant attempt to hack the code so that there are two if statements and a series of options.

I'm quite sure it's wrong -- which is why I'm asking for help -- because I know next to nothing about php.

Any help would be greatly appreciated.

Cheers,
TheNightOwl



Code:
<?php 

if (($function_exists=="dfrad" && $category=="tomatoes"))
{ echo dfrad('ads_tomatoes_468x60'); }

or

if (($function_exists=="dfrad" && $category=="oranges"))
{ echo dfrad('ads_oranges_468x60'); }

or

if (($function_exists=="dfrad" && $category=="icecream"))
{ echo dfrad('ads_icecream_468x60'); }

else { echo('ads_meat_300x300'); }

?>
P.S. I've included an else command there as well in the event that I add a category to my blog later and, say, forget (or don't have time) to create ads to rotate. Until I create them, some generic ads or announcements, or quotations, or joke of the day banners, or whatever, will show.
#hack #moment #php
  • Profile picture of the author maxleadford
    You've changed the "function_exists" function to a variable, so you'll want to change that back to function_exists('dfrad'). Fix that up and it should work like you expect it to.

    Also, you want to separate your conditional testing so that the "else" that pulls up your ads_meat_300x300 only gets called if the "dfrad" function does exist. See below...

    Also, if you want to get a touch more advanced so you're not staring at a million conditionals...
    PHP Code:

    function thenightowl_puts_an_ad_here($category_name$ad_size)
    $default_ad_file 'ads_meat_300x300';

    if (
    function_exists('dfrad'))  # does the ad function exist?
    {
        if (
    $category ''# make sure a category is entered
        
    {
            
    $ad_file 'ads_' $category '_468x60';
            if (
    is_file($ad_file)) # make sure ad file exists
            
    { echo dfrad($ad_file); }
        }
        else
        {
            
    $ad_file $default_ad_file;
            if (
    is_file($ad_file)) # make sure ad file exists
            
    { echo dfrad($ad_file); }
        }

    This will allow you to just alter the $category variable with the Ad type. You could do something similar by changing the
    '_468x60'
    to
    '_' . $ad_size
    and then setting the ad size on the fly as well. If want, wrap the whole thing in a new function...

    function category_dfrad($category) { /* put the code from above in here */ } and then just call category_dfrad('icecream') whenever you're placing a category advert.

    Hope this helps!

    To growth and success,

    Max Leadford
    {{ DiscussionBoard.errors[996466].message }}
  • Profile picture of the author TheNightOwl
    Heya Max

    Thanks very much!

    Now if only I could understand what you're saying! LOL!

    This could be a classic, textbook case of "How much?"

    I'll PM you.

    Cheers,
    TheNightOwl
    Signature
    {{ DiscussionBoard.errors[998420].message }}
  • Profile picture of the author maxleadford
    Nighty,

    I couldn't possibly charge you for something like this. That'd just be wrong.

    Below is the wrapper function with four calls to the wrapper function and what file each function call would be seeking.

    This isn't the most graceful method, but it should be pretty easy to break down and understand if you're so inclined. And more importantly, it'll work! ;-)

    [Feel free to change the function name...]

    PHP Code:
    <?php

    function thenightowl_puts_an_ad_here($category=null$size=null)
    {
        if (
    function_exists('dfrad'))  # does the ad function exist?
        
    {
            if (!
    $category) { $category 'meat'; }  # if $category empty, set default category
            
    if (!$size) { $size '468x60'; }  # if $size empty, set default size
            
            # build $ad_file name
            
    $ad_file 'ads_' $category '_' $size '.txt';
                
            if (
    is_file($ad_file)) # make sure ad file exists
            
    { echo dfrad($ad_file); }  # load ad file & output
        
    }  
    }

    thenightowl_puts_an_ad_here('icecream');
    # output - ads_icecream_468x60.txt

    thenightowl_puts_an_ad_here('pickles''125x125');
    # output - ads_pickles_125x125.txt

    thenightowl_puts_an_ad_here('anklesocks''300x300');
    # output - ads_anklesocks_300x300.txt

    thenightowl_puts_an_ad_here();
    # output - ads_meat_468x60.txt
    ?>
    To growth and success,

    Max Leadford
    {{ DiscussionBoard.errors[1004400].message }}
  • Profile picture of the author TheNightOwl
    Thank you, Max! Very kind. Really.

    From PM:
    Originally Posted by maxleadford

    I can't charge you for that. That's just silly. Your wrapper function is waiting in the thread...
    I'll give that a run. I think I can get that sucker to work, now. I'm still laughing at my total ignoramus attempt to code it, not even knowing the correct syntax! Ha ha! Too funny.

    I appreciate your help, greatly.

    Best regards,
    TheNightOwl

    P.S.
    "anklesocks"? Now you're just messin' with me, ain'tcha? Didn't think I'd catch that one eh, wiseguy...

    P.P.S. On a serious note: I may have follow-up questions. You should charge me for that. I'm guessing this stuff is a snap for you, but it's not for me. And I don't really have the time or inclination to learn anything but the most rudimentary php for now. It's your time and expertise, y'know. *shrug*

    I'm not inviting you or anyone else to charge me stupid, extortionist rates for simple work, obviously, but I don't mind saying thanks in a way that'll contribute to the next Boys' Cards Night or whatever, y'know.

    Thanks again.
    Signature
    {{ DiscussionBoard.errors[1005534].message }}

Trending Topics