Jump to content

new Page() always is guest user


suntrop
 Share

Recommended Posts

I cam across this issue a long time ago. When I create a page via API that page is always "created by" the guest user. 

I tried to add a dedicated user or the admin, but had no luck. Perhaps that field is – like created date – not API-able. 

$c = new Page();
$c->createdUser = wire('users')->get('name=admin');

Although I know that some fields won't be editable via API, I think this one is really confusing. For example, the first time I noticed this, was a client of mine, who nervously reported there is a protected page created by "someone from outside". It took me a while to get behind it :-D

Link to comment
Share on other sites

7 minutes ago, suntrop said:

Thanks guys!

created_users_id works great … so I guess modified_users_id will work as well :D

I haven't checked this in a long time, but in my experience, quiet mode won't work for modified, so I ended up doing this:

$sql = "UPDATE `pages` SET `modified_users_id` = '".wire('users')->get('name=admin')->id."' WHERE `id` = '".$c->id."';";
$update = wire('database')->query($sql);

And make sure it is after your original $c->save because each save will change the modified back.

Perhaps the more important issue here, is why do you care about the name of the user that created/modified a newly created page? If it was created on the frontend via the API, then it was created by a guest user no? Or is the scenario different to this?

Link to comment
Share on other sites

Ha ha :-) guessing isn't a good habit when programming.

However, I just checked and this code will have my system user as created and modified 

$c->created_users_id = $this->systemUser;
$c->modified_users_id = $this->systemUser;
$c->save(array('quiet' => true));

 

  • Like 1
Link to comment
Share on other sites

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...