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
Related
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.
Please I want to achieve the goal of excluding <h2>, <a> and <img> html tag from the below code. I am trying to replace tags keywords with links within post content, and exclude the tag link from affecting the html tags i listed above.
function link_words( $text ) {
$tags = get_tags();
if ( $tags ) {
foreach ( $tags as $tag ) {
$from = '/' . $tag->name . '/';
$to = sprintf( '%s', esc_url( get_term_link( $tag ) ), esc_html( $tag->name ) );
$text = preg_replace($from, $to , $text, 2);
}
}
return $text;
}
add_filter( 'the_content', 'link_words' );
Please Gurus in the house, is there any way out. I am new to wp function coding.
For simple HTML, you can do a str_replace:
$text = str_replace(array("<h2>","</h2>"), "", $text);
To strip those more complicated tags:
$text = preg_replace("/<img[^>]+\>/i", "", $text);
$text = preg_replace("/<a\s[^>]+\>/i", "", $text);
I just tested the code below and it appears to work:
function link_words( $text ) {
$text = str_replace(array("<h2>","</h2>","</a>"), "", $text);
$text = preg_replace("/<img[^>]+\>/i", "", $text);
$text = preg_replace("/<a\s[^>]+\>/i", "", $text);
return $text;
}
add_filter( 'the_content', 'link_words' );
I really frustrated, I am a novice in wordpress I am trying to Limit Maximum Numbers of Tag Links to appear within post content article. Below is my code. I don't know how to fix it up.
function link_words( $text ) {
$replace = array();
$tags = get_tags();
$count=0;
if ( $tags ) {
foreach ( $tags as $tag ) {
$count++;
$replace[ $tag->name ] = sprintf( '%s', esc_url( get_term_link( $tag ) ), esc_html( $tag->name ) );
if( $count > 2 ) break;
}
}
$text = str_replace( array_keys($replace), $replace, $text );
return $text;
}
add_filter( 'the_content', 'link_words' );
If your tags are comma-separated, this might work for you:
function limit_tags($tags) {
$tags = substr($tags, 0, strpos($tags, ',', strpos($tags, ',')+1));
return $tags;
}
add_filter('the_tags','limit_tags');
The $tags variable is actually a string...
You mention your function works the way you want it to (linking tags) so I won't mess with that. If you look at the docs for get_tags(), you'll see it accepts a few arguments including number which will limit it. This way you won't have a $counter type variable to mess with.
You can also just check to see if the $tags variable gets set to a truthy value, you don't need to define it first.
Your str_replace is also occurring whether or not $tags is defined, which can cause issues if none are found, so you should move that up into the if statement.
For semantic clarity, I've also changed the $text variable to $content since you're using the_content filter.
add_filter( 'the_content', 'link_tags_in_content' );
function link_tags_in_content( $content ){
if( $tags = get_tags( array( 'number' => 2 ) ) ){
foreach( $tags as $tag ){
$tag_link = sprintf( '%s', esc_url( get_term_link( $tag ) ), esc_html( $tag->name ) );
$content = str_replace( $tag->name, $tag_link, $content );
}
}
return $content;
}
Ok, so I think I understand what you want better now...
function link_words( $text ) {
$tags = get_tags();
if ( $tags ) {
foreach ( $tags as $tag ) {
$from = '!<h2>[^<>]*<\/h2>(*SKIP)(*F)|<b>[^<>]*<\/b>(*SKIP)(*F)|<a\b[^>]*>.*?</a>(*SKIP)(*F)|(\b'.$tag->name.'\b)(?=[^>]*(?:<|$))!';
$to = sprintf( ' %s ', esc_url( get_term_link( $tag ) ), esc_html( $tag->name ) );
$text = preg_replace($from, $to , $text, 2);
}
}
return $text;
}
add_filter( 'the_content', 'link_words' );
Because of the limit on preg_replace (2), and it being inside the tag-loop, this replaces two instances of a tag name in the text for every tag... Is this along the lines of what you want? Be aware that this is not case-insensitive, so it will not match if your tag is the first word in a sentence, and is capitalized. To do something like that, maybe take a look at this thread:
PHP preg_replace: Case insensitive match with case sensitive replacement
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;
});
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/