GENWiki

Premier IT Outsourcing and Support Services within the UK

User Tools

Site Tools


joomla:create_or_edit_an_article_in_php

To create an article programatically in Joomla…

if (version_compare(JVERSION, '3.0', 'lt')) {
    JTable::addIncludePath(JPATH_PLATFORM . 'joomla/database/table');        
}   
$article = JTable::getInstance('content');
$article->title            = 'This is my super cool title!';
$article->alias            = JFilterOutput::stringURLSafe('This is my super cool title!');
$article->introtext        = '<p>This is my super cool article!</p>';
$article->catid            = 9;
$article->created          = JFactory::getDate()->toSQL();
$article->created_by_alias = 'Super User';
$article->state            = 1;
$article->access           = 1;
$article->metadata         = '{"page_title":"","author":"","robots":""}';
$article->language         = '*';
 
// Check to make sure our data is valid, raise notice if it's not.
 
if (!$article->check()) {
    JError::raiseNotice(500, $article->getError());
}
 
// Now store the article, raise notice if it doesn't get stored.
 
if (!$article->store(TRUE)) {
    JError::raiseNotice(500, $article->getError());
}

To Create OR Update based on an alias…

$ipf=JFilterOutput::stringURLSafe($ip); // this is the alias
 
$article = JTable::getInstance('content');
$article->load(array('alias'=>$ipf));
$id=$article->id;
 
if ($id==null) {
$article->title = 'Blacklist for IP '.$ip;
$article->alias = $ipf;
$article->catid = 13;
$article->introtext = $html; 
$article->created = JFactory::getDate()->toSQL();
$article->created_by_alias = 'Super User';
$article->state = 1;
$article->access = 1;
$article->metadata = '{"page_title":"","author":"","robots":""}';
$article->language = '*';
if (!$article->check()) {
echo "CREATE CHECK FAILED ".$article->getError(); 
} 
if (!$article->store(TRUE)) {
echo "CREATE STORE FAILED ".$article->getError(); 
} 
} else {
$article->introtext = $html;
if (!$article->check()) {
echo "UPDATE CHECK FAILED ".$article->getError(); 
}
if (!$article->store()) {
echo "UPDATE STORE FAILED ".$article->getError(); 
} 
}

Richard 2019/03/21 11:16

/data/webs/external/dokuwiki/data/pages/joomla/create_or_edit_an_article_in_php.txt · Last modified: 2019/05/20 09:02 by genadmin

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki