how to insert advertisement code into wordpress post - wordpress

I want to insert advertisement code into WordPress post(detail page) after or before first paragraph. like this post is there any plugin or any idea..

You can try the plugin Ad Inserter.
Or use the example from this WPBeginner tutorial:
<?php
//Insert ads after second paragraph of single post content.
add_filter( 'the_content', 'prefix_insert_post_ads' );
function prefix_insert_post_ads( $content ) {
$ad_code = '<div>Ads code goes here</div>';
if ( is_single() && ! is_admin() ) {
return prefix_insert_after_paragraph( $ad_code, 2, $content );
}
return $content;
}
// Parent Function that makes the magic happen
function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
$closing_p = '</p>';
$paragraphs = explode( $closing_p, $content );
foreach ($paragraphs as $index => $paragraph) {
if ( trim( $paragraph ) ) {
$paragraphs[$index] .= $closing_p;
}
if ( $paragraph_id == $index + 1 ) {
$paragraphs[$index] .= $insertion;
}
}
return implode( '', $paragraphs );
}

Here is the code for showing ad randomly inside WordPress post content-
//Insert ads between first and fourth paragraph of single post content to show it randomly between first and second paragraph.
add_filter( 'the_content', 'prefix_insert_post_ads' );
function prefix_insert_post_ads( $content ) {
// Add code for mobile
$ad_code_mobile = 'AD CODE FOR MOBILE';
// Add code for PC
$ad_code_pc = 'AD CODE FOR PC/DESKTOP/LAPTOP';
if ( is_single() && ! is_admin() ) {
if (!wp_is_mobile()) {
$randnumpc = mt_rand(1,4);
return prefix_insert_after_paragraph( $ad_code_pc, $randnumpc, $content );
}
else {
$randnummobi = mt_rand(1,4);
return prefix_insert_after_paragraph( $ad_code_mobile, $randnummobi, $content );
}
}
return $content;
}
// Parent Function that makes the magic happen
function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
$closing_p = '</p>';
$paragraphs = explode( $closing_p, $content );
foreach ($paragraphs as $index => $paragraph) {
if ( trim( $paragraph ) ) {
$paragraphs[$index] .= $closing_p;
}
if ( $paragraph_id == $index + 1 ) {
$paragraphs[$index] .= $insertion;
}
}
return implode( '', $paragraphs );
}
That code will show ad randomly within paragraph number 1 and 4. You can place this code at the end of your functions.php of your theme or you can create a separate plugin for this purpose.
Source: https://www.eyeswift.com/random-ad-code-in-wordpress-post-content-without-plugin/

Related

Shortcode that display a single product attribute for a WooCommerce product

I am looking for a way to display a specific attribute on the single Product page of WooCommerce.
I found this code in the folling topic; Shortcode that display all product attributes set for a WooCommerce product
function get_product_attributes_shortcode($atts ) {
// Extract shortcode attributes
extract( shortcode_atts( array(
'id' => get_the_ID(),
), $atts, 'display-attributes' ) );
global $product;
if ( ! is_a($product, 'WC_Product') ) {
$product = wc_get_product( $id );
}
if ( is_a($product, 'WC_Product') ) {
$html = []; // Initializing
foreach ( $product->get_attributes() as $attribute => $values ) {
$attribute_name = wc_attribute_label($values->get_name());
$attribute_data = $values->get_data();
$is_taxonomy = $attribute_data['is_taxonomy'];
$option_values = array(); // Initializing
// For taxonomy product attribute values
if( $is_taxonomy ) {
$terms = $values->get_terms(); // Get attribute WP_Terms
// Loop through attribute WP_Term(s)
foreach ( $terms as $term ) {
$term_link = get_term_link( $term, $attribute );
$option_values[] = ''.$term->name.'';
}
}
// For "custom" product attributes values
else {
// Loop through attribute option values
foreach ( $values->get_options() as $term_name ) {
$option_values[] = $term_name;
}
}
$html[] = '<strong>' . $attribute_name . '</strong>: ' . implode(', ', $option_values);
}
return '<div class="product-attributes">' . implode(' | ', $html) . '<div>';
}
}
add_shortcode( 'display-attributes', 'get_product_attributes_shortcode' );
I can display all attributes by using the shortcode [display-attributes] but I want to display only one specific attribute (in my case the attribute is "Kunstenaar"). However I don't know how to adjust the code to make this work. Anyone who can help me out?
If you need just specific attribute, you don't have to get them all and then go through them. WC have function get_attribute() - documentation
function get_product_attributes_shortcode( $atts ) {
extract( shortcode_atts( array(
'id' => get_the_ID(),
), $atts, 'display-attributes' ) );
global $product;
if ( ! is_a($product, 'WC_Product') ) {
$product = wc_get_product( $id );
}
if ( is_a($product, 'WC_Product') ) {
$kunstenaar = $product->get_attribute( 'Kunstenaar' );
return '<div class="product-attributes"><strong>Kunstenaar</strong>: ' . $kunstenaar . '<div>';
}
}
add_shortcode( 'display-attributes', 'get_product_attributes_shortcode' );

Creating a custom tag / field with Contact Form 7

I want to create a custom textrea field in Contact form 7. Were I can pre-populate it with data. I have the below which shows on the front end and creates a text area field. But when I submit the form the tag is always blank within the email so the data doesn't get emailed. The tag also does not show in the list of available tags in Contact form 7. Any ideas?
add_action( 'wpcf7_init', 'wpcf7_add_form_tag_pathtag' );
function wpcf7_add_form_tag_pathtag() {
wpcf7_add_form_tag(
array( 'pathtag', 'pathtag*'), 'pathtag_form_tag_handler', array( 'name-attr' => true )
);
}
function pathtag_form_tag_handler( $tag ) {
$tag = new WPCF7_FormTag( $tag );
if ( empty( $tag->name ) ) {
return '';
}
$atts = array();
$class = wpcf7_form_controls_class( $tag->type );
$atts['class'] = $tag->get_class_option( $class );
$atts['id'] = $tag->get_id_option();
$atts['name'] = $tag->name;
$atts = wpcf7_format_atts( $atts );
$output = '';
$output .= '<textarea cols="40" rows="10">';
foreach ( $_SESSION['question-path'] as $path ) {
$output .= $path ."\n\n";
}
$output .= '</textarea>';
return $output;
}
You did not add the attributes to the textarea code. Also, make sure you enter your shortcode in the contact form as
[pathtag field-name]
function pathtag_form_tag_handler($tag) {
$tag = new WPCF7_FormTag($tag);
if (empty($tag->name)) {
return '';
}
$atts = array();
$class = wpcf7_form_controls_class($tag->type);
$atts['class'] = $tag->get_class_option($class);
$atts['id'] = $tag->get_id_option();
$atts['name'] = $tag->name;
$atts = wpcf7_format_atts($atts);
$output = '';
// Make sure to add the $class and $atts to the form tag
$output .= sprintf('<textarea cols="40" rows="10" %s %s>', $class, $atts);
foreach ($_SESSION['question-path'] as $path) {
$output .= $path . "\n\n";
}
$output .= '</textarea>';
return $output;
}

Display sidebar after x paragraphs

I am looking for a way to display a sidebar dynamically after x number of paragraphs in my content.
Problem : dynamic_sidebar (' name ') doesn't display text : var_dump($ad_code) = bool(true).
Result : My sidebar is displayed twice in the header, once before the content and at the right paragraphs it displays the number "1".
add_filter( 'the_content', 'prefix_insert_post_ads' );
function prefix_insert_post_ads( $content ) {
$ad_code = dynamic_sidebar( 'sidebar-6' );
if ( is_single() && ! is_admin() ) {
return prefix_insert_after_paragraph( $ad_code, 1, $content );
}
return $content;
}
function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
$closing_p = '</p>';
$paragraphs = explode( $closing_p, $content );
foreach ($paragraphs as $index => $paragraph) {
if ( trim( $paragraph ) ) {
$paragraphs[$index] .= $closing_p;
}
if ( $paragraph_id == $index + 1 ) {
$paragraphs[$index] .= $insertion;
}
}
return implode( '', $paragraphs );
}
I just add this :
ob_start();
dynamic_sidebar('sidebar-id');
$sidebar = ob_get_contents();
ob_end_clean();
It's working !

How to hide oembed links from wordpress excerpt?

the_excerpt() function shows all oembeded links.
In this case is about a youtube video: http://i.stack.imgur.com/MP3EF.png
Adding the following code to the functions.php file (backing up the file first just in case you break the page):
function remove_links($text)
{
if ('' == $text )
{
$pattern = '~http://[^\s]*~i'; //what we want to remove, the http link
$text = get_the_content('');
$text = preg_replace($pattern, '', $text);
}
return $text;
}
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'remove_links');
This will override the default excerpt() function and look for the link and remove it.
Building on #Howli's answer, here's a modified version that also trims the excerpt and applies the proper filters:
function tect_excerpt( $text ) {
if ( '' == $text ) {
$text = get_the_content('');
$text = strip_shortcodes( $text );
$text = preg_replace( '~http(s)?://[^\s]*~i', '', $text ); // removes URLs
$text = apply_filters( 'the_content', $text );
$text = str_replace(']]>', ']]>', $text);
$excerpt_length = apply_filters( 'excerpt_length', 20 ); // default: 55
$excerpt_more = apply_filters( 'excerpt_more', ' ' . '[…]' );
$text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
}
return $text;
}
remove_filter( 'get_the_excerpt', 'wp_trim_excerpt' );
add_filter( 'get_the_excerpt', 'tect_excerpt');
You may use wp_filter_nohtml_kses function:
echo wp_filter_nohtml_kses(get_the_excerpt());
which will remove the and opening and closing tags

facebook like button and pagination implementation in wordpress

I need advice,
I've installed facebook/google+ on my wordpress category pages.
For example: http://www.sandrophoto.com/category/famous-photographers/
First page has allot of likes, but second and third page only few. This pushed me to think, maybe I should unite those pages into one.
And instead of having separate 'like button' for urls, I would point button only to first page.
http://www.sandrophoto.com/category/famous-photographers/page/2/
http://www.sandrophoto.com/category/famous-photographers/page/3/
Does this make sense?
Any ideas how to implement this? I use this currently:
//get current archives url for fb like and open graph
function get_current_archive_link( $paged = true ) {
$link = false;
if ( is_front_page() ) {
$link = home_url( '/' );
} else if ( is_home() && "page" == get_option('show_on_front') ) {
$link = get_permalink( get_option( 'page_for_posts' ) );
} else if ( is_tax() || is_tag() || is_category() ) {
$term = get_queried_object();
$link = get_term_link( $term, $term->taxonomy );
} else if ( is_post_type_archive() ) {
$link = get_post_type_archive_link( get_post_type() );
} else if ( is_author() ) {
$link = get_author_posts_url( get_query_var('author'), get_query_var('author_name') );
} else if ( is_archive() ) {
if ( is_date() ) {
if ( is_day() ) {
$link = get_day_link( get_query_var('year'), get_query_var('monthnum'), get_query_var('day') );
} else if ( is_month() ) {
$link = get_month_link( get_query_var('year'), get_query_var('monthnum') );
} else if ( is_year() ) {
$link = get_year_link( get_query_var('year') );
}
}
}
if ( $paged && $link && get_query_var('paged') > 1 ) {
global $wp_rewrite;
if ( !$wp_rewrite->using_permalinks() ) {
$link = add_query_arg( 'paged', get_query_var('paged'), $link );
} else {
$link = user_trailingslashit( trailingslashit( $link ) . trailingslashit( $wp_rewrite->pagination_base ) . get_query_var('paged'), 'archive' );
}
}
return $link;
}
For both platforms allow you to specify the URL you want Liked/Plused, rather than them trying to determine the URL from the page location and or other meta data. Just specify the same URL in the plugin code to be used on both pages.
Facebook HTML5 version of the plugin would need to be updated like:
<div class="fb-like" data-href="{your URL}"></div>
and Plus 1
<g:plusone annotation="inline" href="{your URL}"></g:plusone>

Resources