Creating MODX Articles in Code I

Use a PHP script to create new blog Articles

In the previous article, we looked at how to include other extras in a transport package. In this one, we'll see how to create Articles in code.

MODX logo

The Problem

MODX Forum user lancipoos asked for a way to create Articles resources in code. One reason for doing this is to create the skeleton of an Article to give Manager users a head start on the pages they will create. Another use case would be the automated creation of blog pages for an events calendar or something similar.

The Solution

The process of creating Articles resources is fairly simple. There are only two differences between a regular resource and an Article in an Articles blog. The Article must have the class_key field set to Article, and the parent must be the ID of an Articles Container resource.

The Code

This is the simplest way to create a resource, though it has some drawbacks that we'll discuss (and see how to avoid) in a later article.

/* Article Creation Code */

/* Set this to the ID of the Articles Container resource  */
$parent = 12;

/* Set this to the ID of an Article template
    (*not* the Article container template) */
$template = 122; set to the ID of the Articles Template.

/* Set this to the ID of the page creator */
$creator = 1;

$pagetitle = 'Some Pagetitle';
$alias = 'some-pagetitle';

$doc = $modx->newObject('modResource');
$doc->set('class_key', 'Article');
$doc->set('parent', $parent);
$doc->set('pagetitle', $pagetitle);
$doc->set('template', $template);
$doc->set('alias', $alias);
$doc->set('createdon', time());
$doc->set('published', $published);

/* Set publishedon field if published */
if ($published) {
    $doc->set('publishedon', time());


You can add code above to set any of the other resource fields (except the id field, which should never be set manually). The possible fields are listed on this page, though the content should be set separately.

When using this method, it's critical to set the alias, parent, and template. Otherwise, they will be 0 or blank, even if the automatic_alias System Setting is on and the default_template setting is set. The publish_default System Setting will also be ignored.

Setting the Content

Rather than creating a completely empty document, you might want to include some content. To do so, add this code near the top:

$content = '
<h3>My Page</h3>
        <p>Some Content</p>
Then, add this code just above the $doc->save() line:

Notice that we didn't use $doc->set('content', $content);. That would work as well, but the recommended method for setting the content of MODX objects like resources, chunks, templates, snippets, and plugins is to use setContent() because you don't have to remember the name of the field that holds the content of the object, which varies from object to object.

If you have a slightly more complicated bit of HTML for the content section, it's often more convenient to use PHP's heredoc format for the text, especially if the HTML contains quotation marks. Here's an example.

$content = <<<EOT
<div class="something">
    <ul class="mylist">


In the code above, think of the EOT token as replacing the quotation mark at the beginning of a quoted string and the EOT; token at the end as replacing the closing quotation mark. The part of the opening token following the <<< (in this case EOT can be any text string (though EOT is the most commonly used), but it must match the one at the end. Important: The closing token must be on its own line and cannot be indented.

The advantage of the heredoc format is that any quotation marks between the tokens are rendered as is with no need to escape or separate them.

Rather than hard-coding it into your script, you might want to pull the original content from a chunk. We'll see how to do that in the next article.

Coming Up

In the next article, we'll look at how to set the content of your Article resource using a chunk.

For more information on how to use MODX to create a web site, see my web site Bob's Guides, or better yet, buy my book: MODX: The Official Guide.

Looking for high-quality, MODX-friendly hosting? As of May 2016, Bob's Guides is hosted at A2 hosting. (More information in the box below.)

Comments (0)

Please login to comment.