Skip to content

Instantly share code, notes, and snippets.

@aschweigert
Created August 10, 2017 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aschweigert/e12490cfd04cf0576946cbccbd1b8364 to your computer and use it in GitHub Desktop.
Save aschweigert/e12490cfd04cf0576946cbccbd1b8364 to your computer and use it in GitHub Desktop.
add_filter( 'dfw_network_code', 'mj_dfp_network_code' );
add_filter( 'dfw_breakpoints', 'mj_dfp_breakpoints' );
add_filter( 'dfw_targeting_criteria', 'mj_dfp_targeting' );
add_filter( 'dfw_mappings', 'mj_dfp_mapping' );
function mj_dfp_network_code() {
return 49189977;
}
function mj_dfp_breakpoints( $breakpoints ) {
$mj_breakpoints = array(
array(
'identifier' => 'mobile',
'min-width' => 0,
'max-width' => 629,
'option' => false,
),
array(
'identifier' => 'desktop',
'min-width' => 630,
'max-width' => 9999,
'option' => false,
),
);
foreach ( $mj_breakpoints as $breakpoint ) {
$breakpoints[] = $breakpoint;
}
return $breakpoints;
}
function mj_dfp_targeting( $targeting ) {
global $post;
if ( defined( 'VIP_GO_ENV' ) && VIP_GO_ENV ) {
$targeting['env'][] = VIP_GO_ENV;
}
if ( function_exists( 'largo_get_current_url' ) ) {
$targeting['url'][] = largo_get_current_url();
}
if ( is_singular() && mj_is_content_type( 'full_width_article', get_the_ID() ) ) {
$targeting['Page'][] = 'fullwidth';
}
return $targeting;
}
function mj_dfp_mapping( $mappings ) {
if ( is_single() ) {
// incontent ads are only served on desktop for full-widths.
$desktop_sizes = array();
if ( mj_is_content_type( 'full_width_article', get_the_ID() ) ) {
$desktop_sizes = array(
array( 970, 250 ),
array( 970, 90 ),
array( 728, 90 ),
);
}
// mobile is 300x250 on all articles.
$mappings['InContent'] = array(
array(
'browser' => array( 0, 1 ),
'ad_sizes' => array(
array( 300, 250 ),
),
),
array(
'browser' => array( 630, 1 ),
'ad_sizes' => $desktop_sizes,
),
);
}
return $mappings;
}
/**
* Wrapper for $doubleclick->place_ad to make sure plugin is active, etc.
*
* @param string $id the ad unit identifier from DFP.
* @param array $sizes the creative sizes that are valid for different breakpoints.
* @param string $before html to output before the ad tag.
* @param string $after html to output after the ad tag.
*/
function mj_place_ad( $id, $sizes, $args = array() ) {
global $mj, $doubleclick;
// bail if the dfp plugin isn't active.
if ( ! class_exists( 'DoubleClick_For_WordPress' ) ) {
return;
}
// bail if the hide_ads flag is set.
if ( isset( $mj['meta']['mj_hide_ads'] ) ) {
return;
}
$defaults = array(
'before' => '',
'after' => '',
'lazyLoad' => false,
'outofPage' => false,
);
$args = wp_parse_args( $args, $defaults );
if ( $args['before'] && '' !== $args['before'] ) {
echo wp_kses_post( $args['before'] );
}
$doubleclick->place_ad( $id, $sizes, $args );
if ( $args['after'] && '' !== $args['after'] ) {
echo wp_kses_post( $args['after'] );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment