Shortcode rendering as text not as shortcode should

前端 未结 3 1212
渐次进展
渐次进展 2021-01-25 09:53

I am building a shopping website and I am trying to put a shortcode in that will show the customer a buy button and the quantity of the product the customer wants to purchase. O

相关标签:
3条回答
  • 2021-01-25 10:30

    Typically your shortcode is getting registered in a plugin or your theme's functions.php file. In a plugin it's often something like:

    add_action('init', 'register_my_shortcode');
    
    function register_my_shortcode(){
      add_shortcode('my_shortcode', 'do_my_shortcode');
    }
    

    And then you'd have a function do_my_short_code() that actually outputs the content. With something like that the shortcode is getting registered via the 'init' hook (http://codex.wordpress.org/Plugin_API/Action_Reference) which is called before WP has started figuring out what template to use, what content to output, etc.

    But some plugins will register the shortcode in a way that it is only available on pages / posts where it's going to potentially be used. For example, I can think of one plugin where they register the shortcode and enqueue some javascripts in the same function. That function checks to see if you're on a particular page before it executes so that the js files are not included unnecessarily all over the place. Since the shortcode registration takes place in the same function it means the shortcode only exists on those pages.

    Anyhow, if the shortcode is showing as existing on your archives page you know that isn't the problem, so check that first and let me know what you find.

    0 讨论(0)
  • 2021-01-25 10:32

    Add this to your functions.php

    // Allow shortcodes on widgets
    add_filter('widget_text','do_shortcode');
    
    // Allow shortcodes on pages (not tested, but should work)
    add_filter('the_content','do_shortcode');
    
    0 讨论(0)
  • 2021-01-25 10:36

    Try using single quotes in the do_shortcode call, like so:

     echo do_shortcode('$my_textbox_value');
    

    More likely though is that the shortcode isn't defined on the archive page so you'd need to look at where it is being instantiated to see if that is the issue. Normally when a shortcode just echoes out the content it means that shortcode doesn't exist. You can test easily enough by using the shortcode_exists() function:

    <?php if ( shortcode_exists( 'add_to_cart' ) ) { echo "The shortcode exists";} ?> 
    

    If that doesn't work then you know the issue is with the shortcode not being registered on your archives page. If it does work then you know it's something with the format of the content being passed to the shortcode.

    0 讨论(0)
提交回复
热议问题