开发者

do_shortcode not working

开发者 https://www.devze.com 2023-02-16 05:01 出处:网络
I\'ve been stuck on this for a while. I\'m working on a wordpress site where I wrote the theme from scratch, I use php calls to get the wordpress functionality that I need in certain sections.

I've been stuck on this for a while. I'm working on a wordpress site where I wrote the theme from scratch, I use php calls to get the wordpress functionality that I need in certain sections.

I'm trying to use a plugin, but calling it via

echo do_shortcode('[STORE-LOCATOR]');

just isnt working. Even when I switch to the default template and post that code, it still doesnt work. It simply echoes "[STORE-LOCATOR]"

Any help would be g开发者_StackOverflow中文版reatly appreciated.


[STORE-LOCATOR] is probably not a 'shortcode' in WordPress sense.

I encountered this on different plugin, Stream media player. They use the same syntax as shortcodes, but they are actually not.

Try using:

echo apply_filters( 'the_content',' [STORE-LOCATOR] ');

instead of do_shortcode, and see if it helps.


do_shortcode() returns a string. I get it working by doing:

<?php echo do_shortcode(...); ?>


This is specific to the Store Locator plugin, not do_shortcode in general.

apply_filters can be an acceptable workaround for other plugins, but this does not work for Store Locator; you will only see an empty space and some controls. This is because it is looking for that shortcode in the page/post body to determine whether or not to include all of its js references at the top of the page. And without these references, nothing will work. See the sl_head_scripts function in sl-functions.php.

To change this behavior, simply modify that function to match based upon page title instead. In my instance I wanted it only on a "shop" page, so I commented out the entire $on_sl_page test and replaced it with this:

$on_sl_page = ( strpos($pagename, 'shop') === 0 );

I then called it from my page with apply_filters as indicated in the other answer:

echo apply_filters( 'the_content','[STORE-LOCATOR]');

And this appears to work perfectly.


echo do_shortcode('[STORE-LOCATOR][/STORE-LOCATOR]');


Try using shortcode after the WordPress environment has been set up.

function my_function() {
   echo do_shortcode('[STORE-LOCATOR]');
}
             
add_action('wp', 'my_function');


If you're writing the whole thing from scratch, you'll want to make sure that the function you create is in the root php file of your plugin. The function might look something like this, but you'll have to sub in whatever logic you're using to arrive at the store location:

<?php
function doCoolStuff () {
$var1 = "value1";
$var2 = "value2";
$output = $var1+$var2;
}
return $output;
}
add_shortcode('SOTRE-LOCATIOR', 'doCoolStuff');
?>

Then in your template put the code:

<?php echo do_shortcode('[STORE-LOCATOR]');?>

Happy coding and good luck!

0

精彩评论

暂无评论...
验证码 换一张
取 消