Quantcast
Channel: Joomla! Forum - community, help and support
Viewing all articles
Browse latest Browse all 2209

Joomla! 4.x Coding • Re: Programatically add an article?

$
0
0
Step 1: Set Up Joomla Environment
Ensure you have access to your Joomla installation and have set up your development environment with access to the Joomla libraries. by Clothing Manufacturer

Step 2: Create a Script to Add an Article
Create a PHP script within your Joomla installation directory, for example, add_article.php.
Include Joomla framework and initialize it in your script.

Code:

<?php// Define the application's root pathdefine('_JEXEC', 1);define('JPATH_BASE', dirname(__FILE__));define('DS', DIRECTORY_SEPARATOR);// Include necessary Joomla framework filesrequire_once (JPATH_BASE . DS . 'includes' . DS . 'defines.php');require_once (JPATH_BASE . DS . 'includes' . DS . 'framework.php');// Initialize the Joomla application$app = JFactory::getApplication('site');// Include the article modelrequire_once (JPATH_BASE . DS . 'components' . DS . 'com_content' . DS . 'models' . DS . 'article.php');// Get the data for the new article$data = array(    'title' => 'Sample Article Title',    'alias' => 'sample-article-title',    'introtext' => '<p>This is the introduction of the sample article.</p>',    'fulltext' => '<p>This is the full text of the sample article.</p>',    'catid' => 2, // Replace with your category ID    'state' => 1, // 1 for published, 0 for unpublished    'access' => 1, // 1 for public    'language' => '*', // * for all languages    'created_by' => 42, // Replace with the user ID of the author);// Create a new instance of the Article model$model = JModelLegacy::getInstance('Article', 'ContentModel');// Save the articleif ($model->save($data)) {    echo "Article saved successfully!";} else {    echo "Failed to save article.";}?>

Statistics: Posted by vendorist — Tue Jul 09, 2024 7:02 am



Viewing all articles
Browse latest Browse all 2209

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>