How to Create an Article in Joomla with PHP

Below, you can find a PHP snippet that allows you to create a new article by providing a title, alias, intro, full text, and the category and its state.

$app = \Joomla\CMS\Factory::getApplication();
$mvcFactory = $app->bootComponent('com_content')->getMVCFactory();
$articleModel = $mvcFactory->createModel('Article', 'Administrator', ['ignore_request' => true]);

$article = [
    'catid' => 2,
    'alias' => \Joomla\CMS\Filter\OutputFilter::stringURLSafe('A41234My Article Title'),
    'title' => '123My Article Title',
    'introtext' => 'My Article Intro Text',
    'fulltext' => 'My Article Full Text',
    'state' => 1,
	'language' => '*',
];

if (!$articleModel->save($article))
{
    throw new Exception($articleModel->getError());
}
Last updated on Apr 23rd 2025 14:04