Creating a Weblink in Code

Creating, setting, and saving a new modWebLink object in PHP code.


You might someday want to create a Weblink in code, using either a snippet or a plugin. In this article, we'll look at how to make a new Weblink in PHP code.


MODX logo

Creating Weblinks

Creating a new Weblink in code is fairly simple. We create the new object, set a few of its fields, and save it. For the content field, we'll use the variable $content because it goes in the content field. It might be a Resource ID, a URL, or a link tag. In the code, we'll give examples of all three.

Remember that a resource ID will not work on MODX versions before 2.2.5. Also, don't forget to capitalize the "L" in modWebLink.


The Code

/* Weblink creation code */

/* Set this to the ID of the resource you want to link to */
$content = 12;
// $content = 'https://mysite.com/somePage.html';
// $content = '[[~12]]';

$webLink = $modx->newObject('modWebLink');
$webLink->set('content', $content);
$webLink->set('class_key', 'modWebLink');
$webLink->set('pagetitle', 'My Page');
$webLink->set('menutitle', 'My Page');
$webLink->set('alias', 'my-page');

$webLink->save();

Because we're setting a number of fields for the Weblink, you might be tempted to use this code for the creation of the Weblink:

$fields = array(
    'content' => $content,
    'class_key' => 'modWebLink',
    'pagetitle' => 'My Page',
    'menutitle' => 'My Page',
    'alias' => 'my-page',
);
$webLink = $modx->newObject('modWebLink', $fields);

$webLink->save();

For some MODX objects, this will work, but it often fails. For Weblinks, not all of the fields will be set if you do it this way. Fortunately there's another way, and you should use it any time you're setting multiple fields on a new MODX object.

$fields = array(
    'content' => $content,
    'class_key' => 'modWebLink',
    'pagetitle' => 'My Page',
    'menutitle' => 'My Page',
    'alias' => 'my-page',
);
$webLink = $modx->newObject('modWebLink');
$webLink->fromArray($fields);

$webLink->save();

Using fromArray() to set the new object's fields is much more reliable, and it will work with any MODX object because fromArray() is a method of xPDOObject. All MODX objects are descendants of xPDOObject, so they will always have the fromArray() method.

The fromArray() method has a number of additional arguments, but for this use case, the defaults for those arguments are exactly what you want.


It's worth mentioning that if you use a URL for the WebLink's content field, it doesn't have to be a link to a resource on your site. Obviously, if it's a Resource ID or a link tag, it will have to be local, but you can use a URL in that field to create a link that will forward the user to anywhere on the Web.


Coming Up

If you've been paying attention, you've noticed that we didn't set the response code. It will be set automatically to HTTP/1.1 301 Moved Permanently the next time you save the Weblink in the Manager. It's not included here because it's a little bit tricky. It's not critical because, as I read the code, it will be used as the default even if that field of the Weblink is blank. If you're like me though, you'll want to play it safe and save it when the Weblink is created. There may also be cases where you want to change it. In the next article, we'll see how to set the response code property.



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 Hosting.com (formerly A2 Hosting). (More information in the box below.)



Comments (0)


Please login to comment.

  (Login)