Remove A semicolon from shortcode

Hi,

I've recently implemented your private messaging on a classified listings theme. So that when you're on an age page the sidebar shows the shortcode. However the button shows "> below it (see screenshot aswell). And I'm not quite sure what went wrong.

My code:

<button class="btn btn-default btn-lg berichtbutton"><span class="berichtbutton" href="<?php echo $pmshortcode ?>"></span></button>

As for what's in my string:
<?php $pmshortcode = do_shortcode('[pm_user user_id="'. get_the_author_meta( ID ) .'" subject=""]'); ?>

Now I'm an absolute beginner in coding so it might just be me making a dumb mistake. Kindly awaiting your reply!

  • Ken Kimbrell

    Hello Amarildo-

    It appears you have an extra quote and closing tag ">

    See visual representation below:

    After closer inspection, it does look like your code is not properly concatenated. I would try this code instead and see if it works.

    <button class="btn btn-default btn-lg berichtbutton">
    
    	<span class="berichtbutton" href="' . <?php echo $pmshortcode; ?> . '"></span>
    
    </button>

    Let me know how this works for you!

    Take Care,

    Ken - WPMU DEV Support

  • Amarildo
    <?php
    /**
     * Ad listing Sidebar template.
     *
     */
    $pmshortcode = do_shortcode('[pm_user user_id="'. get_the_author_meta( ID ) .'" subject=""]');
    ?>
    
    <!-- right sidebar -->
    <div class="content_right">
    	<!-- Reageer button -->
    <button class="btn btn-default btn-lg berichtbutton">
    
    	<span class="berichtbutton"> <a href="' . <?php echo $pmshortcode; ?> . '"></a></span>
    
    </button>
    
    	<div class="gegevensbox">
    		<ul>
    			<li><span><i class="glyphicon glyphicon-user"></i> Adverteerder: </span><a href="<?php echo get_author_posts_url( get_the_author_meta( 'ID' ) ); ?>"><?php the_author_meta( 'display_name' ); ?></a></span></li>
    			<li><span><i class="glyphicon glyphicon-calendar"></i> Lid sinds:</span> <?php echo appthemes_display_date( get_the_author_meta( 'user_registered' ), 'date', true ); ?></li>
    			<li><span><i class="glyphicon glyphicon-eye-open"></i> <?php appthemes_stats_counter( $post->ID ); ?></span></li>
    			<li><span><i class="glyphicon glyphicon-th-list"></i> <?php _e( 'Advertentienummer:', APP_TD ); ?></span></strong> <?php if ( get_post_meta( $post->ID, 'cp_sys_ad_conf_id', true ) ) echo get_post_meta( $post->ID, 'cp_sys_ad_conf_id', true ); else _e( 'N/A', APP_TD ); ?></li>
    		</ul>
    
    		<ul>
    			<li><span><i class="glyphicon glyphicon-bullhorn"></i> Advertentie omhoogplaatsen</span></li>
    			<li><span><i class="glyphicon glyphicon-exclamation-sign"></i> Meld misbruik</span></li>
    		</ul>
    	</div>
    
    	<?php appthemes_after_sidebar_widgets( 'ad' ); ?>
    
    </div><!-- /content_right -->
  • Ken Kimbrell

    Hello Amarildo-

    Thanks for providing the entire code. I believe after closer review that I discovered your problem :slight_smile:

    If you go into your wp-config.php file there is a really handy degug option on there that allows you to see the outputted errors on your page. By default define('WP_DEBUG', false); is set to false. The reason is, you do not want these messages being outputted to guest visiting your site. It is only there for the use of developers.

    When you input a php code and the output is something like you had, like an out of place ; or ' or ", most likely there is a php error. A quick way to determine what the error is, is by going into your wp-config.php file and setting the WP_DEBUG to true. See visual representation below:

    Upon enabling the code, I see this error:

    Now, this actually was not the cause of the extra ; but I will get to that :slight_smile:

    after inspection of your code:

    $pmshortcode = do_shortcode('[pm_user user_id="'. get_the_author_meta( ID ) .'" subject=""]');

    It appears you are trying to get the ID but it has not been defined. So if your were trying to dynamically get the current user's ID, where it would output the correct display name. This would not have worked. Instead you will need to use this code:

    $current_user = get_current_user_id();
    $pmshortcode = do_shortcode('[pm_user user_id="'. $current_user .'" subject=""]');

    As you can see, I am using the variable $current_user in place of the function get_current_user_id(); This is just common practice and makes your code a bit more tidy. If you were to say echo get_current_user_id(), depending on the current user you may get a 1 or a 2 on the output of the page. the user_id in wordpress is unique and each profile has it's own integer. This is an easy way for us to determine who is the current user and what do I want to do. By Echoing out simple functions like this, it will give you a better understanding of the data dynamically being pulled and how that function works.

    Also I recommend troubleshooting with two users, your admin user and a basic subscriber without admin privileges. That way you can see if the name is dynamically changing and the user experience of a basic user visiting the site. Of course you would want to use a different browser for the other user. So you are not logging out logging in and logging out and logging in on the same browser, this can be an infinite loop in itself :slight_smile:

    Just a little bit of advice!

    Now on to the problem, it does seem there was no need to create a hyper link in the button. If you just echo out your shortcode, you will get the desired result you are after. See code below:

    <button class="btn btn-default btn-lg berichtbutton">
    
         <span class="berichtbutton>  
    
              <?php echo $pmshortcode; ?>
    
         </span>
    
    </button>

    Happy Developing!

    Take Care,

    Ken - WPMU DEV Support

  • Amarildo

    Hi Ken,

    Thankyou so much for the detailed explanation and helping me understand the problem better. It actually did remove the semicolon. However the button itself was no longer clickable like this and the pop-up didn't show up.

    I've actually managed to fix that part just now by changing the CSS of the button itself to the following:

    .btn[disabled], fieldset[disabled] .ig-container .btn {
        cursor: -webkit-grab;
        pointer-events: visible;
        opacity: .65;
        filter: alpha(opacity=65);
        -webkit-box-shadow: none;
        box-shadow: none;
    }

    I'm not quite sure why it's setting the button as disabled, since it's active. However this combined with your code this the trick. Thanks for the great support!

  • Ken Kimbrell

    Hello Amarildo-

    I had another member asking about our private messaging button. It does appear that I may have misunderstood what you were trying to accomplish. If the desired result is to have your users go to a post and message the author of the post. Then I recommend this revision of my code:

    //////////////////////////////////
    // Private Messaging Shortcode
    /////////////////////////////////
    
    function custom_private_messaging(){
    
    //-------------- Define Variables-----------------//
    
    // Gets ID of Author
    $get_author_id 	    = get_the_author_meta('ID');
    
    // Gets display_name of Author
    $get_author         = get_the_author_meta('display_name');
    
    //-------Add the following optional values to the shortcode------------//
    // add text=""  to change the value of the button text
    // add class="" to add a class to the button and style to your liking :)
    // add subject="" to add a default subject if one is not added
    // add in_the_loop="1" if you add the shortcode to a loop
    $pmshortcode        = do_shortcode('[pm_user user_id="'. $get_author_id .'" subject="" in_the_loop="1"]');
    
    // Checks if user is logged in
    $user               = is_user_logged_in();
    
    // If user is logged in, it displays the Private Message Button
    $user_logged_in     = '<div style="text-align:center;" class="pm_get_author_id"> Message ' . $get_author . '! </div>' .
    		 		      '<div style="text-align:center;" class="align_pm_button">' . $pmshortcode . '</div>';
    
    // Else we kindly ask the current user to register before they can message other members
    $user_not_logged_in = '<div style="text-align:center;" class="not_registered">You must be a registered member<br>
    		                                                                        to send me private messages!
    		               </div>';
    
    // if conditional that checks if user is logged in
      	if ($user){
    
    	      echo $user_logged_in;      
    
    	}
    	else{
    
    		  echo $user_not_logged_in;
    	}
    }
    
    // Just add [pm_me] as a shortcode and display the custom function :)
    add_shortcode('pm_me', 'custom_private_messaging');
    
    //////////////////////////////////
    // Private Messaging Shortcode
    /////////////////////////////////

    The new revision, will now only allow your logged in users to message the author of the post. I hope this works better for you. You would have to add this short code to the loop of your post if you want it to appear in every post, it could be on single.php file or even in an includes folder and added as a template part. It depends how your current theme is structured. Use this code below to add to the loop:

    <?php echo do_shortcode( '[pm_me]' ); ?>

    I have also created a TUTORIAL on how you can add this to your theme's loop and dynamically display the Author of the post and the PM button on all Single page Posts.

    Let me know how this works for you!

    Take Care,

    Ken - WPMU DEV Support

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.