0

My theme uses this code which I am trying to convert to a shortcode so I can use on custom templates:

<div class="x-breadcrumb-wrap">
      <div class="x-container max width">

        <?php x_breadcrumbs(); ?>

      </div>
    </div>

I know I have to add a shortcode like this to my functions.php but can't figure out the syntax.

add_shortcode( 'mycrumbs', 'mytest_breadcrumbs' );

UPDATE:

For easy of use I only need to call the function x_breadcrumbs. My theme defines this function as:

function x_breadcrumbs() {

    if ( x_get_option( 'x_breadcrumb_display' ) ) {

      GLOBAL $post;

      $is_ltr         = ! is_rtl();
      $stack          = x_get_stack();
      $delimiter      = x_get_breadcrumb_delimiter();
      $home_text      = x_get_breadcrumb_home_text();
      $home_link      = home_url();
      $current_before = x_get_breadcrumb_current_before();
      $current_after  = x_get_breadcrumb_current_after();
      $page_title     = get_the_title();
      $blog_title     = get_the_title( get_option( 'page_for_posts', true ) );

      if ( ! is_404() ) {
        $post_parent = $post->post_parent;
      } else {
        $post_parent = '';
      }

      if ( X_WOOCOMMERCE_IS_ACTIVE ) {
        $shop_url   = x_get_shop_link();
        $shop_title = x_get_option( 'x_' . $stack . '_shop_title' );
        $shop_link  = '<a href="'. $shop_url .'">' . $shop_title . '</a>';
      }

      echo '<div class="x-breadcrumbs"><a href="' . $home_link . '">' . $home_text . '</a>' . $delimiter;

        if ( is_home() ) {

          echo $current_before . $blog_title . $current_after;

        } elseif ( is_category() ) {

          $the_cat = get_category( get_query_var( 'cat' ), false );
          if ( $the_cat->parent != 0 ) echo get_category_parents( $the_cat->parent, TRUE, $delimiter );
          echo $current_before . single_cat_title( '', false ) . $current_after;

        } elseif ( x_is_product_category() ) {

          if ( $is_ltr ) {
            echo $shop_link . $delimiter . $current_before . single_cat_title( '', false ) . $current_after;
          } else {
            echo $current_before . single_cat_title( '', false ) . $current_after . $delimiter . $shop_link;
          }

        } elseif ( x_is_product_tag() ) {

          if ( $is_ltr ) {
            echo $shop_link . $delimiter . $current_before . single_tag_title( '', false ) . $current_after;
          } else {
            echo $current_before . single_tag_title( '', false ) . $current_after . $delimiter . $shop_link;
          }

        } elseif ( is_search() ) {

          echo $current_before . __( 'Search Results for ', '__x__' ) . '&#8220;' . get_search_query() . '&#8221;' . $current_after;

        } elseif ( is_singular( 'post' ) ) {

          if ( get_option( 'page_for_posts' ) == is_front_page() ) {
            echo $current_before . $page_title . $current_after;
          } else {
            if ( $is_ltr ) {
              echo '<a href="' . get_permalink( get_option( 'page_for_posts' ) ) . '">' . $blog_title . '</a>' . $delimiter . $current_before . $page_title . $current_after;
            } else {
              echo $current_before . $page_title . $current_after . $delimiter . '<a href="' . get_permalink( get_option( 'page_for_posts' ) ) . '">' . $blog_title . '</a>';
            }
          }

        } elseif ( x_is_portfolio() ) {

          echo $current_before . get_the_title() . $current_after;

        } elseif ( x_is_portfolio_item() ) {

          $link  = x_get_parent_portfolio_link();
          $title = x_get_parent_portfolio_title();

          if ( $is_ltr ) {
            echo '<a href="' . $link . '">' . $title . '</a>' . $delimiter . $current_before . $page_title . $current_after;
          } else {
            echo $current_before . $page_title . $current_after . $delimiter . '<a href="' . $link . '">' . $title . '</a>';
          }

        } elseif ( x_is_product() ) {

          if ( $is_ltr ) {
            echo $shop_link . $delimiter . $current_before . $page_title . $current_after;
          } else {
            echo $current_before . $page_title . $current_after . $delimiter . $shop_link;
          }

        } elseif ( x_is_buddypress() ) {

          if ( bp_is_group() ) {
            echo '<a href="' . bp_get_groups_directory_permalink() . '">' . x_get_option( 'x_buddypress_groups_title' ) . '</a>' . $delimiter . $current_before . x_buddypress_get_the_title() . $current_after;
          } elseif ( bp_is_user() ) {
            echo '<a href="' . bp_get_members_directory_permalink() . '">' . x_get_option( 'x_buddypress_members_title' ) . '</a>' . $delimiter . $current_before . x_buddypress_get_the_title() . $current_after;
          } else {
            echo $current_before . x_buddypress_get_the_title() . $current_after;
          }

        } elseif ( x_is_bbpress() ) {

          remove_filter( 'bbp_no_breadcrumb', '__return_true' );

          if ( bbp_is_forum_archive() ) {
            echo $current_before . bbp_get_forum_archive_title() . $current_after;
          } else {
            echo bbp_get_breadcrumb();
          }

          add_filter( 'bbp_no_breadcrumb', '__return_true' );

        } elseif ( is_page() && ! $post_parent ) {

          echo $current_before . $page_title . $current_after;

        } elseif ( is_page() && $post_parent ) {

          $parent_id   = $post_parent;
          $breadcrumbs = array();

          if ( is_rtl() ) {
            echo $current_before . $page_title . $current_after . $delimiter;
          }

          while ( $parent_id ) {
            $page          = get_page( $parent_id );
            $breadcrumbs[] = '<a href="' . get_permalink( $page->ID ) . '">' . get_the_title( $page->ID ) . '</a>';
            $parent_id     = $page->post_parent;
          }

          if ( $is_ltr ) {
            $breadcrumbs = array_reverse( $breadcrumbs );
          }

          for ( $i = 0; $i < count( $breadcrumbs ); $i++ ) {
            echo $breadcrumbs[$i];
            if ( $i != count( $breadcrumbs ) -1 ) echo $delimiter;
          }

          if ( $is_ltr ) {
            echo $delimiter . $current_before . $page_title . $current_after;
          }

        } elseif ( is_tag() ) {

          echo $current_before . single_tag_title( '', false ) . $current_after;

        } elseif ( is_author() ) {

          GLOBAL $author;
          $userdata = get_userdata( $author );
          echo $current_before . __( 'Posts by ', '__x__' ) . '&#8220;' . $userdata->display_name . $current_after . '&#8221;';

        } elseif ( is_404() ) {

          echo $current_before . __( '404 (Page Not Found)', '__x__' ) . $current_after;

        } elseif ( is_archive() ) {

          if ( x_is_shop() ) {
            echo $current_before . $shop_title . $current_after;
          } else {
            echo $current_before . __( 'Archives ', '__x__' ) . $current_after;
          }

        }

      echo '</div>';

    }

  }
endif;
  • Are there versions of your x_ functions that return values instead of echoing them? – Andy Macaulay-Brook Oct 13 '16 at 19:04
  • please see update.. – Joanna Mikalai Oct 13 '16 at 21:10
  • @JoannaMikalai your new code will have to be wrapped in ob_*() functions again, the reason for this is that both your codes, 1st and 2nd one produce output, but shortcodes must not produce output (see add_shortcode docs), so you have to capture output with ob_start and then save if with ob_get_clean() then return it, this is the only way you can do it. Or you will have to rewrite the function removing any echo or similar, and this seems doable for your function. One drawback, if the original function is improved by the theme author, will you keep track of that and modify your code? – Nabil Kadimi Oct 14 '16 at 6:40
  • And again, ob_* is safe stackoverflow.com/questions/4401949/… it becomes dangerous if not used currectly, as any code in any language. – Nabil Kadimi Oct 14 '16 at 6:42
1

This should do it, I used output buffering with ob_start() and ob_get_clean:

<?php

/**
 * Breadcrumbs based on theme's functions
 *
 * @author  Nabil Kadimi <nabil@kadimi.com>
 * @link    http://wordpress.stackexchange.com/a/242547/17187
 */
add_action( 'init', function() {
    add_shortcode( 'mycrumbs', function() {

        /**
         * Start capturing output.
         */
        ob_start();

        ?>
        <div class="x-breadcrumb-wrap">
            <div class="x-container max width">
                <?php x_breadcrumbs(); ?>
                <?php if ( is_single() || x_is_portfolio_item() ) : ?>
                    <?php x_entry_navigation(); ?>
                <?php endif; ?>
            </div>
        </div>
        <?

        /**
         * Stop capturing output and return what was captured to WordPress.
         */
        return ob_get_clean();

    } ); // add_shortcode( 'mycrumbs', closure );
} ); // add_action( 'init', closure );
|improve this answer|||||
  • 1
    Someone told me to avoid ob_start - what is the purpose of this? – Joanna Mikalai Oct 13 '16 at 15:06
  • i am getting white screen of death with this code – Joanna Mikalai Oct 13 '16 at 15:23
  • Make sure you have a supported version of PHP (5.6 or 7, although 5.4 should support that code), check your error logs for errors or turn on WordPress debugging to see what is the error. I can't suggest a fix if I don't know the error. – Nabil Kadimi Oct 13 '16 at 15:59
  • @JoannaMikalai the point of the output buffering is to get around your x_ functions echoing output but a shortcode function needing to return a value instead. You're right that output buffering is frowned upon, I believe because it will cause problems in some circumstances if two plugins both try and use it. – Andy Macaulay-Brook Oct 13 '16 at 19:02
  • @JoannaMikalai And output buffering is perfectly safe if done correctly – Nabil Kadimi Oct 13 '16 at 20:48
0

try this?

function mytest_breadcrumbs() {

$crumbs ='<div class="x-breadcrumb-wrap"><div class="x-container max width">';
$crumbs.=x_breadcrumbs();
if ( is_single() || x_is_portfolio_item() ) {
$crumbs.=x_entry_navigation();
}
$crumbs.='</div></div>';


    // Code
return $crumbs;
}
add_shortcode( 'mycrumbs', 'mytest_breadcrumbs' );

then call in page with [mycrumbs]

|improve this answer|||||
  • But the x_ functions don't return values according to the OP's original code. – Andy Macaulay-Brook Oct 13 '16 at 19:04
  • i dont' know what those functions look like but the error i get when i run is that i can't find the x_functions, which is exactly as I would expect since i don't have them. i dont' link ob_start either but it may be the only solution. I'll wait until OP replies and then comment further if the above doesn't work – rudtek Oct 13 '16 at 19:11
  • please see update of my question.. – Joanna Mikalai Oct 13 '16 at 21:09
  • did my solution work? – rudtek Oct 17 '16 at 18:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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