PHP code example of digitoimistodude / dude-really-simple-ads

1. Go to this page and download the library: Download digitoimistodude/dude-really-simple-ads library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

digitoimistodude / dude-really-simple-ads example snippets


add_filter( 'drsa_ad_placement_sizes', 'myprefix_register_ad_places' );
function myprefix_register_ad_places() {
    $spots = array(
        array(
            // title whichs shows when adding ads
            'name'      => __( 'Frontpage default' ),

            // unique identified for this placed, used to get ads
            'id'        => 'frontpage-default',

            // add size, uploaded image will be tested against these values
            'width'     => 480,
            'height'    => 480,

            // for having multiple ads in the same place, ads will be shown in the order of least shown to most shown
            'multiple'  => true,
        ),
    );

    return $spots;
}

$ad = false;
if ( function_exists( 'get_the_active_ad' ) ) {
    $ad = get_the_active_ad( 'frontpage-default' );
}

if ( $ad ) {
    echo '<a href="' . $ad['target'] . '" target="_blank" class="' . $ad['click_counter_class'] . '"><img src="' . $ad['src'] . '" class="ad ad-place-' . $ad['place'] . '"/></a>';
}

add_shortcode( 'ad', 'myprefix_shortcode_show_ad' );
function myprefix_shortcode_show_ad( $atts ) {
    if ( ! function_exists( 'get_the_active_ad' ) ) {
        return; // plugin not active, bail
    }

    if ( empty( $atts ) ) {
        return; // no attributes to shortcode, bail
    }

    // no ad place defined, show error to user and bail if visitor
    if ( ! isset( $atts['place'] ) ) {
        if ( is_user_logged_in() && current_user_can( 'edit_others_posts' ) ) {
            return __( 'No ad place defined', 'textdomain' );
        } else {
            return;
        }
    }

    // get the ad
    $ad = get_the_active_ad( $atts['place'] );

    // no active ad, show error to user and bail if visitor
    if ( ! $ad ) {
        if ( is_user_logged_in() && current_user_can( 'edit_others_posts' ) ) {
            return __( 'No active ads or campaigns', 'textdomain' );
        } else {
            return;
        }
    }

    // return ad html
    return '<a href="' . $ad['target'] . '" target="_blank" class="' . $ad['click_counter_class'] . '"><img src="' . $ad['src'] . '" class="ad ad-place-' . $ad['place'] . '"/></a>';
}