0

I am trying to combine html and running a shortcode in my custom function output but I am getting a parse error. Please see below for what I am trying to achieve. Is this approach correct or is there a better way of achieving this?

   function gshar_text_shortcode() {
        return '<span class="kalia">Share with friends</span><div class="kalisha">' . echo do_shortcode('[share facebook="true" twitter="true" linkedin="true" email="true"]') . '</div>';
    }
    add_shortcode('globeshare', 'gshar_text_shortcode');

My [share] shortcode in functions.php

  function x_portfolio_item_social_v2() {

    $share_project_title = x_get_option( 'x_portfolio_share_project_title' );
      $enable_facebook     = x_get_option( 'x_portfolio_enable_facebook_sharing' );
      $enable_twitter      = x_get_option( 'x_portfolio_enable_twitter_sharing' );
      $enable_google_plus  = x_get_option( 'x_portfolio_enable_google_plus_sharing' );
      $enable_linkedin     = x_get_option( 'x_portfolio_enable_linkedin_sharing' );
      $enable_pinterest    = x_get_option( 'x_portfolio_enable_pinterest_sharing' );
      $enable_reddit       = x_get_option( 'x_portfolio_enable_reddit_sharing' );
      $enable_email        = x_get_option( 'x_portfolio_enable_email_sharing' );

      $share_url     = urlencode( get_permalink() );
      $share_title   = urlencode( get_the_title() );
      $share_source  = urlencode( get_bloginfo( 'name' ) );
      $share_content = urlencode( get_the_excerpt() );
      $share_image   = urlencode( x_get_featured_image_with_fallback_url() );

     [truncated...]

      if ( $enable_facebook == '1' || $enable_twitter == '1' || $enable_google_plus == '1' || $enable_linkedin == '1' || $enable_pinterest == '1' || $enable_reddit == '1' || $enable_email == '1' ) :

        ?>

        <div class="x-entry-share">
          <div class="x-share-options">
            <?php echo $facebook . $twitter . $google_plus . $linkedin . $pinterest . $reddit . urldecode( $email ); ?>
          </div>
        </div>

      <?php

    endif;

  }

add_action('wp_head', 'change_item_social_v2');

function change_item_social_v2() {
    remove_shortcode( 'share' );
    add_shortcode( 'share', 'x_portfolio_item_social_v2' );
  }
2
  • what is the exact full text of the parse error?
    – Michael
    Commented Jul 28, 2017 at 23:54
  • Michael, we have fixed the parse error, but now we are trying to figure out, why my functions.php [share] shortcode outputs at the top of the page whereas if I don't use it in my functions.php, it works fine..
    – JoaMika
    Commented Jul 29, 2017 at 9:06

1 Answer 1

2

UPDATE

Seems like the shortcode you're trying to use is not well coded. If they use it somewhere in the templates alone like echo do_shortcode('[share]'); it doesn't really matter since the '[share]' is the only content passed to do_shortcode(). In your case you're trying to reuse the shortcode in a context where it's placed among other content - that's what the shortcodes are for in the first place. Try for following code:

add_action('wp_head', function() {

  // bail if shortcode doesn't exists
  if ( ! shortcode_exists( 'share' ) ) {
    return;
  }

  remove_shortcode( 'share' ); // remove what is defined before in change_item_social_v2()

  add_shortcode( 'share', function( $args = [], $content = '' ) {
    ob_start();

    x_portfolio_item_social_v2( $args, $content );

    return ob_get_clean();
  } );
}, 100 ); // high hook order 100 > 10 (default), ensure this is called after original change_item_social_v2()

By calling ob_start() before x_portfolio_item_social_v2() we are simply preventing whatever is output, since that moment, to "display", instead we store it into a buffer which is flushed and its contents returned by ob_get_clean(), acting like a proper shortcode handler.

ORIGINAL

I don't see a parse issue here but the obvious mistake is echoing in a function that should return. You see your echo do_shortcode(...) is actually echoing immediately at that time it's run. echo will output but will not have a return and it will never be where you expect it to be (inside <div class="kalis"></div>). First try removing echo

7
  • ok I removed echo and it works but the output of [share] shortcode is displayed on top of the page, instead of being where I call it from.. I can post the full code of [share] shortcode if needed.
    – JoaMika
    Commented Jul 28, 2017 at 21:45
  • I would expect the output of [share] to be on top when echo **IS there.
    – Z. Zlatev
    Commented Jul 28, 2017 at 22:05
  • yes there is an echo, but the theme uses this function and it shows up correctly. It's only when I modify the function in my functions.php in child theme that it shows on top. Please see updated question with [share] code.
    – JoaMika
    Commented Jul 28, 2017 at 22:12
  • Are you sure x_portfolio_item_social_v2 function is passed as second argument in add_shortcode( 'share', 'x_portfolio_item_social_v2' ) ? It doesn't look like a proper shortcode handler since it doesn't return.
    – Z. Zlatev
    Commented Jul 29, 2017 at 6:57
  • yes, its passed - i forgot part of the code which i have now added at the bottom.
    – JoaMika
    Commented Jul 29, 2017 at 9:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.