$values['alias'] = '';
$values['alias'] = $modx->db->escape($fields['title']);
/**
* Format alias to be URL-safe. Strip invalid characters.
*
* @param string Alias to be formatted
* @return string Safe alias
*/
function stripAlias($alias) {
// let add-ons overwrite the default behavior
$results = $this->invokeEvent('OnStripAlias', array ('alias'=>$alias));
if (!empty($results)) {
// if multiple plugins are registered, only the last one is used
return end($results);
} else {
// default behavior: strip invalid characters and replace spaces with dashes.
$alias = strip_tags($alias); // strip HTML
$alias = preg_replace('/[^\.A-Za-z0-9 _-]/', '', $alias); // strip non-alphanumeric characters
$alias = preg_replace('/\s+/', '-', $alias); // convert white-space to dash
$alias = preg_replace('/-+/', '-', $alias); // convert multiple dashes to one
$alias = trim($alias, '-'); // trim excess
return $alias;
}
}
$alias = strtolower($modx->stripAlias(trim($pagetitle)));
<?php
function CreateBlogPost( &$fields ){
global $modx;
$values = array();
$values['pagetitle'] = $modx->db->escape($fields['title']);
$values['longtitle'] = '';
$values['description'] = '';
$values['alias'] = strtolower($modx->stripAlias(trim($fields['title'])));
$values['published'] = 1; //0 - if you want the document to be created unpublished
$values['parent'] = 47; //id of the container for the the new document
$values['introtext'] = '';
$values['content'] = $modx->db->escape($fields['content']);
$values['template'] = 7; //the template of your new document
$values['searchable'] = 1;
$values['cacheable'] = 1;
$values['createdby'] = 1; //this is not the web user ID. Change it to an existing manager id
$values['createdon '] = time();
$values['editedby'] = 1; //this is not the web user ID. Change it to an existing manager id
$values['editedon '] = time();
//you can find more document properties by browsing the modx_site_content table
//insert the new document. In case you need the id of the new document use the $newId variable
$newId = $modx->db->insert( $values, $modx->getFullTableName('site_content') );
//page specific template variables
$tvValues = array();
$tvValues['tmplvarid'] = 2;//ID of the TV
$tvValues['contentid'] = $newId; //The ID of the DOCUMENT
$tvValues['value'] = $modx->db->escape($fields['tags']);
$modx->db->insert( $tvValues, $modx->getFullTableName('site_tmplvar_contentvalues') );
//clear the cache. in case you use Ditto to display the new document
clearCache();
return true;
}
function clearCache() {
include_once "manager/processors/cache_sync.class.processor.php";
$sync = new synccache();
$sync->setCachepath("assets/cache/");
$sync->setReport(false);
$sync->emptyCache();
}
?>