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.
Related
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.
Joomla! generates the meta tags before the title tag, For SEO Purposes, I would like to change it the other way, first title, and then meta tags, CSS, JS and other stuff.
I know I can do it by changing the order in the head.php file, but hacking the core files is very bad because when I'll want to update the system version, what I did will be removed.
Is there any other way to do that? maybe from the template or even by an extension?
One way would be to write a plugin to re-order this section via a regex search-and-replace before the page is displayed on the screen.
Looking at head.php, the simplest solution I can think of is that I see that it sniffs to see if a meta tag exists before outputting it to the page (sadly, it does not do this with the title tag).
In your template, you could always save the meta data values in parameters, then unset it each one with code along the lines of
JFactory::getDocument()->setGenerator('');
You could display the head as normal below this (which should now have the title at the top due to having no meta data to display)...
<jdoc:include type="head" />
... before hardcoding your metadata using the saved parameters beneath this.
The above all said, I would really question your SEO. I cannot think of a single reason why Google should penalise a site for having a title tag below the metadata. If it was a well known SEO no-no, then Joomla would likely have updated it - it would be a very small bug to fix. It sounds like SEO hearsay rather than anything real, and so fixing it would be bulking up your codebase for no good reason.
I need to insert a google adsense square box in the middle of all articles on the site.
What is the best approach?
The site is in drupal, so I figures I can hook to the view node function. this way the ad won't be saved together with the article. does that make sense? If so, than the question is how do I ensure I won't have image on the other side of the ad (the ad is aligned to the left and should take 50% of the article width)?
should I be using a block? but how will I know to place the block in inside the block.
So in short, I really have no idea where to start to implement it... How is this thing implemented in all the news sites?
I'd approach it by implementing a custom input filter that would insert the AdSense code (or the AdSense module shortcode, if you want) into the processed text. Once you have that custom filter implemented, you'll have to enable it in the text format you're using for the content type.
For implementing a custom filter, take a look at filter_example here.
This solution is for Drupal 7. The solution which worked for me is that I used the adsense tags format and then used this format to show ads on the site. Note that this line doesnot need any js inclusion or script tags. You just need to enable the Adsense tags format(I mean shortcode). Actually I was getting Ads disabled for admin and for other users i was getting the empty ad with space occupied on page but nothing shown. After different tweaks and experiments I got the following code working for me
[adsense:336x280:XXXXXXXXXX]
2nd parameter is width x height
3rd parameter: XXXXXXXXXX denotes the google ad slot/id.
I hope this will help others.
Ata ul Mustafa
I need to insert a google adsense square box in the middle of all articles on the site. What is the best approach?
From my experience:
On some drupal sites I used simply node editor to put box with adsense into content. For websites with not many nodes its best solution because of:
1) Box perfect fits in rest of content - you can change it manually.
2) Total control of viewed boxes - images, flash, text adds etc.
Etc.
But for websites with many nodes, this solution looks like stupid suicide.
Better option will be change node.tpl file in your theme folder. But there is some trouble. In this file, you have $content variable, with contains all of content in this node. This situation gives you ability to put box up or down of this node. But no in the middle. Perhaps solution will be to divide $content into 2x variables which will contains a half of contents. But question is - how do to it? Or maybe while content from node is saved into database, then you can put some html code with adsense box? And search best position to put box by regular expressions? Or maybe change all of nodes by adding html box directly in database?
should I be using a block? but how will I know to place the block in
inside the block.
As I remember, you cannot put block inside another block.
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.
I'm trying to develop web application (php/mysql), sort of blog site where ~10 posts are listed on front page. Only first 4-5 lines of each post are displayed and when user clicks the title it opens new page with full post displayed.
Users will use TinyMCE to post with very limited functionality, so bold, italics, underline, undo/redo and bullet lists only. No HTML view, no links nor images so nothing fancy.
Now, as two displays of the same posts are involved, so the one on front page where part is displayed only, and full version, I'm not sure if I should:
when user submits post I should clear it up with something like HTMLPurifier and store one full version in database. Then cut first 4-5 lines of it, and clear it with HTMLPurifier again to make sure all tags are closed properly. This is to make sure the text I cut doesn't finish with html tag cut in half but rather is valid (x)html properly closed. I would store this shorten front-page version somewhere in database again. So I would have two versions in database ready, full post version and shorten front-page version. Then when someone accesses the site and displays front page with 10 posts, these just need to be read from database and displayed. No need for reading full version, cutting the top of it, making sure all tags are properly closed etc. etc. for all 10 posts every time someone opens the site. The obvious problem would seem that part of content is doubled in database so full version and short version.
Another option I thought about would be when user submits post, clear it up with HTMLPurifier, and store only full version to database. Then when someone accesses the site, cut top part of each blog post, clear it up with HTML purifier (again!) to make sure its all valid, tags are closed etc. and display on front page. Now the problem seems that HTMLPurifier would have to be run and clean every single time when someone accesses the short version of the post, so e.g. 10x for frontage for each visit and so on.
I used HTMLPurifier above as an example only as I'm sure there are other tools douing same thing so please advice if anything else would be more appropriate in my situation. Also I will probably allow users to display more than 10 posts per page.
Ok, I'm sure there is some common pattern to deal with this sort of site and if so please give me an idea of how it should be done as my thought above seems to have many obvious problems. As always your help is much appreciated.
EDIT: I should have clarified.. Its more like an exercise to me and I wanted to learn how this sort of site should be done. Same concept can be implemented on other sites so advertising, forums etc. anything with posting really. For blogging I sure could use something ready but this time I wanted to learn.
When a user posts a blog entry, all you need to do is insert the entire post into the database. Before inserting, you need to escape/clean-up the post so that nothing harmful is inserted.
When viewing the post you will have two type of views - the excerpt and the full. The excerpt will only be the first 4-5 lines of the post. To get this you need to TRUNCATE the post before outputting it. To do that, you need to create a truncate function that will limit the number of words/characters.
You do not need to insert the post twice ( excert and full ) into the database.
For instance:
// Excerpt Post
echo $postTitle;
echo truncate($postBody); // where truncate is the function you used to trim
// Full Post
echo $postTitle;
echo $postBody;
Hope this gets you started!