Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
/**
* Figure out the categories on the given page/post/cpt
*/
add_action( 'wp_head', 'ubm_category_detection' );
function ubm_category_detection() {
if ( !function_exists( 'p2p_register_connection_type' ) )
return;
$cat = 'surgery';
$subcats = '';
$cat_id = 10500;
// if ( is_singular( 'post' ) ) {
$connected_cats = new WP_Query( array(
'connected_type' => 'ubm_category_to_post',
'connected_items' => get_queried_object(),
'nopaging' => true,
) );
// }
if ( $connected_cats->have_posts() ) {
while ( $connected_cats->have_posts() ) : $connected_cats->the_post();
$cat = get_the_title();
$cat_id = get_the_id();
endwhile;
}
// if ( is_singular( 'post' ) ) {
$connected_subcats = new WP_Query( array(
'connected_type' => 'ubm_subcategory_to_post',
'connected_items' => get_queried_object(),
'nopaging' => true,
) );
// }
if ( $connected_subcats->have_posts() ) {
while ( $connected_subcats->have_posts() ) : $connected_subcats->the_post();
$subcats[] = get_the_title();
$subcat_ids[] = get_the_id();
endwhile;
}
if ( empty( $subcats ) ) {
$connected_subcats = new WP_Query( array(
'connected_type' => 'ubm_category_to_subcategory',
'connected_items' => $cat_id,
'nopaging' => true,
) );
if ( $connected_subcats->have_posts() ) {
while ( $connected_subcats->have_posts() ) : $connected_subcats->the_post();
$subcats[] = get_the_title();
$subcat_ids[] = get_the_id();
endwhile;
}
}
wp_reset_postdata();
$all_cats = array( $cat, $subcats );
ubm_javascript_output( $all_cats );
}
function ubm_javascript_output( $all_cats ) {
$cat = $all_cats[0];
$subcat = $all_cats[1];
$subcat = "'" . implode( "', '", $subcat ) . "'";
if ( current_user_can( 'edit_posts' ) ) {
echo '<div class="adcodesshown">';
echo '<strong>UBM Category:</strong> ' . $cat . '</br>';
echo '<strong>UBM Subcategories:</strong> ' . $subcat;
echo '</div>';
}
?>
<style>
.adcodesshown {
background-color: rgba( 0, 0, 0, 0.9 );
font-size: 12px;
color: white;
position: fixed;
padding: 10px;
left: 0;
bottom: 0;
z-index: 998;
}
.ubm_code {
background-color: rgba( 0,0,0, 0.2 ) !important;
padding: 0 !important;
position: relative;
}
html {
color: white;
}
pre {
font-size: 14px;
background-color: white;
}
.ad-admin-info {
background-color: rgba( 0,0,0, 0.9 );
color: white;
font-size: 12px;
position: absolute;
top: 0;
left: 0;
line-height: 1.2em;
z-index: 999;
display: block;
padding: 5px 7px;
text-align: left;
}
</style>
<!-- First required script for UBM (this one is constant and doesn't change) -->
<script type='text/javascript'>
var googletag = googletag || {};
googletag.cmd = googletag.cmd || [];
(function() {
var gads = document.createElement('script');
gads.async = true;
gads.type = 'text/javascript';
var useSSL = 'https:' == document.location.protocol;
gads.src = (useSSL ? 'https:' : 'http:') +
'//www.googletagservices.com/tag/js/gpt.js';
var node = document.getElementsByTagName('script')[0];
node.parentNode.insertBefore(gads, node);
})();
</script>
<!-- Second required script for UBM (this one has quite a bit going into it) -->
<script type='text/javascript'>
googletag.cmd.push(function() {
<?php ubm_generate_js( $cat, $subcat ); ?>
googletag.pubads().enableSingleRequest();
googletag.enableServices();
});
</script>
<?php
}
function ubm_generate_js( $cat, $subcat ) {
/**
* Loop through all of the widgets, grabbing the UBM ones, then extract the necessary options from those.
*/
global $wp_registered_widgets;
foreach ( $wp_registered_widgets as $widget ) {
if ( $widget[ 'name' ] == 'UBM Ad Code' ) {
// The ID is the widget ID number, e.g. ubm_code-2
$id = $widget[ 'id' ];
// The div_id is the id of the dive the ad will output in, e.g. ad-ubm_code-2
$div_id = 'ad-' . $id;
// The number is the plain number of the widget, which we'll need to get the widget settings
$number = $widget['params'][0]['number'];
$var = 'widget' . $number;
// We dig into the settings to grab the size this ad is set to
$widget_settings = new ubm_code();
$settings = $widget_settings->get_settings();
$size = $settings[$number]['size'];
// Break that down into height and width
$ad_dimensions = explode( ', ', $size );
$width = $ad_dimensions[0];
$height = $ad_dimensions[1];
// Get the ad position
$position = $settings[$number]['position'];
if ( $position != '' ) {
// All the magic is happening right here, where we're outputting the individual script for each ad:
?>
var <?php echo $var; ?> = googletag.sizeMapping().addSize([3000, 1100], [<?php echo $width; ?>, <?php echo $height; ?>]).build();
googletag.defineSlot('/4688/ubm.insidesurgery/<? echo $cat; ?>', [<?php echo $width; ?>, <?php echo $height; ?>], '<?php echo $div_id; ?>').setTargeting('pos', ['<?php echo $position; ?>']).setTargeting('cat', [<?php echo $subcat; ?>]).defineSizeMapping(<?php echo $var; ?>).addService(googletag.pubads());
<?php
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.