Support » Plugin: Code Snippets » Putting Snippets on Specific Pages

  • I was wondering if there was a way to have php code just placed on a single page or a group of pages?

    In the past I have used “PHP Code Widget” to put php into widgets. They stopped development and the plugin does not work anymore. Since then I have been using “Insert PHP Code Snippet” to put php into short codes. But neither one has been updated in a while so I was getting a bit nervous and thinking I should switch again. If your plugin let me place snippets in various places on a site, I would try it out.

Viewing 1 replies (of 1 total)
  • Plugin Author Shea Bunge

    (@bungeshea)

    Absolutely! You can achieve this using a shortcode snippet:

    add_shortcode( 'shortcode_name', function () {
        ob_start(); ?>
    
    put your HTML/PHP code here
    
    <?php return ob_get_clean();
    } );

    Replace the middle part with the code you want to output, and change shortcode_name with something more appropriate. Once added, you can use shortcode_name within posts, pages, widgets, etc to output your code.

    Additionally, Code Snippets v3 will make this entire process much easier, with a new snippet type specifically for adding shortcode snippets. Stay tuned for the release.

Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.