Removing Images from a WordPress Post - php

I wanted to remove the images from a WordPress post to give me more control of how I could layout the design for the front page of a website. After doing some messing around and failing, I finally found a great post which had an amazing little piece of code that solved my problem.
<?php
$content = get_the_content();
$postOutput = preg_replace('/<img[^>]+./','', $post->post_content);
echo $postOutput;
?>
But some time i have link above images like:
<img src="PATH_IMAGES">
So, How can remove it?

Change above code by following code which remove link if exist into your content.
$content = get_the_content();
$postOutput = preg_replace('/<img[^>]+./','', $post->post_content);
$postContent = preg_replace("/<a[^>]+\>/i", "", $postOutput);
echo $postContent;

Related

Show body content with popup in Wordpress

$large_image = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full');
$output .= '<a class="overlay" rel="shadowbox" href="'.$large_image[0].'">';
It's OK, working fine.
But I want, show post's body in popup. I tried like this but not working.
$content = wp_get_attachment_image_src( body($post->ID));
Whats the problem? How can I fix it?
Thanks.
I don't know, maybe because body isn't a function in WP or php, or because you're trying to use a function that return image source (come on the function name tell you by itself what it does) and hope it'll return (by magic) post content?
Here is how you get the content from a post, within the loop:
$content = get_the_content();
And if you're outside the loop, you can do:
$content = $post->post_content;
you can consider alligator popup
check how it works, you will get some idea.

Wordpress filters and excerpts

I am trying to pull content from one page into another. I could be going about this the wrong way, but I am just trying to get the general method of how it is done. This is what I have so far trying to get the data I need. I am not getting the permalink or the image. The other issue is when applying the substr method to the $content variable it actually cuts the html tags off to making it to where anything after it gets nested into whatever html happens to be in the content. I appreciate any tips you may have. Here is the function I created to try and pull the content in it's initial stage.
function show_post($path) {
$post = get_page_by_path($path);
$title = apply_filters('the_title', $post->post_title);
$image = apply_filters('featured_image', $post->the_post_thumbnail);
$content = apply_filters('the_content', $post->post_content);
$link = apply_filters('the_permalink', $post->get_permalink);
echo '<h3>'.$title.'</h3>';
echo substr($content, 0, 300) . '...';
}
$path is the relative url of the website to where I am pulling content so lets say my url i want to grab data is www.test.com/about/bio I would make the path be about/bio.
I appreciate your help in this.

Wordpress retain formatting when calling extended content?

I am calling in content in Wordpress via the below code. Eseentially, I am dividing the content of the post into three sections; 1. Before the tag, 2. After the tag and 3. Post gallery. The code I have so far works perfectly to get the content, however I am having an issue as all formatting tags (p in particular) are being stripped. Is there a way to retain these?
Thanks
<?php
// Fetch post content
$content = get_post_field( 'post_content', get_the_ID() );
// Get content parts
$content_parts = get_extended( $content );
?>
<p>
<?php echo $content_parts['main']; // Output content before <!--more--> ?>
</p>
<p class="read-more">
<?php echo strip_shortcodes($content_parts['extended']); // Output content after <!--more--> ?>
</p>
<button>Read More</button>
<?php $gallery = get_post_gallery_images( $post ); ?>
When you pull the post content using get_post_field, the autop filter is not applied:
http://codex.wordpress.org/Function_Reference/wpautop
You can apply all of the content filters yourself by adding this line after you set $content:
$content = apply_filters('the_content', $content);

Inserting code after each post in WordPress

I am wanting to insert some code after each post in WordPress... I know you can do it after this for example, in single.php
<?php the_content(); ?>
However if I do that it puts it in the wrong place.. an example post is here: http://www.hardwareblog.com/348/computer-hardware/top-10-gadget-gift-ideas-to-avoid-this-christmas/ -- if I put it AFTER the code example above it will be placed AFTER the sociable & facebook links..... I want to put it BEFORE those, so it's RIGHT AFTER the post.
I did some checking & testing.. this code here from post-template.php
function the_content($more_link_text = null, $stripteaser = 0) {
$content = get_the_content($more_link_text, $stripteaser);
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
echo $content;
}
It seems the facebook & sociable code is inserted into the output within the apply_filters() function.... though I can't work out where.
Any help on what I am trying to do?
Here is an example of a filter on the content and the function:
function the_content_replacer($content)
{
//global $post, $posts;
//$content = str_replace(']]>', ']]>', $content);
$content .= "\n<div style=\"display:none;\">text here</div>";
//$content = preg_replace('/="http:\/\/cnn/i',
// '="http://example.com?http://cnn', $content, -1);
return $content;
}
add_filter('the_content', 'the_content_replacer', 1);
much more examples on this filter on http://wordpress.stackexchange.com ..........
You can just copy and paste the piece of content in the file "functions.php" in your theme and it will work.
You can also just drop it in the directory wp-content/mu-plugins if your run multisite so it works on all the blogs in your multisite environment.
the third parameters determines the importance of when applying the filter, see: https://wordpress.stackexchange.com/questions/2126/at-what-priority-does-add-filter-overwrite-core-functions
--> it is better to post all WordPress questions in http://wordpress.stackexchange.com !!!
--> if you use e.g.
$content .= "\n<div style=\"display:none;\">text here</div>";
it will not remove a closing paragraph tag (note the linebreak at the beginning of the string)

PHP - remove <img> tag from string

Hey, I need to delete all images from a string and I just can't find the right way to do it.
Here is what I tryed, but it doesn't work:
preg_replace("/<img[^>]+\>/i", "(image) ", $content);
echo $content;
Any ideas?
Try dropping the \ in front of the >.
Edit: I just tested your regex and it works fine. This is what I used:
<?
$content = "this is something with an <img src=\"test.png\"/> in it.";
$content = preg_replace("/<img[^>]+\>/i", "(image) ", $content);
echo $content;
?>
The result is:
this is something with an (image) in it.
You need to assign the result back to $content as preg_replace does not modify the original string.
$content = preg_replace("/<img[^>]+\>/i", "(image) ", $content);
I would suggest using the strip_tags method.
Sean it works fine i've just used this code
$content = preg_replace("/<img[^>]+\>/i", " ", $content);
echo $content;
//the result it's only the plain text. It works!!!
I wanted to display the first 300 words of a news story as a preview which unfortunately meant that if a story had an image within the first 300 words then it was displayed in the list of previews which really messed with my layout. I used the above code to hide all of the images from the string taken from my database and it works wonderfully!
$news = $row_latest_news ['content'];
$news = preg_replace("/<img[^>]+\>/i", "", $news);
if (strlen($news) > 300){
echo substr($news, 0, strpos($news,' ',300)).'...';
}
else {
echo $news;
}
$this->load->helper('security');
$h=mysql_real_escape_string(strip_image_tags($comment));
If user inputs
<img src="#">
In the database table just insert character this #
Works for me
simply use the form_validation class of codeigniter:
strip_image_tags($str).
$this->load->library('form_validation');
$this->form_validation->set_rules('nombre_campo', 'label', 'strip_image_tags');

Categories