I have a custom site plugin, that has some custom shortcodes. Basically, I have these shortcodes:
1 login/logout button
Network blog count
Current user blogs list
The problem I am getting is that for shortcodes 1 and 3, for some reason, is being displayed before the beaver content. Shortcode 2 is not getting the problem though.
It looks like the issue is that your shortcode functions are echoing content instead of returning it. As per the codex, shortcode functions should return content…
Note that the function called by the shortcode should never produce output of any kind. Shortcode functions should return the text that is to be used to replace the shortcode. Producing the output directly will lead to unexpected results. This is similar to the way filter functions should behave, in that they should not produce expected side effects from the call, since you cannot control when and where they are called from.
I changed all the “echo” with “return”. For the shortcode [boton-especial] it is working correctly.
For the shortcode [userblogs_list] it is not working correctly though. This shortcode should have two different instances.
A) When a user is logged out, a text should display. This is working correctly.
B) When a user is logged in, the shortcode should display a list of the users sites + a “Add a new site”. This is not being displayed.
Here is the result of the code with the Return value:
function count_sitiosdeusuario( $atts = null ){
if(is_user_logged_in()) {
global $current_user;
$blogs = get_blogs_of_user( $current_user->id );
if($blogs) {
return '<div><ul>';
foreach ( $blogs as $blog ) {
if($blog->userblog_id != 1){
return '<li><a href="http://' . $blog->domain . $blog->path .'">' . $blog->blogname . '</a></li>';
}
}
return '<li>Crear Un Nuevo Sitio Web</li></ul></div>';
}
} else {
return 'Por favor, ingresa para ver tus sitios <a href="'. network_site_url() . 'login">aqui</a>';
}
}
add_shortcode( 'userblogs_list', 'count_sitiosdeusuario' );
Also, I am getting a different error. The last function of the plugin is intended to redirect logged in users from the home page to the page that displays the list of their sites. Since I am using the bb-theme I think I should change something, but I am not sure what.
But it is displaying this error when visiting the home page:
Warning: Cannot modify header information - headers already sent by (output started at /home2/cdivas2/0SitiosWeb/W/Weebir/wp-content/themes/bb-theme/header.php:2) in /home2/cdivas2/0SitiosWeb/W/Weebir/wp-includes/pluggable.php on line 1196
B) When a user is logged in, the shortcode should display a list of the users sites + a “Add a new site”. This is not being displayed.
That is happening because you have multiple return statements. Once a return statement is reached, the function is exited. You instead need to build your HTML string in a variable and return that. Here’s an example…
Also, I am getting a different error. The last function of the plugin is intended to redirect logged in users from the home page to the page that displays the list of their sites. Since I am using the bb-theme I think I should change something, but I am not sure what.
Redirects can’t happen after page content is displayed because they use PHP headers that are already sent by that point. Try changing the action from wp_head to init.
Thank you for your amazing help. I have fixed the user blogs list shortcode.
I still get problems with the redirection though. I changed “add_action(‘wp_head’,” with “add_action(‘init’,”. Now there is no error, but there is no redirection either.
I dont understand why it is not working, since I also have another test network using your theme and the exact same code is working. Before switching to beaver, I was using WPMU DEV’s upfront theme and it was working there also.
I still get problems with the redirection though. I changed “add_action(‘wp_head’,” with “add_action(‘init’,”. Now there is no error, but there is no redirection either.
It’s possible that a condition in your “if” statement isn’t true which is causing the redirect to be bypassed. This is a bit beyond the scope of Beaver Builder, so it’s hard for me to say what that might be. It doesn’t appear to be a theme issue, so you might consider contacting the person you got that code from.