Having Some Issues With Custom Shortcodes

Hello guys!

I have a custom site plugin, that has some custom shortcodes. Basically, I have these shortcodes:

  1. 1 login/logout button
  2. Network blog count
  3. 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.

You can see an example here: http://weebir.com/mis-sitios

I will send the plugin code as a private response.

Thank you!

[Content Hidden]

Hey Carlos,

I’ve already assigned another member of the team to assist you further.

Ben

Thank you Ben!

Hi Carlos,

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.

Let me know if you have any questions about that.

Justin

Hello Justin!

Thank you for yuor reply.

So basically, I should only replace “echo” with “return”, correct?

Thank you!

Yep! It looks like that should work in this case.

Justin

Hello again Justin!

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.

Here is my current code:

add_action('wp_head','wpmy_redirect_logged_in_users_away_from_home');
function wpmy_redirect_logged_in_users_away_from_home() {
	global $pagenow; 

    if( is_user_logged_in() && !is_super_admin()  && $pagenow != 'wp-signup.php' && ( is_home() || is_front_page() ) ) {

		wp_redirect('http://weebir.com/mis-sitios');

		exit;

    }

}

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

Thank you for your help!

Hi Carlos,

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…

$html = '';

$html .= '<div><ul>';
$html .= '<li></li>';
$html .= '<ul></div>';

return $html;
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.

Justin

Hello Justin!

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.

What do you think could be worng here?

You’re welcome, Carlos!

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.

Justin

Thank you Justin!