substr remove images from post content - php

I have a problem with getting the post content without the images.
I have a code like this:
$html="";
$html .= '<p class="news-text col-xl-12">'. preg_replace('/\s+?(\S+)?$/', '…',
substr(get_post_field('post_content', $post_id) .'', 0, 250)) .'</p>';
I have also tried:
substr(strip_tags( get_the_excerpt() ),0,250));
but I didn't have any luck with it. Could you please help me out?

i think this is ok
$content = get_post_field('post_content', $post_id);
$content = preg_replace("/<img[^>]+\>/i", "", $content);

Related

Seperating post text and images in different locations on the same page

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

Changing output markup of an media file

I am creating a wordpress theme for a friend and am trying to add some markup before and after an image that has been inserted using the media button on a post.
Here is what I have so far but it's giving me an error. Any suggestions or help would be much appreciated!
add_filter ('the_content', 'wpse264886_content_insert_html', 10, 1);
function wpse264886_content_insert_html ($content) {
$html1 = 'before ... ';
$html2 = 'after ...';
$content = preg_replace('/(<a.*?)><(img.*?)/', '$1>' . $html1 . '<$2>' . $html2, $content);
return $content;
}
Thanks!
Ponte

Show excerpt on front page using the_content(); and ACF

I need to show a excerpt on my home page. I have standard post and a custom post type 'webinar'. The CPT 'webinar' has a custom field, using ACF 'webinar_description' that is a an equivalent of 'description' in normal posts.
I am able to display both, by adding a filter for the 'webinar_description' like this:
add_filter('the_content','add_my_custom_field');
function add_my_custom_field($data) {
$data .= get_field('webinar_description');
return $data;
}
Now I have both post types displaying, but it displays the entire 'description' or 'webinar_description' field. I need to trim it at 40 words and add a '... Read More' with 'Read More' being a link to the article.
I have tried this, but it only works on the normal 'post' type field 'description' it doesn't work on my 'webinar' custom post type -> custom field 'webinar-description'
<?php $content = get_the_content(); echo mb_strimwidth($content, 0, 400, '[Read more]');?>
How can I create a filter or function that will limit both to 400 (or whatever) characters and add the link?
Not sure if this will help anyone in a similar situation, but this is how I solved it. First, in functions.php make the custom post type available
function cpt_get_excerpt(){
$excerpt = get_field('webinar_description');
$excerpt = preg_replace(" (\[.*?\])",'',$excerpt);
$excerpt = strip_shortcodes($excerpt);
$excerpt = strip_tags($excerpt);
$excerpt = substr($excerpt, 0, 400);
$excerpt = substr($excerpt, 0, strripos($excerpt, " "));
$excerpt = trim(preg_replace( '/\s+/', ' ', $excerpt));
$excerpt = $excerpt.'... <a class="c-drkGold" href="'.get_permalink($post->ID).'">Read More</a>';
return $excerpt;
}
Then where you want to display it, use a if/else statement based on the post type and display accordingly (this example is from static front page).
<?php
if( get_post_type() == 'post' ) {
?><p class="l-nmb"><?php
$content = get_the_content(); echo mb_strimwidth($content, 0, 400, '... Read more'); ?> </p>
<?php } else {
?><p class="l-nmb"><?php
$content = cpt_get_excerpt(); echo mb_strimwidth($content, 0, 400, '... Read more'); ?> </p>
<?php } ?>

removing block quote from post text - Wordpress

I have a Wordpress site and I want to remove the block quotes from the post and put just the regular text. (also want to remove any images that are in the text, I just want the regular text)
This code does the OPPOSITE of what I want - takes out the block quotes and posts that. I want it to post the other text and NOT the block quote.
<?php
// get the content
$block = get_the_content();
// check and retrieve blockquote
if(preg_match('~<blockquote>([\s\S]+?)</blockquote>~', $block, $matches))
// output blockquote
echo $matches[1];
?>
What you need is a content filter. Add the following to your functions.php file
add_filter( 'the_content', 'rm_quotes_and_images' );
function rm_quotes_and_images($content)
{
$content = preg_replace("~<blockquote>([\s\S]+?)</blockquote>~", "", $content);
$content = preg_replace("/<img[^>]+>/i", "", $content);
return $content;
}
Try this
add_filter( 'the_content', 'block_the_content_filter' );
function block_the_content_filter($content) {
$content = preg_replace("~<blockquote>([\s\S]+?)</blockquote>~", "", $content);
return $content;
}
Just add this to your code:
$content = preg_replace("~<blockquote>([\s\S]+?)</blockquote>~", "", $content);
$content = strip_tags($content, '<img>');
echo $content;
The way wali hassan sayed is to add following code to your function.php:
add_filter( 'the_content', 'block_the_content_filter' );
function block_the_content_filter($content) {
$content = preg_replace("~<blockquote>([\s\S]+?)</blockquote>~", "", $content);
$content = strip_tags($content, '<img>');
return $content;
}
This overrides the default "the_content()" function so in you page template you only need to call:
the_content();

Undoing a strip_tags in php

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){

Categories