Add Author Profiles to your MODX Blog Articles

If you have a MODX Articles blog, the default action if you click on the author of an article is to show you other articles by that author. What if you’d rather have it link to an author bio page (or have both a link to more of the author’s articles *and* an author bio page link)?

Here’s a relatively easy way for you to provide an author bio link for each article. The trick is to either create a bio page for each author named with the author’s ID, or create a single page to show the bio for any user with a snippet and send the author’s ID to that snippet.

Finding the Code

Look in your Article template Tpl chunk for some code that looks something like this (search for “Posted on”):

[code language=”html”]<p class="post-info">
<span class="left">Posted on [[*publishedon:strtotime:date=`%b %d, %Y`]] by <a href="[[~[[*parent]]]]author/[[*publishedby:userinfo=`username`]]">[[*publishedby:userinfo=`username`]]</a></span>
[[*articlestags:notempty=`<span class="tags left">&nbsp;| Tags: [[+article_tags]]</span>`]]
[[+comments_enabled:is=`1`:then=`&nbsp;| <a href="[[~[[*id]]]]#comments" class="comments">Comments ([[+comments_count]])</a>`]]

This is the section of that code that shows the author’s other articles:

[code language=”html”]by <a href="[[~[[*parent]]]]author/[[*publishedby:userinfo=`username`]]"> . . . </a>[/code]

Your Tpl chunk may not look exactly like this one, but it’s the part inside the double quotes you want to change to show the author’s bio page instead. When MODX sees the original tag, it converts it to a link to the main blog page, followed by the authors’ username. When Articles sees that, it knows to show the author’s other articles. Notice that there’s no / before the ‘author’ reference. Typically, the main blog page is a container and the container suffix is set to / so it will automatically have one at the end of the parent URL.

Changing the Code

You can make that href point anywhere you want. How you do it depends on whether you want to create a separate new resource with a profile for each author, or whether you want to use the Profile snippet (part of the Login extra) to show the author information, in which case you only need a single page.

Using a Separate Page for Each Bio

If you have separate pages, you can create a container document (not an Article) that will be a parent to the profile pages (let’s say its ID is 12), and put the profile pages below it in the tree. Each one’s pagetitle will be ‘Author##’, where ## is the author’s ID. Make sure the parent is marked as a container.

Then change the href to:

[code language=”html”]<a href="[[~12]]Author[[*publishedby]]"> . . .</a>[/code]

The publishedby field holds the ID of the user who published the resource. If that’s not the author, you can use the createdby field.

Using a Single Page for All Bios

If you want to use the Profile snippet (a much more efficient method unless you want to hand-create each author’s bio page), you’d send everyone to the same page (again, let’s say its ID is 12), then use the Profile snippet to set placeholders for the particular user you want to show.

For security, I would put a tag for a custom snippet (called SaveAuthorId) in that Article Template that writes the author’s ID to a $_SESSION variable rather than exposing it as a parameter in the URL.

[code language=”html”][[!SaveAuthorId]][/code]
[code language=”html”]<?php
/* SaveAuthorId */
$_SESSION[‘AuthorId’] = $modx->resource->get(‘publishedby’);
return ”;

In that case, the href would look like this:

[code language=”html”]<a href="[[~12]]"> . . .</a>[/code]

For the landing page (12), you can create a complimentary snippet to get the author ID from the $_SESSION variable:

[code language=”html”][[!GetAuthorId]][/code]
[code language=”php”]<?php
return $_SESSION[‘AuthorId’];[/code]

Then, you can use that in the Profile snippet tag like this:

[code language=”html”][[!Profile? &user=`[[!GetAuthorId]]`]][/code]

You’ll have to put placeholders on the page for the various user fields you want to show. The Profile snippet will set placeholders for the username and all fields in the user Profile. It will set placeholders for all extended fields as well if you add the &useExtended=`1` property in the Profile snippet tag.

Having Your Cake and Eating it Too

What if you want both a link to the author’s other pages *and* a link to the author’s bio page? There’s no reason you can’t have both. Just leave the original link alone and add another link somewhere else in the Tpl chunk (maybe right after the other one) that links to the bio page as described above.

One Last Security Note

If you’re using a separate resource for each user’s bio page, remember that the pagetitle contains the user ID, which you might not want to expose. I would not show the pagetitle in the template for the user bio page and I’d also use a custom alias for the page that didn’t include the user’s ID.

See the Profile snippet documentation for more information.

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 quality MODX Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , , , , | Posted under MODX, MODX | RSS 2.0

Author Spotlight

Bob Ray

Bob Ray is the author of MODX: The Official Guide and over 30 MODX add-on components. He hosts Bob's Guides, a source of valuable information for MODX users, and has been very active in the MODX Forums with over 19,000 posts.

2 Comments on Add Author Profiles to your MODX Blog Articles

  1. Sean says:

    Hi Bob,
    Just having a bit of an issue with the following line:
    . . .
    I’ve changed [[~12]] to my relevant id and I’ve made it a container and have Author1 as a child.
    So on a click I get sent to the following URL:

    However it’s at this point that it doesn’t display the correct page, just wondering if this method has been depreciated?

    • Bob Ray says:

      The technique will only work if you have Friendly URLs turned on. That will create a regular URL (no index.php or id=) with the author and author id on the end.

Leave a Reply

Your email address will not be published. Required fields are marked *