Possibility to have the page thumbnail as the social media og:image?

Permalink Browser Info Environment
Hi Ramon,

I love this add-on and all the possibilities! I just have one more request that would make this add-on superduper. It would be awesome if there was a possibility to have the default page thumbnail to be used as the og:image.

All my pages have a thumbnail assigned. To use those as the og:image now i would have to assign them again on all my individual pages (and there are many). Is there a way to let the add-on use the default page thumbnail instead? Or maybe it's a little trick for you to add it as an option in the add-on?

I guess many sites use the default thumbnail when using the blog-function for example. Or when using event-pages with the a calendar add-on.

Please let me know if possible. If not, for this site i will use another add-on for the og:image and take this license to another site.

Type: Pre-Sale
Status: Resolved
buurvrouw
View Replies:
ramonleenders replied on at Permalink Reply
ramonleenders
Hi there,

Anything is possible! ;)

Go to a package you have for your theme. Go to the controller.php file. Add this on the very top:

use Concrete\Package\DevodaSeo\Src\DevodaSeo;
use Concrete\Package\DevodaSeo\Src\DevodaSeoConfig;


This will include the use statements for some classes we are going to use. Now look if there is an "on_start" function in this file already. It should look like this:

public function on_start()
   {
// some code
}


If there is one already, you can leave this part off from the code I'm going to paste below. If there isn't already a function named like this, you can just paste the whole code below in this file (before the last "}" in this file):

public function on_start()
   {
      Events::addListener('on_header_required_ready', function ($event) {
         // If we have a page that is working
         if (($c = Page::getCurrentPage()) && !$c->isError()) {
            $metaTags = $event->hasArgument('metaTags') ? $event->getArgument('metaTags') : array();
            // Check if we have a valid thumbnail
            if ($thumbnail = $c->getAttribute('thumbnail')) {
               $socials = isset($metaTags['social']) && trim($metaTags['social']) != '' ? $metaTags['social'] : null;
               // If we already have social meta set in our tags
               if ($socials) {
                  // If open graph is enabled
                  if (DevodaSeoConfig::get('social.open_graph')) {
                     $pattern = '/<meta property="og:image" content="(.*)">/i';
                     preg_match($pattern, $socials, $ogImageMatches);


This code will search for the "thumbnail" attribute of a page. If the "Facebook Image" hasn't been set yet, it will do so. If it HAS already been set, it will do nothing (since you probably want to override it, right). So it should cover all situations for this og:image. It also checks if you have open graph meta data enabled within Devoda SEO, so everything should be in it.

Of course you can make changes/additions where needed. A lot is possible by not touching any core stuff. Pretty awesome, right?

Let me know if it worked out for you!

Kind regards,

Ramon
buurvrouw replied on at Permalink Reply
buurvrouw
Wow, that a quick and complete answer! Thanks for that :-)
It just doesn't work yet...

I get this error:
Class 'Concrete\Package\MakThemePackage\Events' not found


So i did some searching in the developer documentation and tried adding this in my package controller:
use Concrete\Core\Support\Facade\Events;


But if i do so, i'm getting a different error:
Class 'Concrete\Package\MakThemePackage\Page' not found


Could you help a bit more to get this working?

This is what i have in my package controller:
<?php
/**
 * Mak Blokweer Theme Package Controller File.
 * @author   Maura van Tuijl <[email protected]>
 * @license  See attached license file
 */
namespace Concrete\Package\MakThemePackage;
use Concrete\Core\Page\Theme\Theme;
use Core;
use Concrete\Core\Foundation\Service\ProviderList;
use Concrete\Core\Package\Package;
use Illuminate\Filesystem\Filesystem;
//use Concrete\Core\Support\Facade\Events;
//use Concrete\Core\Page\Controller\PageController;
//Toegevoegd voor Devoda SEO thumbnail check voor gebruik als socialmedia image:
ramonleenders replied on at Permalink Reply
ramonleenders
This should do the trick:

use Events;
use Page;


Missed them, my bad!

Kind regards,

Ramon
buurvrouw replied on at Permalink Reply
buurvrouw
Ah, thanks! That did the trick!!
ramonleenders replied on at Permalink Reply
ramonleenders
Glad that did it. Have fun with the Add-On!

Kind regards,

Ramon

concrete5 Environment Information

.

Browser User-Agent String

Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.1 Safari/603.1.30

Hide Post Content

This will replace the post content with the message: "Content has been removed by an Administrator"

Hide Content

Request Refund

You may not request a refund that is not currently owned by you.