Jump to content

Possible to insert new data using api?


ChrisKB
 Share

Recommended Posts

Creating content in PW API is as simple as it can get.

First we want to create a new Page object, then fill some properties and give it a template, parent and a title. Now save it.

Or to modify an existing, you can simply load a page with $pages->get(id|path|selector); and change properties and save it back.

In praxis this basicly looks like this from within a template code:

$mypage = new Page();
$mypage->template  = "basic-page";
$mypage->parent = $pages->get("/"); // root
$mypage->title = "My New Page";
$mypage->image = "http://www.some.com/img/myimage.jpg";
$mypage->body = "Hello World";
$mypage->save();

Or within a php file bootstrap of the PW index.php, ( or template scope too )

// bootstrap API
include("./index.php");

$mypage = new Page();
$mypage->template  = "basic-page";
$mypage->parent = wire("pages")->get("/"); // root
....
$mypage->save();

To load and edit a page which has fields with text formatters like dates or text fields, you might need to turn off output formatting of the page you edit. $page->setOutputFormatting(false); or alternative short syntax: $page->of(false);

$mypage = wire("pages")->get("/about/");
$mypage->of(false);
$mypage->template  = "basic-page";
....
$mypage->save();
  • Like 2
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

×
×
  • Create New...