Skip to main content

When I call switch_to_blog()switch_to_blog() with a blog id, I don’t know whether that blog actually exists. The function returns always TRUEtrue.

Test case:

switch_to_blog( PHP_INT_MAX );
$post = get_post( 1 );
restore_current_blog();
switch_to_blog(PHP_INT_MAX);
$post = get_post(1);
restore_current_blog();

This will result in database errors whichthat are exposed to the user. How can I prevent that?

Real world-world use case

I was the lead developer of Multilingual PressMultilingualPress. When a user translates a post, she gets a screen like this:

enter image description here

Now the following can happen:

  1. She saves the post successfully and continues translating the post.
  2. Another user, a network admin, deletes the German blog while she is writing.
  3. She hits save again and gets database errors.

I want to avoid that scenario. How can I check quickly if the target blog exists? I call switch_to_blog() very often in multiple different classes, so it has to be fast.

When I call switch_to_blog() with a blog id, I don’t know whether that blog actually exists. The function returns always TRUE.

Test case:

switch_to_blog( PHP_INT_MAX );
$post = get_post( 1 );
restore_current_blog();

This will result in database errors which are exposed to the user. How can I prevent that?

Real world use case

I was the lead developer of Multilingual Press. When a user translates a post, she gets a screen like this:

enter image description here

Now the following can happen:

  1. She saves the post successfully and continues translating the post.
  2. Another user, a network admin, deletes the German blog while she is writing.
  3. She hits save again and gets database errors.

I want to avoid that scenario. How can I check quickly if the target blog exists? I call switch_to_blog() very often in multiple different classes, so it has to be fast.

When I call switch_to_blog() with a blog id, I don’t know whether that blog actually exists. The function returns always true.

Test case:

switch_to_blog(PHP_INT_MAX);
$post = get_post(1);
restore_current_blog();

This will result in database errors that are exposed to the user. How can I prevent that?

Real-world use case

I was the lead developer of MultilingualPress. When a user translates a post, she gets a screen like this:

enter image description here

Now the following can happen:

  1. She saves the post successfully and continues translating the post.
  2. Another user, a network admin, deletes the German blog while she is writing.
  3. She hits save again and gets database errors.

I want to avoid that scenario. How can I check quickly if the target blog exists? I call switch_to_blog() very often in multiple different classes, so it has to be fast.

Grammar. sigh
Source Link
fuxia
  • 107.1k
  • 38
  • 255
  • 459

When I call switch_to_blog() with a blog id, I don’t know ifwhether that blog actually exists. The functionsfunction returns always TRUE.

Here is my problemTest case:

switch_to_blog( PHP_INT_MAX );
$post = get_post( 1 );
restore_current_blog();

This will result in database errors which are exposed to the user. How can I prevent that?

Real world use case

I amwas the lead developer of Multilingual Press. When a user translates a post, she gets a screen like this:

enter image description here

Now the following can happen:

  1. She saves the post successfully and continues translating the post.
  2. Another user, a network admin, deletes the German blog while she is writing.
  3. She hits save again and gets database errors.

I want to avoid that scenario. How can I check quickly if the target blog exists? I call switch_to_blog() very often in multiple different classes, so it has to be fast.

When I call switch_to_blog() with a blog id, I don’t know if that blog actually exists. The functions returns always TRUE.

Here is my problem:

switch_to_blog( PHP_INT_MAX );
$post = get_post( 1 );
restore_current_blog();

This will result in database errors which are exposed to the user. How can I prevent that?

Real world use case

I am the lead developer of Multilingual Press. When a user translates a post, she gets a screen like this:

enter image description here

Now the following can happen:

  1. She saves the post successfully and continues translating the post.
  2. Another user, a network admin, deletes the German blog while she is writing.
  3. She hits save again and gets database errors.

I want to avoid that scenario. How can I check quickly if the target blog exists? I call switch_to_blog() very often in multiple different classes, so it has to be fast.

When I call switch_to_blog() with a blog id, I don’t know whether that blog actually exists. The function returns always TRUE.

Test case:

switch_to_blog( PHP_INT_MAX );
$post = get_post( 1 );
restore_current_blog();

This will result in database errors which are exposed to the user. How can I prevent that?

Real world use case

I was the lead developer of Multilingual Press. When a user translates a post, she gets a screen like this:

enter image description here

Now the following can happen:

  1. She saves the post successfully and continues translating the post.
  2. Another user, a network admin, deletes the German blog while she is writing.
  3. She hits save again and gets database errors.

I want to avoid that scenario. How can I check quickly if the target blog exists? I call switch_to_blog() very often in multiple different classes, so it has to be fast.

Tweeted twitter.com/#!/StackWordPress/status/445753976366333952
Source Link
fuxia
  • 107.1k
  • 38
  • 255
  • 459

How can I trust switch_to_blog()?

When I call switch_to_blog() with a blog id, I don’t know if that blog actually exists. The functions returns always TRUE.

Here is my problem:

switch_to_blog( PHP_INT_MAX );
$post = get_post( 1 );
restore_current_blog();

This will result in database errors which are exposed to the user. How can I prevent that?

Real world use case

I am the lead developer of Multilingual Press. When a user translates a post, she gets a screen like this:

enter image description here

Now the following can happen:

  1. She saves the post successfully and continues translating the post.
  2. Another user, a network admin, deletes the German blog while she is writing.
  3. She hits save again and gets database errors.

I want to avoid that scenario. How can I check quickly if the target blog exists? I call switch_to_blog() very often in multiple different classes, so it has to be fast.