Last active
September 6, 2022 07:36
-
-
Save frisospeulman/7751607 to your computer and use it in GitHub Desktop.
Converts keyword tv to seoPro Keywords objectCreate a snippet and run it on a blank page.
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
<?php | |
/** | |
* Convert your keyword tv to Sterc's SeoPro | |
* | |
* Please read the following. | |
* Everytime you run this snippet, your keywords within seoPro will be overwritten. | |
* Make sure the TV exists before runnig this code. | |
* | |
*/ | |
$seoPro = $modx->getService('seopro', 'seoPro', $modx->getOption('seopro.core_path', null, $modx->getOption('core_path') . 'components/seopro/') . 'model/seopro/', $scriptProperties); | |
if (!($seoPro instanceof seoPro)) { | |
return '<strong>Oooops!</strong>. SeoPro does not exist in this MODx installation'; | |
} | |
/** SET YOUR TV NAME HERE * */ | |
$keyWordTV = ''; | |
/** Set the seperator you used in you tv* */ | |
$keyWordDelimiter = ','; | |
$tvCount = $modx->getCount('modTemplateVar', array('name' => $keyWordTV)); | |
if (!$tvCount) { | |
return '<strong>Oooops!</strong>. The given tv:' . $keyWordTV . ' does not exist in this MODx installation'; | |
} | |
$resources = $modx->getCollection('modResource'); | |
foreach ($resources as $resource) { | |
$seoKeywords = $modx->getObject('seoKeywords', array('resource' => $resource->get('id'))); | |
if (!$seoKeywords) { | |
$seoKeywords = $modx->newObject('seoKeywords', array('resource' => $resource->get('id'))); | |
} | |
$keywords = $resource->getTVValue($keyWordTV); | |
if (!empty($keywords)) { | |
$arrKeywords = explode($keyWordDelimiter, $keywords); | |
$seoKeywords->set('keywords', implode(',', array_map('trim', $arrKeywords))); | |
} | |
if ($seoKeywords->save()) { | |
echo $resource->get('pagetitle') . ' keywords changed to seoPro Keywords ' . PHP_EOL; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks for the feedback. I edited the gist.