Stripping images from post teasers/excerpts in Wordpress - php

In my theme I'm trying to display the excerpts or teasers of child pages, yet I do not want to display any images, I want image tags stripped out.
Once I've gotten the teaser in all its html glory in a php variable, how do I strip out the img tags prior to using echo?

And after you got the content in a html variable a small regular expression should do the rest
$content = preg_replace("/<img(.*?)>/si", "", $content);
Should do the trick

You probably want to use the semantics of in your pages, similar to how post excerpts work. You can get the list of subpages http://codex.wordpress.org/Template_Tags/wp_list_pages from here and then iterate over them (the above document has an example for styling the names of pages). you can then get the page contents with a custom function and do whatever you want with it.
http://www.mattvarone.com/wordpress/functionsphp-wordpress-themes/
That url explains how to add your own custom functions to a given theme.

You could use the the_excerpt() which automatically grabs just the text or you can use a more sophisticated version of the same thing called The Excerpt Reloaded

Related

Hide Gutenberg's Pullquote Block from RSS Feed in Wordpress

Brief explanation of our need: When we use a pullquote in a post we want it filtered out/hidden from the RSS feed, while at the same time continuing to show up in the content when viewing the post on the website.
Fuller explanation of our need: A pullquote highlights a specific piece of content from an article/page/post by repeating the content and bringing it slightly outside of the flow of the article/page/post content. This works well when using CSS, because it makes it clear that it is not to be read in the flow of the article's content, but to highlight a good sentence or two. Pullquotes are repeated content. However, in an RSS feed, the repeated content is awkward. We'd like to automatically filter out all pullquotes from our blog content on RSS feeds, while keeping it in the post content for frontend viewing.
What have I tried?
I've tried this plugin and it didn't work: https://wordpress.org/plugins/content-visibility-rss-feed/
I've read this, but don't know how to make it work for filtering pullquotes: Remove certain elements from an RSS feed such as short codes
I've read this, and while he outlines the problem well, we need a PHP solution and not a JS solution: https://css-tricks.com/better-pull-quotes/
I've read this stack overflow post, but we don't want to use custom fields: Hide/block page elements from RSS feed
A friend provided this code that I've tested, and it works well.
// Remove all Gutenberg pull quote blocks from feeds
function theme_filter_pull_quotes( $content ) {
return preg_replace('#<figure class="wp-block-pullquote(.*?)</figure>#', '', $content );
}
add_filter('the_content_feed', 'theme_filter_pull_quotes');
As you can see, the opening tag is not closed because depending upon your block settings, Wordpress may add other classes which would not match the pattern. Leaving the tag opened allows any additional classes to be included. It seems wp-block-quote is always the first class.

wordpress deletes <sup> on save comment

I'm trying to implement an WYSIWYG editor to my WP comment section. I experience many difficulties with saving the HTML data in the WordPress database. WP strips many of the HTML tags automatically. I was able to restore the tags trough the 'preprocess_comment' filter. I added the wpautop() filter to the content. Now the line breaks are appearing. But when I want to use the tag, WordPress deletes is too.
I use the Trumbowysiwyg js solution to replace the default comment textarea with the wysiwyg editor. When using the editor, the HTML code looks nice. It has all the HTML tags but after saving the comment, these tags are stripped. Can someone tell me how to allow HTML comments to be saved in the database?
WordPress keeps a list of allowed tags which you can use in comments, surely you do not want users to post any html they want. So I suggest you edit the allowedtags so they will not be removed.
add_action('comment_post', 'allow_more_tags');
function allow_more_tags() {
global $allowedtags;
$allwedtags['sup'] = array('class'=>array());
}
Like this you can add any tags you like and even allow certain classs to be added to them.

Modify article on the fly in Joomla

I can change a title by placing the next line into my template:
$doc->setTitle('my new title');
But is any method to modify an article's 'fulltext' in the same way?
Short answer: yes, with a content plugin.
Details
If you can use $doc->setTitle(), I assume that somewhere before that line there's a line which says:
$doc = JFactory::getDocument();
But the document refers to the whole page, so you're actually setting the html <title> tag.
If you want to change an article text, you're dealing with the result of a specific component, i.e. com_content, which is only a part of the whole document.
There are more than one ways to change it.
Change the whole page content
If you want to change the whole page content in your template, you have to do:
$buffer = $doc->getBuffer();
// ... do whatever modification to buffer
$doc->setBuffer($buffer)
Quite simple to do, but it's not a good idea to put this kind of logic in the template (if you ever want to change your template, you'll have to copy / paste the code).
Use a system plugin to change the buffer
The core of this solution is the same as before, but you put the logic in a system plugin, hooking on the onAfterRender event. You get the buffer, you modify it, you set the buffer.
Good solution for very simple modifications and/or if you want to make sure the modifications applies to any part of the page.
Use a content plugin
A content plugin applies only to articles body, product descriptions, and similar text parts.
You should use this if you want only to modify those parts.

How to insert Google Adsense in a single post after the <!--More--> Tag

I'm using wordpress, and I want to add the adsense javascript tags where the Read More tag goes in a blogpost. It's stored as <!--more--> in the database, but when you use the_content() this is automatically changed before I have the chance to use a str_replace() function.
I'm looking to place an adsense tag for each post in the single.php page where the teaser ends. Has anyone been able to do this?
Create a filter on the content, to trigger text manipulation between retrieval and rendering.
Define a shortcode to create something like [adsense] you can put in a post. When rendered WordPress calls your function, to replace the code by some text.
If every post must be changed use the filter. Use the shortcode if you want to be able to trigger replacement per post.

Wordpress auto excerpts from post content?

I'm trying to create an auto generated post excerpt from the current page's post content using a function in my theme's header file. The post excerpt will be used as the page's meta description. Can someone give me an idea of how you might go about this once you've got the post content into a string variable?
The somewhat tricky part is that, in order to predict a viable stopping point for the post excerpt, I'd like to specify that the cutoff point be the end of the first paragraph of text.
And for that reason, it does not make sense to load the entire post content into the string I'm using. Can I grab the first paragraph without having to load the entire post content string?
And I'm not certain how to test for that in php. Would regex be the only way?
You can't parse HTML with regex. Posts are stored formatted in HTML (i.e. <p></p> <br /> etc).
I'm also assuming you are implementing this on a blog with lots of existing posts.
What you can do is:
Retrieve the post and run it through an XML Parser. Grab the first paragraph. This is incredibly expensive for such a simple task.
Use a quick tag in the post to denote the excerpt stop point, strip HTML from everything to the left of it. Similar to the <-- more --> tag.
Store an excerpt with each post, I believe WP already has facilities for that.
It would be much, much easier if you could simply select the excerpt without having to do any additional fiddling in order to use it, so the time to handle it is when a post is saved.
So, if you can initially select each post, parse it, get the first paragraph and insert it into another table, then have your plugin do that when each new post is saved, you're home. Naturally, you'd update the same if a post was edited (making that optional).
Just please, please, please don't introduce a plugin in WP that uses regex to parse a context free language. Its just asking for trouble.

Categories