Skip Navigation

[Resolved] How to take View Shortcode from one site to another site in WordPress Multisite

This support ticket is created 5 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 3 replies, has 2 voices.

Last updated by Waqar 5 years, 1 month ago.

Assisted by: Waqar.

Author
Posts
#1351059

Tell us what you are trying to do?
I want to display some View Shortcodes inside one site to another in the multisite installation.
I see that there is this code

switch_to_blog( $blog_id );
// Do something
restore_current_blog();

If I will create a shortcode in the main site like this

function view(){
switch_to_blog(5);
return do_shortcode{'[view-shortcode]'}
restore_current_blog();
}
add_shortcode('view', 'view');

You think it can work?

Because now I clone the posts from subsite to main site, I change the canonical URL to the partent subsite post and I show it, but this process it is not automated and the posts clone are a redundance in the database...

Is there any documentation that you are following? I don't know.

Is there a similar example that we can see? I don't know.

What is the link to your site? inonda.tv

#1351427

Hi Francesco,

Thank you for contacting us and I'd be happy to assist.

I've tested the approach that you're planning and it works on my test website.

You can create a custom shortcode and use the "switch_to_blog" function within, to call output from the different site's view(s) from it.

Note: A function's executions stop at the "return" statement, which is why it is better to move the "restore_current_blog" function's call before the return.

Example"


switch_to_blog( 5 );

$output = do_shortcode('[wpv-view name="slug-of-the-view"]');

restore_current_blog();

return $output;

I hope this helps and please let me know if you need any further assistance around this.

regards,
Waqar

#1351613

Thanks Waqar.

And also, I have another question.

If I will creating single shortcodes one for post title, one for featured image, one for post meta, to take them from the subsites using the

switch_to_blog( 2,5,7,8,ecc );

And I will use these shortcodes inside a View to display the last network posts like a slideshow, will Toolset View let me do it? Or it will crashes the network?

#1351655

Hi Francesco,

Thanks for writing back.

The "switch_to_blog" is a WordPress own function, which supports only a single site ID in a single call, as explained in its documentation:
https://codex.wordpress.org/Function_Reference/switch_to_blog

To get data from the multiple sites, you can use it in a loop, as shown in the example snippet under the heading "Multiple switches", in the above link.

Toolset Views components, like a content template or view, shouldn't have any issue with your custom shortcode, as long as it returns that data properly.

For more ideas, suggestions, and recommendations based on best practices around general WordPress features and functions, you can also consult dedicated WordPress community forums:

https://wordpress.org/support/forums/
https://wordpress.stackexchange.com/

regards,
Waqar