Skip to main content

Callbacks functions

This page  tries to explain the usage of callback functions. callbacks are php-functions used to filter and manipulate feed data. Each item in a feed is parsed  by the callback function. In the feed configuration the default callback is set to generic_cb. You can change this to your own functions. Callback functions go into the feeds.php script, there is a 'edit callbacks' tab/link in the menu's

 

 

Generic structure callback functions

function yourcallback_cb(&$item) {
# your stuff here
generic_cb($item);
#more stuff here
}

Next to solving some encoding issues the function generic_cb maps the fields from the feed to the fields in the database.

So after the call to generic_cb you have the deeplink in href ( $item['href'] ) the fields from 'Field Selection' in menu_1 to menu_9, the group (Select 0) in menu_0, the name of the feed ( = merchant ) in the field feed and the price in prijs

If you insert you code before the call to generic_cb you must use the fields from the datafeed ( name, country etc) if you put your code after the call to generic_cb you must use the database fields ( title, menu_1 etc). This allows to use a single callback function for several feeds regardless the structure of the feed

Excluding items

You can exclude items by clearing the 'title' feed or the complete $item array:

function yourcallback_cb(&$item) {
generic_cb($item);
if ( condition ) {
$item['title']='';
}
}

for condition you can use any (pattern) matching on fields in the item array

examples

'Ugly' ==$item['menu_1'] 
excludes when Select 1 is exactly Ugly
preg_match("/ugly/i",$item['menu_1'] )
excludes any item having ugly as part the Select 1 field ( case incentive )
   

 

Changing Values

in the callback you can change any value as you like.

function travel_cb(&$item) {
generic_cb($item);
$item['menu_1']=str_ireplace("England","United Kingdom",$item['menu_1']);
}


Importing offers only

Assuming you have a feed with 'old'-prices and 'current'-prices, and you wish to import the offers only.

Assign the 'current' price to the field 'price' and the 'old' price to Select 9 ( you can pick any free  field)

function offersonly_cb(&$item) {
generic_cb($item);
#be sure your prices are localized ( thus 10.00 and not 10,00)
#otherwise:
#$item['prijs']=str_replace(',','.',$item['prijs']);
#$item['menu_9']=str_replace(',','.',$item['menu_9']);

if ( $item['prijs'] >= $item['menu_9'] ) { # this is not an offer
$item['title']=''; #exclude
return;
}
#put the offer in the Select 9 field.
$item['menu_9']= $item['menu_9']-$item['prijs'];

#append the offer to the description

$item['description'].="

You discount ". $item['menu_9']."

";

Cascading functions

similar feeds ofter share similar problems, for example renaming categories. To reuse and share code callback functions can simply call each other. There is just on catch, the function generic_cb must be called once, and just once.

Below a structure for solving this for 'fashion', each 'fashion' feed will call the 'fashion_cb' function ( configured in the feed configuration ) unless it need some special handling having it's own callback ( and the shared one)

 

{include_code_listing /var/www/www.affiliatefeeds.nl/images/code/shared-3098.txt}

 

Filtering on title

function mytitle_cb(&$item) {
 generic_cb($item);

if ( stripos($item['title'],'hay') !== false ) {
$item['title']=''; #exclude
return;
# OR #
item['menu_8']='hay';
}
}

Splitting Stuff

Many feeds have a complete tree in the 'category' instead of splitting their categories nicely for you. For example (from a tradedoubler CSV feed)

merchantcategoryname Donna > Scarpe > Derby

You can't assign this merchantcategoryname directly to Select1..Select3. The solution is using a callback function.

By default the callback function generic_cb is used. Instead of this function add and use a function splitting the category.

function generic_gt_cb(&$item) {
                generic_cb($item);
                list($item['menu_1'],$item['menu_2'],$item['menu_3'])=
explode(">",$item['menu_1'],3); }

>

Other frequent delimiters are ';','/' '-' and ' - '. The last to appear to have the same delimiter but will have a different effect on : womens - blue-jeans