0

I'm creating my own shortcode, and within that function I'm calling a different shortcode through do_shortcode. I'm trying to call a substr after the do_shortcode call, but I'm not getting expected results. It seems like substr is being called before do_shortcode can resolve. Example:

function my_shortcode_function() {
    $string = "";
    $string = do_shortcode('[different_shortcode]');
    $string = substr($string, 4);
    return $string;
}
add_shortcode( 'my_shortcode', 'my_shortcode_function' );

This is returning "ferent_shortcode]" and not returning a string that should be resolved through the do_shortcode.

What am I doing wrong?

  • please post the code of the 'different_shortcode' function; does that also happen if the '[different_shortcode]' is a generic shortcode like '[caption]' – Michael Oct 24 '16 at 22:52
  • I created a different simple shortcode that returns the post permalink, and that does work correctly. Unfortunately, the shortcode I'm calling is in a different plugin, and I'm not sure how to fix this issue. It's called Store Locator Plus, and they have a bunch of shortcodes documented here: storelocatorplus.com/documentation/store-locator-plus/… – Zach Winnie Oct 25 '16 at 0:27
  • try to contact the developer of the plugin for help - not everything in square brackets is shortcode as defined in the WordPress shortcode API ... – Michael Oct 25 '16 at 5:01
0

using php substr requires you add a start point when calling substr function http://php.net/manual/en/function.substr.php

A possible fix:

function my_shortcode_function() {
$string = "";
$string = do_shortcode('[different_shortcode]');
$string = substr($string, 0, 4);
return $string;
}

add_shortcode( 'my_shortcode', 'my_shortcode_function' );
  • Sorry, but that didn't work. – Zach Winnie Oct 25 '16 at 0:24

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.