-
-
Save ilearnbydoing/b7d35e65b263462baff719952b5bddfc to your computer and use it in GitHub Desktop.
Pulling in Yoast SEO meta for terms using Corcel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// This model simply inherits from the base Corcel Taxonomy model. | |
// It allows us to set our DB connection and also implement the | |
// ImportsTermSeo trait (see next file) | |
namespace App\Models\Corcel; | |
use App\Wordpress\ImportsTermSeo; | |
use Taxonomy as Corcel; | |
class Taxonomy extends Corcel | |
{ | |
use ImportsTermSeo; | |
protected $connection = 'wordpress'; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Wordpress; | |
use App\Models\Corcel\Options; | |
trait ImportsTermSeo { | |
// This associative array simply lists the key names that you would like to | |
// use in your template along with the corresponding Yoast SEO key | |
protected $seoKeys = [ | |
'keywords' => 'wpseo_focuskw', | |
'title' => 'wpseo_title', | |
'description' => 'wpseo_desc', | |
'noIndex' => 'wpseo_noindex', | |
'openGraphTitle' => 'wpseo_opengraph-title', | |
'openGraphDescription' => 'wpseo_opengraph-description', | |
'openGraphImage' => 'wpseo_opengraph-image', | |
'twitterTitle' => 'wpseo_twitter-title', | |
'twitterDescription' => 'wpseo_twitter-description', | |
'twitterImage' => 'wpseo_twitter-image', | |
]; | |
// Yoast stores term SEO in a row in the wp_options table. | |
// We therefore need to get this serialized value and convert | |
// all the keys for the taxonomy term we're interested in | |
public function getSeoAttribute() | |
{ | |
// Get the serialized blob of term SEO data from the wp_options table (Corcel handles deserialization) | |
$seoData = Options::get('wpseo_taxonomy_meta'); | |
// Get only the SEO data for the particular term in the specific taxonomy | |
$seo = $seoData[$this->taxonomy][$this->term_id]; | |
// Convert the resulting array of SEO data into a Collection and map this Collection | |
// to produce an array of SEO data with our 'nice' keys | |
return collect($this->seoKeys)->mapWithKeys(function($yoastKey, $niceKey) use ($seo){ | |
return [$niceKey => $this->seoByKey($yoastKey, $seo)]; | |
})->all(); | |
} | |
// Helper function to check if SEO meta exists for a given yoast key | |
// returns a blank string if not | |
private function seoByKey($key, $seo) | |
{ | |
return !empty($seo[$key]) ? $seo[$key] : ''; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers; | |
// Note that this is our own taxonomy model, not the default Corcel one | |
use App\Models\Corcel\Taxonomy; | |
use App\Http\Controllers\Controller; | |
class MyController extends Controller | |
{ | |
public function show() | |
{ | |
$term = Taxonomy::where('taxonomy', 'my_taxonomy')->slug('my-term-slug')->first(); | |
return view('myview', compact('term')); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// How you use the SEO data is kind of up to you, in my case I did something like: | |
@section('seo') | |
@include('global.seo', ['seoData' => $term->seo]) | |
@endsection | |
// then in views/glabal/seo.blade.php I did something like this | |
// (note that some Yoast fields are not in the list below but you can be as | |
// thorough as you want with grabbing all the yoast keys | |
<meta name="description" content="{{ $seoData['description'] }}"/> | |
<meta property="og:locale" content="en_GB" /> | |
<meta property="og:type" content="website" /> | |
<meta property="og:title" content="{{ $seoData['openGraphTitle'] }}" /> | |
<meta property="og:description" content="{{ $seoData['openGraphDescription'] }}" /> | |
<meta name="twitter:card" content="summary" /> | |
<meta name="twitter:description" content="{{ $seoData['twitterDescription'] }}" /> | |
<meta name="twitter:title" content="{{ $seoData['twitterTitle'] }}" /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment