Display sidebar after x paragraphs - wordpress

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 !

Related

How to add target blank in this woocommerce code

find this code in the business bloomber. I want to add target blank attribute in this code can any help me?
function bbloomer_image_link_external_url( $html,
$post_thumbnail_id ) {
global $product;
if ( ! $product->is_type( 'external' ) ) return $html;
$url = $product->add_to_cart_url();
$pattern = "/(?<=href=(\"|'))[^\"']+(?=(\"|'))/";
$html = preg_replace( $pattern, $url, $html);
return $html ;
}
You can use str_replace. just add the below line after preg_replace.
$html = str_replace('<a', '<a target="_blank" ', $html);
Complete code.
function bbloomer_image_link_external_url( $html, $post_thumbnail_id ) {
global $product;
if ( ! $product->is_type( 'external' ) ) return $html;
$url = $product->add_to_cart_url();
$pattern = "/(?<=href=(\"|'))[^\"']+(?=(\"|'))/";
$html = preg_replace( $pattern, $url, $html );
$html = str_replace('<a', '<a target="_blank" ', $html);
return $html;
}
add_filter( 'woocommerce_single_product_image_thumbnail_html', 'bbloomer_image_link_external_url', 100, 2 );
Tested and works.

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;
}

How to make a change to a file in wp-includes?

I'm using the Hestia theme. I created a child theme for Hestia.
Hestia theme
wp-content/themes/hestia
Child Theme
wp-content/themes/hestia-child
I need to make a change in the file wp-includes/general-template.php
How Can I make the change below without directly altering the file from wp-includes?
wp-content/themes/hestia/archive.php
<?php the_archive_title( '<h1 class="hestia-title">', '</h1>' ); ?>
wp-includes/general-template.php
// This function calls get_the_archive_title
function the_archive_title( $before = '', $after = '' ) {
$title = get_the_archive_title();
if ( ! empty( $title ) ) {
echo $before . $title . $after;
}
}
// Original
function get_the_archive_title() {
if ( is_author() ) {
$title = sprintf( __( 'Author: %s' ), '<span class="vcard">' . get_the_author() . '</span>' );
}
}
// What I need to change - 'Author: %s' to '%s'
function get_the_archive_title() {
if ( is_author() ) {
$title = sprintf( __( '%s' ), '<span class="vcard">' .get_the_author() . '</span>' );
}
}
Some "filter" and "hook" putting this in your functions.php :
Please check below reference link.
Link1 :- https://wordpress.stackexchange.com/questions/60605/function-to-change-a-label-username-in-a-core-wordpress-file-wp-includes-gene/60607?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
Link2 :- https://wordpress.stackexchange.com/questions/255677/how-to-modify-files-inside-wp-includes-directory-in-wordpress?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
add_filter( 'get_the_archive_title', function ( $title ) {
if( is_category() ) {
$title = single_cat_title( '', false );
}
return $title;
});

how to insert advertisement code into wordpress post

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/

WordPress add filter to wp_get_attachment_link

I need custom class for filter to wp_get_attachment_link. So what I so:
function modify_attachment_link( $markup ) {
global $post;
return str_replace( '<a href', '<a class="view" rel="galleryid-'. $post->ID .'" href', $markup );
}
add_filter( 'wp_get_attachment_link', 'modify_attachment_link' );
It's work fine. But what I need to do in case if Link thumbnails to: Attachment Page
I mean I don't need a custom class at this case. Any help please?
And core function for wp_get_attachment_link is:
function wp_get_attachment_link( $id = 0, $size = 'thumbnail', $permalink = false, $icon = false, $text = false ) {
$id = intval( $id );
$_post = & get_post( $id );
if ( empty( $_post ) || ( 'attachment' != $_post->post_type ) || ! $url = wp_get_attachment_url( $_post->ID ) )
return __( 'Missing Attachment' );
if ( $permalink )
$url = get_attachment_link( $_post->ID );
$post_title = esc_attr( $_post->post_title );
if ( $text )
$link_text = esc_attr( $text );
elseif ( $size && 'none' != $size )
$link_text = wp_get_attachment_image( $id, $size, $icon );
else
$link_text = '';
if ( trim( $link_text ) == '' )
$link_text = $_post->post_title;
return apply_filters( 'wp_get_attachment_link', "<a href='$url' title='$post_title'>$link_text</a>", $id, $size, $permalink, $icon, $text );
}
So I mean if ( $permalink ) I don't need to add custom class for this function.
Try
function modify_attachment_link( $markup, $id, $size, $permalink ) {
global $post;
if ( ! $permalink ) {
$markup = str_replace( '<a href', '<a class="view" rel="galleryid-'. $post->ID .'" href', $markup );
}
return $markup;
}
add_filter( 'wp_get_attachment_link', 'modify_attachment_link', 10, 4 );
That may work

Resources