Jump to content

Creating PW page from wordpress site through api results in an Internal Server Error


fbg13
 Share

Recommended Posts

I'm using the multi-instance feature of PW to create a page from the wordpress admin and after the page is created i save its id in a custom meta field in wp.

The page should be created/updated when a post is first created or updated through wp's post_updated hook.

Here is the code:

Spoiler

function loadProcessWire()
{
	require(PW_CORE_FILE_PATH);
	$ProcessWire = new \ProcessWire\ProcessWire(PW_ROOT_PATH);
	return $ProcessWire;
}

add_action( "post_updated", "createPWPage", 10, 3 );
function createPWPage( $post_id, $post_after, $post_before )
{
	if(get_post_type($post_id) == "post")
	{
		delete_transient( "post_" . $post_id );
		$pwm = loadProcessWire();
		// if title changed
		if( $post_after->post_title !== $post_before->post_title )
		{
			// if PW page id is not set
			// must create the page
			$pwid_meta = get_post_meta($post_id, 'pw_post_id', true);
			if(empty($pwid_meta))
			{
				$p = new \ProcessWire\Page();
				$p->template = "post";
				$p->parent   = $pwm->pages->get("/posts/");
				$p->title    = $post_after->post_title;
				$p->wp_post_link    = get_the_permalink();
				$p->save();
				update_post_meta($post_id, 'pw_post_id', $p->id);
			}
			// if PW page id is set
			// page exists, just update
			else if($pwid_meta)
			{
				$p = $pwm->pages->get("parent=/posts/, id={$pwid_meta}");
				$p->title    = $post_after->post_title;
				$p->name     = $post_after->post_name;
				$p->wp_post_link    = get_the_permalink();
				$p->save();
			}
		}
		// if title didn't change
		// for existing WP pages
		else
		{
			// if PW page id is not set
			// must create the page
			$pwid_meta = get_post_meta($post_id, 'pw_post_id', true);
			if(empty($pwid_meta))
			{
				$p = new \ProcessWire\Page();
				$p->template = "post";
				$p->parent   = $pwm->pages->get("/posts/");
				$p->title    = $post_after->post_title;
				$p->name     = $post_after->post_name;
				$p->wp_post_link    = get_the_permalink();
				$p->save();
				update_post_meta($post_id, 'pw_post_id', $p->id);
			}
			// if PW page id is set
			// page exists, just update
			else if($pwid_meta)
			{
				$p = $pwm->pages->get("parent=/posts/, id={$pwid_meta}");
				$p->title    = $post_after->post_title;
				$p->name     = $post_after->post_name;
				$p->wp_post_link    = get_the_permalink();
				$p->save();
			}
		}
	}
}

 

 

The page is created in PW but its id is not saved in wp custom meta field. If i comment the "$p->save();" line i don't get the error, but i obviously need it.

On localhost it works, but on the server i get the error; same PW, WP (same plugins) and PHP version, only the os differs, Windows on localhost and Debian on the server.

Any ideas?

Update: I used the site profile exporter module to move from localhost to the live server and it seems like this was the cause as everything worked after i manually set up the templates and fields on a clean install.

I did uncheck the installed checkbox from the module's options. Could that be the reason?

Link to comment
Share on other sites

Another problem.

When i delete a post in WP i delete a page from PW too, the id of the PW page is stored in a meta field.

The code:

function loadProcessWire()
{
	if (class_exists('\ProcessWire\ProcessWire')) return;
	
	require(PW_CORE_FILE_PATH);
	$ProcessWire = new \ProcessWire\ProcessWire(PW_ROOT_PATH);
	return $ProcessWire;
}

add_action( 'before_delete_post', 'deletePWPage', 10 );
function deletePWPage($post_id)
{
	// get PW page id from meta field
	$pwid = get_post_meta($post_id, 'pw_post_id', true);
	if(!$pwid) return;
	
	$pwm = loadProcessWire();
	if($pwm->pages->get("parent=/posts/, id={$pwid}")->id)
	{
		$post = $pwm->pages->get("parent=/posts/, id=".$pwid);
		$pwm->pages->delete($post, true);
	}
}

This works fine if i delete only one post, but if i try to delete more than one post i get 

Fatal Error

Call to a member function get() on null

When deleting multiple posts, WP creates an array with posts ids and calls the wp_delete_post($post_id) function for each id, the first iteration runs and the post is deleted as well as the PW page, so the error occurs on the second iteration.

Any ideas what is going wrong? Is it PW or WP?

Link to comment
Share on other sites

The problem was with the loadProcessWire() function.

On the first iteration all was good, but on the second iteration PW was already loaded so the function returned nothing, resulting in the $pwm variable being null.

Solved it by replacing the function with

require(PW_CORE_FILE_PATH);
$pwm = new \ProcessWire\ProcessWire(PW_ROOT_PATH);

and calling

global $pwm;

instead of 

$pwm = loadProcessWire();

 

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