Skip to content

Instantly share code, notes, and snippets.

@frisospeulman
Last active September 6, 2022 07:36
Show Gist options
  • Save frisospeulman/7751607 to your computer and use it in GitHub Desktop.
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.
<?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;
}
}
@Jako
Copy link

Jako commented May 18, 2017

The Keyword values in line 33 could be trimmed to remove accidental whitespaces with:

    $seoKeywords->set('keywords', implode(',', array_map('trim', $arrKeywords)));

@frisospeulman
Copy link
Author

thanks for the feedback. I edited the gist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment