WordPress.org

Ready to get started?Download WordPress

Forums

Random / Rotating Ads V2
Ads in post content (7 posts)

  1. marchenko.alexandr
    Member
    Posted 3 years ago #

    So if You want to add your ads into post body, all you need is add following code to wp-content/plugins/datafeedr-ads/datafeedr-ads.php

    function datafeedr_ads_filter($content) {
    	preg_match_all('/\[ad-([^\]]+)\]/six', $content, $matches);
    
    	if(!empty($matches)) {
    		$codes = $matches[0];
    		$ids = $matches[1];
    
    		for($i = 0; $i < count($codes); $i++) {
    			$code = $codes[$i];
    			$id = $ids[$i];
    			$ad = '';
    			if (function_exists('dfrads')) {
    				$ad = dfrads($id);
    			}
    			$content = str_replace($code, $ad, $content);
    		}
    
    		return $content;
    	}
    
    	return $content;
    }
    add_filter('the_content', 'datafeedr_ads_filter');

    And now anywhere in post content you may add code like following:

    [ad-4298077]

    where 2346323 copy pasted from Template code: <?php if (function_exists('dfrads')) { echo dfrads('4298077'); } ?>

  2. Isaakwells
    Member
    Posted 3 years ago #

    I try this method and every time I get a message saying the add does not exist? I wonder why?

  3. Lin Robinson
    Member
    Posted 3 years ago #

    I try it and get nothing showing at all. Any tips?

  4. marchenko.alexandr
    Member
    Posted 3 years ago #

    First of all u need to check is filter working at all. For this try change return lines from:

    return $content;

    to something like:

    return "Hello world $content";

    And refresh your page. If u will see "Hello world" at start - all ok, filter works fine.

    Then u may check is dfrads function actualy exists, change code:

    if (function_exists('dfrads')) {
    	$ad = dfrads($id);
    }

    to:

    if (function_exists('dfrads')) {
    	$ad = dfrads($id);
    } else return "FUNCTION DOES NOT EXISTS";

    And again refresh page, if function exists nothing will happen, otherwise u will see the message.

    And last one, if filter works, and function exists there is probably problems with regex, or maybe you are entering wrong id for ad.

  5. b-summers
    Member
    Posted 3 years ago #

    Thank you I got this method working, however is there a way to show all of your ads on a page, rather than just displaying one at a time?

  6. marchenko.alexandr
    Member
    Posted 3 years ago #

    probably u can not do this with this plugin - as i remember it works that way - so u can show only one banner at a time

    if u want more specific system and dont wan programm anything probably you should use something like google.com/dfp

  7. RobinInTexas
    Member
    Posted 2 years ago #

    Just installed the code from the first post. Works perfectly.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic