Ads shown within the post content.
You can choose different locations to insert ads from Above / Below / Inside / Middle Of / To the Left / To the Right of post content

', 'inpostads', 'In-Post Ad' ); } add_action( 'wp_ajax_wp_insert_inpostads_get_ad_form', 'wp_insert_inpostads_get_ad_form' ); function wp_insert_inpostads_get_ad_form() { wp_insert_get_ad_form( 'wp_insert_inpostads_primary_ad_code_type_change("###IDENTIFIER###"); wp_insert_inpostads_primary_ad_code_location_change_action("###IDENTIFIER###"); wp_insert_inpostads_vi_customize_adcode();' ); } add_action( 'wp_ajax_wp_insert_inpostads_save_ad_data', 'wp_insert_save_ad_data' ); add_action( 'wp_ajax_wp_insert_inpostads_delete_ad_data', 'wp_insert_delete_ad_data' ); add_filter( 'wp_insert_inpostads_form_accordion_tabs', 'wp_insert_inpostads_form_accordion_tabs_location', 10, 3 ); function wp_insert_inpostads_form_accordion_tabs_location( $control, $identifier, $location ) { echo '

Location

'; echo '
'; $paragraphPositioningOptions = [ [ 'text' => '1st', 'value' => '1', ], [ 'text' => '2nd', 'value' => '2', ], [ 'text' => '3rd', 'value' => '3', ], [ 'text' => '4th', 'value' => '4', ], [ 'text' => '5th', 'value' => '5', ], [ 'text' => '6th', 'value' => '6', ], [ 'text' => '7th', 'value' => '7', ], [ 'text' => '8th', 'value' => '8', ], [ 'text' => '9th', 'value' => '9', ], [ 'text' => '10th', 'value' => '10', ], ]; $control->add_control( [ 'type' => 'select', 'className' => 'input', 'style' => 'display: inline;', 'useParagraph' => false, 'optionName' => 'paragraphtopposition', 'options' => $paragraphPositioningOptions, ] ); $nthParagraphTopControl = $control->HTML; $control->clear_controls(); $control->add_control( [ 'type' => 'select', 'className' => 'input', 'style' => 'display: inline;', 'useParagraph' => false, 'optionName' => 'paragraphbottomposition', 'options' => $paragraphPositioningOptions, ] ); $nthParagraphBottomControl = $control->HTML; $control->clear_controls(); $location = ''; if ( ! isset( $control->values['location'] ) ) { switch ( $identifier ) { case 'above': $location = 'above'; break; case 'middle': $location = 'middle'; break; case 'below': $location = 'below'; break; case 'left': $location = 'left'; break; case 'right': $location = 'right'; break; default: $location = 'above'; break; } } else { $location = $control->values['location']; } $locations = [ [ 'text' => 'Above Post Content', 'value' => 'above', ], [ 'text' => 'Middle of Post Content', 'value' => 'middle', ], [ 'text' => 'Below Post Content', 'value' => 'below', ], [ 'text' => 'To the Left of Post Content', 'value' => 'left', ], [ 'text' => 'To the Right of Post Content', 'value' => 'right', ], [ 'text' => 'After ' . $nthParagraphTopControl . ' Paragraph in Post Content (From the Top)', 'value' => 'paragraphtop', ], [ 'text' => 'After ' . $nthParagraphBottomControl . ' Paragraph in Post Content (From the Bottom)', 'value' => 'paragraphbottom', ], ]; $control->add_control( [ 'type' => 'radio-group', 'style' => 'line-height: 40px; margin-top: 3px;', 'optionName' => 'location', 'options' => $locations, 'value' => $location, ] ); $control->create_section( 'Location' ); wp_insert_echo_html( $control->HTML ); $control->clear_controls(); echo '
'; return $control; } add_filter( 'wp_insert_inpostads_form_accordion_tabs', 'wp_insert_form_accordion_tabs_adcode', 20, 3 ); add_filter( 'wp_insert_inpostads_form_accordion_tabs', 'wp_insert_form_accordion_tabs_rules', 30, 3 ); add_filter( 'wp_insert_inpostads_form_accordion_tabs', 'wp_insert_form_accordion_tabs_geo_targeting', 40, 3 ); add_filter( 'wp_insert_inpostads_form_accordion_tabs', 'wp_insert_form_accordion_tabs_devices_styles', 50, 3 ); add_filter( 'wp_insert_inpostads_form_accordion_tabs', 'wp_insert_form_accordion_tabs_notes', 60, 3 ); add_filter( 'wp_insert_inpostads_form_accordion_tabs', 'wp_insert_inpostads_form_accordion_tabs_manual_override', 70, 3 ); add_filter( 'wp_insert_inpostads_form_accordion_tabs', 'wp_insert_inpostads_form_accordion_tabs_positioning', 80, 3 ); function wp_insert_inpostads_form_accordion_tabs_manual_override( $control, $identifier, $location ) { echo '

Manual Override

'; echo '
'; $control->set_HTML( '

[wpinsertinpostad id="' . $identifier . '"]

For those extreme cases when auto positioning just doesnt work out the way you want, use the shortcode above to precisely position your ad unit inside your post content.
Gutenberg users can utilize "Wp-Insert" blocks for manual positioning within post content.

' ); $control->create_section( 'Code to add to your post/page content' ); wp_insert_echo_html( $control->HTML ); $control->clear_controls(); echo '
'; return $control; } function wp_insert_inpostads_form_accordion_tabs_positioning( $control, $identifier, $location ) { echo '

Positioning

'; echo '
'; $control->add_control( [ 'type' => 'text', 'label' => 'Minimum Character Count', 'optionName' => 'minimum_character_count', 'helpText' => 'Show the ad only if the Content meets the minimum character count. If this parameter is set to 0 (or empty) minimum character count check will be deactivated.', ] ); $control->add_control( [ 'type' => 'text', 'label' => 'Paragraph Buffer Count', 'optionName' => 'paragraph_buffer_count', 'helpText' => 'Shows the ad after X number of Paragraphs. If this parameter is set to 0 (or empty) the ad will appear in the middle of the content.', ] ); $control->create_section( 'Positioning' ); wp_insert_echo_html( $control->HTML ); $control->clear_controls(); echo '
'; return $control; } /* End UI Functions */ /* Begin In-Post Ads Ad Insertion */ add_filter( 'the_content', 'wp_insert_inpostads_the_content', 100 ); function wp_insert_inpostads_the_content( $content ) { global $post; if ( ! is_feed() && is_main_query() ) { $inpostads = get_option( 'wp_insert_inpostads' ); if ( isset( $inpostads ) && is_array( $inpostads ) ) { $shortcodeInsertions = []; if ( preg_match_all( '/' . get_shortcode_regex() . '/s', $post->post_content, $matches ) && array_key_exists( 2, $matches ) && in_array( 'wpinsertinpostad', $matches[2] ) ) { foreach ( $matches[2] as $key => $value ) { if ( 'wpinsertinpostad' === $value ) { $shortcodeAtts = shortcode_parse_atts( $matches[3][ $key ] ); if ( isset( $shortcodeAtts ) && isset( $shortcodeAtts['id'] ) && ( $shortcodeAtts['id'] != '' ) ) { $shortcodeInsertions[] = $shortcodeAtts['id']; } } } } $paragraphCount = wp_insert_inpostads_get_paragraph_count( $content ); foreach ( $inpostads as $key => $inpostad ) { if ( ! in_array( $key, $shortcodeInsertions ) ) { if ( ! isset( $inpostad['location'] ) ) { //Get the location value from the key for old users who doesnt have a location saved. switch ( $key ) { case 'above': $inpostad['location'] = 'above'; break; case 'middle': $inpostad['location'] = 'middle'; break; case 'below': $inpostad['location'] = 'below'; break; case 'left': $inpostad['location'] = 'left'; break; case 'right': $inpostad['location'] = 'right'; break; default: $inpostad['location'] = 'above'; break; } } if ( wp_insert_get_ad_status( $inpostad ) ) { switch ( $inpostad['location'] ) { case 'above': $content = wp_insert_get_ad_unit( $inpostad ) . $content; break; case 'middle': if ( $paragraphCount > 1 ) { $paragraphBufferCount = $inpostad['paragraph_buffer_count'] ?? ''; $minimumCharacterCount = $inpostad['minimum_character_count'] ?? ''; if ( ( $paragraphBufferCount == 0 ) || ( $paragraphBufferCount == '' ) ) { $position = wp_insert_inpostads_get_insertion_position( '/p>', $content, round( $paragraphCount / 2 ) ); } else { $position = wp_insert_inpostads_get_insertion_position( '/p>', $content, $paragraphBufferCount ); } if ( $position ) { if ( ( $minimumCharacterCount == 0 ) || ( $minimumCharacterCount == '' ) ) { $content = substr_replace( $content, '/p>' . wp_insert_get_ad_unit( $inpostad ), $position, 3 ); } elseif ( strlen( wp_strip_all_tags( $content ) ) > $minimumCharacterCount ) { $content = substr_replace( $content, '/p>' . wp_insert_get_ad_unit( $inpostad ), $position, 3 ); } } } break; case 'below': $content = $content . wp_insert_get_ad_unit( $inpostad ); break; case 'left': $content = wp_insert_get_ad_unit( $inpostad, 'float: left;' ) . $content; break; case 'right': $content = wp_insert_get_ad_unit( $inpostad, 'float: right;' ) . $content; break; case 'paragraphtop': if ( $paragraphCount > 1 ) { $position = wp_insert_inpostads_get_insertion_position( '/p>', $content, ( $inpostad['paragraphtopposition'] ?? 0 ) ); if ( $position ) { $content = substr_replace( $content, '/p>' . wp_insert_get_ad_unit( $inpostad ), $position, 3 ); } } break; case 'paragraphbottom': if ( $paragraphCount > 1 ) { $paragraphbottomposition = ( $paragraphCount - (int) ( $inpostad['paragraphbottomposition'] ?? 0 ) ); if ( ( $paragraphbottomposition > 0 ) && ( $paragraphbottomposition < $paragraphCount ) ) { $position = wp_insert_inpostads_get_insertion_position( '/p>', $content, $paragraphbottomposition ); if ( $position ) { $content = substr_replace( $content, '/p>' . wp_insert_get_ad_unit( $inpostad ), $position, 3 ); } } } break; } } } } } } return $content; } function wp_insert_inpostads_get_paragraph_count( $content ) { $paragraphs = explode( '/p>', $content ); $paragraphCount = 0; if ( is_array( $paragraphs ) ) { foreach ( $paragraphs as $paragraph ) { if ( strlen( $paragraph ) > 1 ) { ++$paragraphCount; } } } return $paragraphCount; } /** * Byte offset of the $offset-th occurrence of $search within $content. * * @param string $search Delimiter to count (e.g. '/p>'). * @param string $content Content to scan. * @param int $offset 1-based occurrence to locate. * @return int|false Offset, or false when out of range. */ function wp_insert_inpostads_get_insertion_position( $search, $content, $offset ) { $offset = (int) $offset; $segments = explode( $search, $content ); if ( ( $offset <= 0 ) || ( $offset > max( array_keys( $segments ) ) ) ) { return false; } return strlen( implode( $search, array_slice( $segments, 0, $offset ) ) ); } /* End In-Post Ads Ad Insertion */ /* Begin In-Post Ads Shortcode Ad Insertion */ add_shortcode( 'wpinsertinpostad', 'wp_insert_inpostads_get_shortcode' ); function wp_insert_inpostads_get_shortcode( $atts ) { $atts = shortcode_atts( [ 'id' => '' ], $atts, 'wpinsertinpostad' ); if ( isset( $atts['id'] ) && ( $atts['id'] != '' ) ) { $adunit = get_option( 'wp_insert_inpostads' ); if ( isset( $adunit ) && isset( $adunit[ $atts['id'] ] ) && is_array( $adunit[ $atts['id'] ] ) && wp_insert_get_ad_status( $adunit[ $atts['id'] ] ) ) { return wp_insert_get_ad_unit( $adunit[ $atts['id'] ] ); } } } /* End In-Post Ads Shortcode Ad Insertion */