Trimming titles
-
Hi, how does this plugin retrieve titles from posts?
Can it be set up so if I said had a post like this:Title of post
Content of post
It would set the title to “Title of post”, rather than “Title of postContent of post”?
-
Titles are trimmed by Google+. The trimmed title is provided via their API and the plugin just imports that title. So the title will always be the same as on G+.
I’ll have to figure out how Google+ does titles then, since where I read, it said that Google+ sets the title to be the first line’s content in bold, but that’s evidently not the case.
Hey there,
Ok, so I had this problem too and figured I may as well try to fix it.
I made the following changes in g-crossposting/g-crossposting.php:
First, change this:
// create content $post_content = $activity->object->content;
to this:
// create content $post_content = $activity->object->content; // Modify content to remove title if(($pos = strpos($post_content, '</b>')) !== false) { $content_pos = $pos +4; $stripped_title = strip_tags(substr($post_content, 0, $content_pos-1)); $post_content = substr($post_content, $content_pos); }
Then change this:
// set title for post if ($activity->title) { $post_title = $activity->title; }
To this:
// set title for post if(isset($stripped_title) && strlen($stripped_title)>0) { $post_title = $stripped_title; } elseif ($activity->title) { $post_title = $activity->title; }
Optionally, nearer the bottom, change this:
$post_content .= '<div class="g-crossposting-backlink" ><a href="'.$activity->url.'" target="_blank">'.__('This was first posted on Google+', 'g-crossposting').'</a></div>';
To this:
$post_content .= '<div class="g-crossposting-backlink" style="width: 100%; clear: both;"><a href="'.$activity->url.'" target="_blank">'.__('This was first posted on Google+', 'g-crossposting').'</a></div>';
.. which creates a clean break for social media icons appearing after my post’s images. You could make it cleaner by editing the CSS file, but I didn’t bother.
If you want an example of this in action, here’s my G+ post:
https://plus.google.com/108862967958261109459/posts/RemQ2n4q4P6And my auto cross-posted blog post:
https://stevedowe.me/2014/05/bad-boy-bakewell.htmlHope that helps someone!
Cheers,
Steve@stevedowe The problem with your approach to trimmed titles is that it only works if you follow the rule to always separate the title from the rest of the text with a blank line. If someone just writes two paragraphs of text in his post, the title will become the content of the first paragraph. I believe that if you have a short first “paragraph”, G+ will also use it as the official title. However, the plugin can’t rely on such rules and I can’t include this change in the official version.
I also looked about the floating issue of the link. I changed the generated markup so that the link is always shown at the end. So no floating anymore. Also, all output is now wrapped in <p> tags. Version 1.4.0 should be live in a few minutes.
Then why not add it as a formatting option, so in case the current way of trimming titles doesn’t work, people can use stevedowe’s workaround as a fallback?
Because options and their combination must be tested with each update and that’s something I can’t commit to.
We’re talking about trimming titles, not changing the way everything is formatted.
There’s minimal testing in this — the “fallback” title trimming system will fetch the title like the plugin usually does, and all it does is trim the title just a bit more to fit with steve’s specs.
It’s essentially like this (hope you don’t mind the pseudocode)
function trim_title_original() title = get_title() set_title(title) function trim_title_alternate() title = get_title() title = trim_title(title) set_title(title)
I’d be the first one to admit my code is not perfect and, at best, a hack ??
I totally understand where Sebastian is coming from. I want to contribute to open source a lot, but what I can make time for is very little.
The whole problem here seems to stem from Google’s implementation (or lack of it) regarding a standard scheme for titles. In terms of sq10’s original problem, this solution works. This is all I intended to fix, because that’s how I format G+ posts too.
But we could start looking at some basic rules which cover a few post styles.
For example:
- Where we have a single opening sentence and then a break, use that
- In the case of a paragraph as the first text block, extract the first sentence
- When there is a break and then a sentence/paragraph, just remove the initial breaks and grab the first line, as above
- In all other cases, for example where we can’t define a reasonable title length (if grammar is poor in the source post), then just substring to a certain length and cut it off
Just my $0.02…
I’ll check in the coming days if that really gives good results. As a fallback I would not rely on some length, but just use what Google provides as title.
Also, keep in mind it has to work for different kind of G+ posts like sharing a video, photo, link and for own articles. Those types come back differently from the API.
Basically, I think the alternate title trimming should be like this:
On Google+, the people that want the plugin to use the alternate trimming scheme will post like so:
title of post
content of post…and the plugin will determine the title by fetching the bold sentence. The bold sentence has to be the very first sentence of the post for it to be determined as the title, and if there is no bold sentence/phrase/whatever, then it uses the fallback title trimming technique.
So it’ll be kinda like this (if it were in Javascript, since I know little PHP)
//Assuming the post is an HTML string function get_title(post){ //whatever the original function does to get the post title } function get_title_alternate(post){ var result = (new RegExp(/^<b>([\s\S]+?)<\/b>/gm)).exec(post); //If the regex didn't match anything, use the fallback. if (!result) return get_title(post); //Other than that, return the resulting title (capture group 1). return result[1]; }
IMO it shouldn’t be too hard to implement.
@sq10 What’s your G+ ID so that I can also test with your public stream?
I just created a page for testing.
ID: 106424804349715755721Ok, I just released a new version 1.5.0, which follows the strategy outlined by @sq10:
If a G+ post starts with bold text, this bold text is used as title instead of the title generated by Google+ itself.
Sweet, I’ll be sure to try it out.
Thanks!Works wonderfully. Thanks for the quick fix!
- The topic ‘Trimming titles’ is closed to new replies.