Jump to content

How do we emulate WordPress "post author selection" in Processwire?


Zahari M.
 Share

Recommended Posts

Hi Guys!

Hope someone can help me with this one. I'm on the last legs of setting up my website and
this is one of the few remaining things to do!

In WordPress, one of the things you can do from the admin pages for a blog post or page is
change who the author of an article is, at any time.

I need to be able to achieve this functionality with Processwire too! But am not too sure
how to do it as I'm sure it involves hooks. And I don't quite get hooks in Processwire yet.

So best to ask here if there is some kind soul can assist me!

Why the need arises is this. I want to get some people to contribute some articles to my
site. As some of them are not too "site admin" savy, what i do is create the article with
the necessary titles, urls, summary etc all sorted for them. They just need to add in their content!

Whilst I create a user account for each of my contributors and assign them to a "contributor" role, sometimes I forget to log in to their account when I create first their articles. As my articles display the authors name in the byline, this is not acceptable as
their articles end up display my name.... Oops!

Since my site is running locally and still in development, I can go into the database and change the user id for the affected page. But once I go live, I dont want to be doing that!

So.... how do we go about achieving this?

Well I believe the first step would be to create a page reference field. We could set it up such that it accesses pages from the hidden user template. This would give us a drop down list of users in the system. We could then add this field to our articles template.

Now when we first create and save a page, I believe that an internal field that holds the id of the user / author is set. And conveniently for us, at the same time our custom author page reference field will be empty.

If we were logged in as the correct user when the page was saved, then nothing else needs to be done!

But if we got it wrong, or we wish to subsequently change the author of a page, then the idea is we can use our author field to select from the list of users who we now wish the desired author to be and then save the page.

But here is where I am now rather helpless. I dont know how to add a hook that will trigger upon a page save for my "article" template when there is now an author value present!

Also, we dont want our guest contributors being able to see the author selection field in their "Processwire interface".

I gather we need another hook here such that we will only display our author page reference field for users who are of the role "administrator"

Could anyone help me out with this one?

Really appreciate you looking at this...

Cheers!

Link to comment
Share on other sites

Since you are creating a user for each author, you can simply use a page reference field for this. If a user is selected on that field, that's the one considered the author, if there is no user selected it's assumed that the author is the user that was logged when posted.

$author = $page->get('author|createdUser');
//if chosen "Single page (Page) or boolean false when none selected" on the field settings

Of course not all authors have to be users. You could also have a "author" template, and create all authors there. In that case you could do something like:

$author = $page->author ? $page->author : $pages->get("template=author, name={$page->author->name}");

edit: none of those were tested, i just want to illustrate a concept

  • Like 9
Link to comment
Share on other sites

Or you could use this undocumented feature: 

  1. Edit your /site/config.php and set $config->advanced = true; 
  2. In your admin, go to Setup > Templates > your-post-template
  3. Click to the "System" tab. 
  4. See the last field: "Allow the created user to be changed on pages?" Check that box and Save.
  5. Restore your config.php back to normal: Edit your /site/config.php again and set $config->advanced = false. 

Now you should be able to change the "Created User" on the Settings tab of any pages using that template. 

  • Like 16
Link to comment
Share on other sites

Hi Guys

Thanks so much for the replies!

diogo... I like your approach! But question for you. If we create a page reference field for our author and add this field to our article template, how do we prevent the authors from being able to see this author field? I only want the administrator role to be able to see it. How do we hide some fields from some user roles?

Ryan... thanks for that nugget! Look forward to checking it out.

Cheers guys!

Link to comment
Share on other sites

  • 1 year later...

Or you could use this undocumented feature: 

  1. Edit your /site/config.php and set $config->advanced = true; 
  2. In your admin, go to Setup > Templates > your-post-template
  3. Click to the "System" tab. 
  4. See the last field: "Allow the created user to be changed on pages?" Check that box and Save.
  5. Restore your config.php back to normal: Edit your /site/config.php again and set $config->advanced = false. 

Now you should be able to change the "Created User" on the Settings tab of any pages using that template. 

This field seems to be hidden in processwire 2.6 to non super users. How do you allow none super users to change the page author?

Also does anyone know if there is a way to collapse some of the fields on settings page? the feild set "Who can access this page" for example runs on for a really long if you have a lot permissions and users. 

Link to comment
Share on other sites

  • 6 months later...
  • 1 year later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...