Jump to content
Soma

Creating Pages via API

Recommended Posts

Btw, just found the wireshell tool ... wow! :)

... but it doesn't seem to be able to import page content, only creating pages?

Well, I made a little hack to it now, so it can take field contents as a JSON string on the commandline. Will try it out a bit and see if it fulfills my importing needs.

EDIT: Updated link to new pull request.

Edited by saml

Share this post


Link to post
Share on other sites

hi!
i'm trying to create a child for my page in form submit via ajax but i have this error

"Fatal error: Class 'Page' not found in /home/chaniataxi/public_html/site/templates/insertReservations.php on line 4"

the code in insertReservations.php is
 

$p = new Page(); // create new page object // <--line 4
$p->template = 'insertReservations'; // set template
$p->parent = wire('pages')->get('/my-reservations/'); // set the parent
$p->name = $name."-".$sname.'reservation'; // give it a name used in the url for the page
$p->title = 'Νέα κράτηση στο όνομα'.$name." ".$sname; // set page title (not neccessary but recommended)
 
// added by Ryan: save page in preparation for adding files (#1)
$p->save();
 
same with soma's code

i create a template for insertReservations.php with name insertReservations

my processwire version is 2.6.1

did anyone know why i have that error?

Share this post


Link to post
Share on other sites

I've always used a PHP file that resides outside of PW and included an include to the index.php page, but if you are working within PW, you probably don't need to include this at the top of your template:

include("/home/path/to/public_html/index.php");
  • Like 1

Share this post


Link to post
Share on other sites

Just a comment about stating both a name and a title here.

If you set both then it's up to you to ensure that the URL of the page is unique—heavy caveat, I think I am correct in stating this...

Certainly I found an error on the 2nd page creation (with some super dumb test code that did not even try to create unique URLs for pages; so probably not unexpected!), but then I was looking at how to make pages unique etc when I found I was trying too hard... I noted the error I was getting and Wanze's comment that solved my problem here, short answer, don't set a name and PW does it all for you :)

  • Like 1

Share this post


Link to post
Share on other sites

Or you can use adjustName=>true option in the save.

  • Like 4

Share this post


Link to post
Share on other sites

Or you can use adjustName=>true option in the save.

One of those hidden beauties that would be great to have them in the Cheat Sheet...

  • Like 2

Share this post


Link to post
Share on other sites

hi soma,

as this is a very popular post i think it should be mentioned, that there's a best practise for adding pages stated by ryan here: https://processwire.com/blog/posts/processwire-2.6.21-upgrades-comments-more-on-pw-3.x/#more-updates-on-processwire-3.0

$page = $this->wire(new Page());

maybe you can update your post? could save lots of troubles when using multiple instances of pw3 and i think all of us should take care of such things as soon as possible as they are working also in earlier versions :)

Share this post


Link to post
Share on other sites

hi soma,

as this is a very popular post i think it should be mentioned, that there's a best practise for adding pages stated by ryan here: https://processwire.com/blog/posts/processwire-2.6.21-upgrades-comments-more-on-pw-3.x/#more-updates-on-processwire-3.0

$page = $this->wire(new Page());

maybe you can update your post? could save lots of troubles when using multiple instances of pw3 and i think all of us should take care of such things as soon as possible as they are working also in earlier versions :)

Thanks bernhard for the hint. But this is only in modules and when multi-instance is used. The example here is in a bootstrap where there's no $this.

What do you mean by "they are working also in earlier versions" ?

As those multi-instance examples like

$page = $this->wire(new Page());

isn't possible in 2.7 for example. Or am I wrong?

  • Like 1

Share this post


Link to post
Share on other sites

ah, you are right - that was my mistake! still new to all this OOP and namespacing but i'm making progress :)

Share this post


Link to post
Share on other sites

I know that this post is a little old ut it should work even now, right?

My question is: what's the content of that index.php that @Soma included in the first line?

Share this post


Link to post
Share on other sites

@Artomultiplo of course everything is still working.

The index.php file is the main processwire file, see https://processwire.com/api/include/

If you only want to create new pages from inside processwire (eg in a template file) you don't need to include index.php, because processwire is already booted.

  • Like 2

Share this post


Link to post
Share on other sites

Thank you all very much!

I've instead used the "insert into template" method, but the include method is a lot better.

I'm just starting up my processwire exploration with a project where I need to get some data from a JSON from a movie booking app and I'm actually doing giant steps considering that this is my first time with JSON and/or PHP.

Is amazing, I'm doing stuff that I could only dream of when working with wordpress. I'm soooo exited... for real.

This is the gist of what I'm using for creating the pages based on a json that I pull from an URL

https://gist.github.com/protorob/e6050c78b4bee2dbce59c38234afa0de

Any suggestion is well received.

Thanks again fr your support!

  • Like 2

Share this post


Link to post
Share on other sites
4 minutes ago, Artomultiplo said:

Is amazing, I'm doing stuff that I could only dream of when working with wordpress. I'm soooo exited... for real.

Welcome to the world of ProcessWire ;) Same feeling here for the last 4 Years ;)

Regarding your code: Maybe you need to check if the given pagename already exists. Not sure if that happens automatically or not, but you should definitely try and see if it throws an error.

  • Thanks 1

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...