Created
September 26, 2016 13:55
-
-
Save kaynenh/024f350c2eead8bd6fa15837c306456d to your computer and use it in GitHub Desktop.
Wordpress Custom Meta Description based on Faceted Categories
This file contains 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
function add_meta_tags() { | |
global $post; | |
if ( is_archive() || is_home()) { | |
/* | |
* CONTEXT CLASS | |
* Here we go, need to create a context for the library pages and its facets | |
*/ | |
class Context | |
{ | |
public $isContentType = false; | |
public $isTopic = false; | |
public $isIndustry = false; | |
public $isPromoUse = false; | |
public $contentType = false; | |
public $topic = false; | |
public $industry = false; | |
public $promoUse = false; | |
function __construct() { | |
$this->isContentType = (func_get_arg(0) == '-1') ? false : true; | |
$this->isTopic = (func_get_arg(1) == '-1') ? false : true; | |
$this->isIndustry = (func_get_arg(2) == '-1') ? false : true; | |
$this->isPromoUse = (func_get_arg(3) == '-1') ? false : true; | |
$this->contentType = (func_get_arg(0) == '-1') ? false : func_get_arg(0); | |
$this->topic = (func_get_arg(1) == '-1') ? false : func_get_arg(1); | |
$this->industry = (func_get_arg(2) == '-1') ? false : func_get_arg(2); | |
$this->promoUse = (func_get_arg(3) == '-1') ? false : func_get_arg(3); | |
} | |
/* | |
Content Types: | |
4Ideas Friday | |
Blog | |
Blue Paper | |
eBook | |
eNewsletter | |
Infographic | |
Podcast | |
Press Release | |
Staff Favorites | |
Success Stories | |
Video | |
*/ | |
function setContentType($name) { | |
$this->contentType = $name; | |
$this->isContentType = true; | |
} | |
function setTopic($name) { | |
$this->topic = $name; | |
$this->isTopic = true; | |
} | |
function setIndustry($name) { | |
$this->industry = $name; | |
$this->isIndustry = true; | |
} | |
function setPromoUse($name) { | |
$this->promoUse = $name; | |
$this->isPromoUse = true; | |
} | |
function getPluralContentType($ct) { | |
if ($ct == '4Ideas Friday') { | |
return '4Ideas Friday content'; | |
} | |
$plurals = array("Blue Paper", "eBook", "eNewsletter", "Infographic", "Podcast", "Press Release", "Video"); | |
if (in_array($ct, $plurals)) { | |
//var_dump($ct); | |
return $ct . 's'; | |
} | |
return $ct; | |
} | |
function printMetaDescription() { | |
$metaContent = "Discover ways to use your promotional products in our Learning Center."; | |
if ($this->isContentType && $this->isTopic && $this->isIndustry && $this->isPromoUse) { //Correct | |
$metaContent = sprintf("Want to use promotional products for %s? We have examples for %s %s in our %s.", $this->promoUse, $this->industry, $this->topic, $this->getPluralContentType($this->contentType)); | |
} elseif ($this->isContentType && $this->isTopic && $this->isIndustry) { //Correct | |
$metaContent = sprintf("Stay up-to-date on marketing, branding, human resources and more with %s %s in info.4imprint.com’s %s.", $this->industry, $this->topic, $this->getPluralContentType($this->contentType)); | |
} elseif ($this->isContentType && $this->isIndustry && $this->isPromoUse) { //Correct | |
$industry_fix = ($this->industry == 'General' ? 'General industry' : $this->industry); | |
$industry_fix = ($this->industry == 'Nonprofit' ? 'Nonprofits' : $industry_fix); | |
$nonprofit_verb_fix = ($this->industry == 'Nonprofit' ? 'use' : 'uses'); | |
$metaContent = sprintf("Check out our %s to learn how %s %s promotional products for %s.", $this->getPluralContentType($this->contentType), $industry_fix, $nonprofit_verb_fix, $this->promoUse); | |
} elseif ($this->isTopic && $this->isIndustry && $this->isPromoUse) { //Correct | |
$metaContent = sprintf("From %s %s to %s or promotional products, visit our learning center.", $this->industry, $this->topic, $this->promoUse); | |
} elseif ($this->isContentType && $this->isTopic) { //Correct | |
$metaContent = sprintf("When you want to know more about %s, check out the %s at info.4imprint.com.", $this->topic, $this->getPluralContentType($this->contentType)); | |
} elseif ($this->isContentType && $this->isIndustry) { //Correct | |
$metaContent = sprintf("Find answers to your marketing, branding, team-building, business gifts questions and more in our %s %s.", $this->industry, $this->getPluralContentType($this->contentType)); | |
} elseif ($this->isContentType && $this->isPromoUse) { //Correct | |
$metaContent = sprintf("For ideas on using promotional products for %s, check out the %s in the 4imprint Learning Center.", $this->promoUse, $this->getPluralContentType($this->contentType)); | |
} elseif ($this->isTopic && $this->isIndustry) { //Correct | |
$industry_fix = ($this->industry == 'General' ? '' : ' '.$this->industry); | |
$metaContent = sprintf("Promotional products are just the beginning! Enhance your %s with tips from our%s Learning Center.", $this->topic, $industry_fix); | |
} elseif ($this->isTopic && $this->isPromoUse) { //Correct | |
$metaContent = sprintf("Learn how to use promotional products for %s or %s in our Learning Center.", $this->promoUse, $this->topic); | |
} elseif ($this->isIndustry && $this->isPromoUse) { //Correct | |
$metaContent = sprintf("If you’re looking for tips on using promotional products for %s in %s, check out our Learning Center.", $this->promoUse, $this->industry); | |
} elseif ($this->isContentType) { //Correct | |
$metaContent = sprintf("Generate ideas and expand your knowledge on marketing, branding, team-building, business gifts and more with info.4imprint.com %s content.", $this->contentType); | |
} elseif ($this->isTopic) { //Correct | |
$metaContent = sprintf("Find helpful tips on %s – and a wide range of other marketing, HR and promotional products topics - in our Learning Center.", $this->topic); | |
} elseif ($this->isIndustry) { //Correct | |
$metaContent = sprintf("Explore the latest marketing, HR and digital tools – including promotional products – in our %s content.", $this->industry); | |
} elseif ($this->isPromoUse) { //Correct | |
$metaContent = sprintf("Discover a variety of ways to use promotional products for %s in our Learning Center.", $this->promoUse); | |
} | |
echo "\n" . "<meta name='description' content='{$metaContent}' />" . "\n"; | |
} | |
} | |
/* | |
* END CONTEXT CLASS | |
*/ | |
/* | |
* INITIAL CONTEXT | |
* Get arguments from url and set the context | |
*/ | |
if (isset($_GET['content_type']) && ($_GET['content_type'] != '-1')) { | |
$category = get_term_by('id', $_GET['content_type'], 'category'); | |
$category_name = $category->name; | |
$content_type_get = $category_name; | |
} else { | |
$content_type_get = '-1'; | |
} | |
if (isset($_GET['topic']) && ($_GET['topic'] != '-1')) { | |
$category = get_term_by('id', $_GET['topic'], 'category'); | |
$category_name = $category->name; | |
$topic_get = $category_name; | |
} else { | |
$topic_get = '-1'; | |
} | |
if (isset($_GET['industry']) && ($_GET['industry'] != '-1')) { | |
$category = get_term_by('id', $_GET['industry'], 'category'); | |
$category_name = $category->name; | |
$industry_get = $category_name; | |
} else { | |
$industry_get = '-1'; | |
} | |
if (isset($_GET['promo_use']) && ($_GET['promo_use'] != '-1')) { | |
$category = get_term_by('id', $_GET['promo_use'], 'category'); | |
$category_name = $category->name; | |
$promo_use_get = $category_name; | |
} else { | |
$promo_use_get = '-1'; | |
} | |
$current_context = new Context($content_type_get, $topic_get, $industry_get, $promo_use_get); | |
$cat_id = get_query_var('cat'); | |
/* | |
* END INITIAL CONTEXT | |
*/ | |
/* | |
* SINGLE CATEGORY CONTEXT | |
* Arguments are not always set in url, but a single category can still be set | |
* Let's check for those | |
*/ | |
if ('' != $cat_id) { | |
$category = get_term_by('id', $cat_id, 'category'); | |
$content_type = get_term_by('name', 'Content Type', 'category'); | |
$topic = get_term_by('name', 'Topic', 'category'); | |
$industry = get_term_by('name', 'Industry', 'category'); | |
$promo_use = get_term_by('name', 'Promotional Products Use', 'category'); | |
if ($category->parent == $content_type->term_id) { | |
$current_context->setContentType($category->name); | |
} elseif ($category->parent == $topic->term_id) { | |
$current_context->setTopic($category->name); | |
} elseif ($category->parent == $industry->term_id) { | |
$current_context->setIndustry($category->name); | |
} elseif ($category->parent == $promo_use->term_id) { | |
$current_context->setPromoUse($category->name); | |
} elseif (cat_is_ancestor_of(get_cat_id('Topic'), $cat_id)) { | |
$current_context->setTopic($category->name); | |
} | |
} | |
/* | |
* END SINGLE CATEGORY CONTEXT | |
*/ | |
//Let's print this stuff | |
$current_context->printMetaDescription(); | |
} | |
} | |
add_action( 'wp_head', 'add_meta_tags' , 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment