I am trying to add a block to all my posts, i am using the_content Hook to add the shortcode, the code below adds the shortcode in the end of the content, is there something i can do to put the shortcode for example after 25% of the content?
add_filter( 'the_content', 'my_shortchode_in_single_page' );
function my_shortchode_in_single_page($content){
if(is_single())
return $content . do_shortcode('SHORTCODE HERE');
return $content;
}
Actually we can't found 25% of content but we can put it after a certain number of p tag. you can 0 to which number of p tag in content.
add_filter('the_content', 'ak_the_content_filter', 10, 1);
function ak_the_content_filter($content)
{
$parags = explode('</p>', $content);
$parags[0] .= '<br>'.do_shortcode('[SHORTCODE HERE]');// add whatever you want after first paragraph
$content_new = '';
foreach ($parags as $parag) {
$content_new .= $parag;
}
return $content_new;
}
Using the following code to remove images (and paste just the text)
<?php
$content = get_the_content();
$content = preg_replace("/<img[^>]+\>/i", " ", $content);
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
echo $content;
?>
Trying to bring in the images (in a new area of the same page) I a trying to use
preg_match('#(<img.*?>)#', $content, $results);
To no avail. And I am also wondering if there is a better way than to run a function twice, or If I can separate the text and image and call them into two different divs/locations
Get Only text Based content
function custom_strip_image($text) {
$text = preg_replace("/<img[^>]+\>/i", "", $text);
return $text;
}
echo custom_strip_image(get_the_content());
Make second function for get images list
So I have an xml feed that is for some reason displaying a price tag (I don't even know if this is a valid HTML tag and it is displayed as "price/" in angle brackets of course) under each individual blog post causing the feed not to validate (displaying 65 times). I would like to remove this from the feed and tried adding this piece of code to the functions.php file:
function rss_nopricetag($content) {
$content = preg_replace( '/<price/>/is', '', $content );
return $content;
}
add_filter('the_excerpt_rss', 'rss_nopricetag');
add_filter('the_content_feed', 'rss_nopricetag');
To no avail, I am using the below code to remove the tag from my feed and it is working just fine.
function rss_noiframe($content) {
$content = preg_replace( '/<iframe(.*)\/iframe>/is', '', $content );
return $content;
}
add_filter('the_excerpt_rss', 'rss_noiframe');
add_filter('the_content_feed', 'rss_noiframe');
What could I be doing wrong?
Try escaping the slash in the price regex.
$content = preg_replace( '/<price\/>/is', '', $content);
I am trying to customize a Word press template and having some trouble. The page is http://rexonmedia.com/?page_id=113 and under each section there are description, all the text in description is plain text(Actionscript, Visit site ...). I want the text to be formatted or have links like the source (http://rexonmedia.com/?portfolio=audiomaya) with hyperlink.
I came to find out that the culprit here is "strip_tags" tags. It's used on two different places. I tried couple of scenarios but didn't work. Please help
public function customFormat($content,$strip_tags = false,$shortcode=true){
$content = stripslashes($content);
if($shortcode)
$content = do_shortcode( shortcode_unautop( $content ) );
$content = preg_replace('#^<\/p>|^<br\s?\/?>|<p>$|<p>\s*( )?\s*<\/p>#', '', $content);
if($strip_tags)
$content = strip_tags($content,"<hades>,<tabend>");
return $content;
}
<?php
global $more; // Declare global $more (before the loop).
$more = 1;
$content = get_the_content('');
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
$this->shortenContent( $content_limit , strip_tags( $content ) );
?>
Supply strip_tags with the allowable tags. The line in the first block of code would change to:
$content = strip_tags($content,"<strong><a>");
And the line in the second block of code would change to:
$this->shortenContent( $content_limit , strip_tags( $content, "<strong><a>" ) );
By the way, the function defines $strip_tags as false in the function definition if it is not supplied. Check that!
public function customFormat($content,$strip_tags = false,$shortcode=true){
How do I set a character limit on the_content() and the_excerpt() in wordpress? I have only found solutions for the word limit - I want to be able to set an exact amount characters of outputted.
Or even easier and without the need to create a filter: use PHP's mb_strimwidth to truncate a string to a certain width (length). Just make sure you use one of the get_ syntaxes.
For example with the content:
<?php $content = get_the_content(); echo mb_strimwidth($content, 0, 400, '...');?>
Update 2022
mb_strimwidth breaks the HTML if the comment tag is used. use official wordpress wp_trim_words functions
<?php $content = get_the_content(); echo wp_trim_words( get_the_content(), 400, '...' );?>
This will cut the string at 400 characters and close it with ....
Just add a "read more"-link to the end by pointing to the permalink with get_permalink().
Read more
Of course you could also build the read more in the first line. Than just replace '...' with '[Read more]'
You could use a Wordpress filter callback function. In your theme's directory, locate or create a file called functions.php and add the following in:
<?php
add_filter("the_content", "plugin_myContentFilter");
function plugin_myContentFilter($content)
{
// Take the existing content and return a subset of it
return substr($content, 0, 300);
}
?>
The plugin_myContentFilter() is a function you provide that will be called each time you request the content of a post type like posts/pages via the_content(). It provides you with the content as an input, and will use whatever you return from the function for subsequent output or other filter functions.
You can also use add_filter() for other functions like the_excerpt() to provide a callback function whenever the excerpt is requested.
See the Wordpress filter reference docs for more details.
wp_trim_words This function trims text to a certain number of words and returns the trimmed text.
Example:-
echo wp_trim_words( get_the_content(), 40, '...' );
This also balances HTML tags so that they won't be left open and doesn't break words.
add_filter("the_content", "break_text");
function break_text($text){
$length = 500;
if(strlen($text)<$length+10) return $text;//don't cut if too short
$break_pos = strpos($text, ' ', $length);//find next space after desired length
$visible = substr($text, 0, $break_pos);
return balanceTags($visible) . " […]";
}
For Using the_content() functions (for displaying the main content of the page)
$content = get_the_content();
echo substr($content, 0, 100);
For Using the_excerpt() functions (for displaying the excerpt-short content of the page)
$excerpt= get_the_excerpt();
echo substr($excerpt, 0, 100);
Replace <?php the_content();?> by the code below
<?php
$char_limit = 100; //character limit
$content = $post->post_content; //contents saved in a variable
echo substr(strip_tags($content), 0, $char_limit);
?>
php substr() function refrence
php strip_tags() function refrence
wp_trim_words() This function trims text to a certain number of words and returns the trimmed text.
$excerpt = wp_trim_words( get_the_content(), 40, 'More Link');
Get truncated string with specified width using mb_strimwidth() php function.
$excerpt = mb_strimwidth( strip_tags(get_the_content()), 0, 100, '...' );
Using add_filter() method of WordPress on the_content filter hook.
add_filter( "the_content", "limit_content_chr" );
function limit_content_chr( $content ){
if ( 'post' == get_post_type() ) {
return mb_strimwidth( strip_tags($content), 0, 100, '...' );
} else {
return $content;
}
}
Using custom php function to limit content characters.
function limit_content_chr( $content, $limit=100 ) {
return mb_strimwidth( strip_tags($content), 0, $limit, '...' );
}
// using above function in template tags
echo limit_content_chr( get_the_content(), 50 );
just to help, if any one want to limit post length at home page .. then can use below code to do that..
the below code is simply a modification of #bfred.it Sir
add_filter("the_content", "break_text");
function limit_text($text){
if(is_front_page())
{
$length = 250;
if(strlen($text)<$length+10) return $text; //don't cut if too short
$break_pos = strpos($text, ' ', $length); //find next space after desired length
$visible = substr($text, 0, $break_pos);
return balanceTags($visible) . "... <a href='".get_permalink()."'>read more</a>";
}else{
return $text;
}
}
<?php
echo apply_filters( 'woocommerce_short_description', substr($post->post_excerpt, 0, 500) )
?>
I know this post is a bit old, but thought I would add the functions I use that take into account any filters and any <![CDATA[some stuff]]> content you want to safely exclude.
Simply add to your functions.php file and use anywhere you would like, such as:
content(53);
or
excerpt(27);
Enjoy!
//limit excerpt
function excerpt($limit) {
$excerpt = explode(' ', get_the_excerpt(), $limit);
if (count($excerpt)>=$limit) {
array_pop($excerpt);
$excerpt = implode(" ",$excerpt).'...';
} else {
$excerpt = implode(" ",$excerpt);
}
$excerpt = preg_replace('`\[[^\]]*\]`','',$excerpt);
return $excerpt;
}
//limit content
function content($limit) {
$content = explode(' ', get_the_content(), $limit);
if (count($content)>=$limit) {
array_pop($content);
$content = implode(" ",$content).'...';
} else {
$content = implode(" ",$content);
}
$content = preg_replace('/\[.+\]/','', $content);
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
return $content;
}