Search code examples
htmlbbcode

How to replace HTML tags by custom BBCode tags?


I’m creating a blog system without any CMS.

Is it possible to create custom replacements for HTML tags for the blog like: <img src="linktopic"> to something like [img="linktopic"].

If it’s possible the same for an <iframe> and a <a> tag.


Solution

  • That's an example of a wordpress shortcode: https://codex.wordpress.org/Shortcode_API

    A simple example of creating a shortcode will be to go to your website's theme folder and add the following code to your functions.php file:

    //[foobar]
    function foobar_func( $atts ){
        return "foo and bar";
    }
    add_shortcode( 'foobar', 'foobar_func' );
    

    So when you will use [foobar] in your backend, the frontend will display "foo and bar" text.

    If you are using wordpress, you can create custom shortcodes. You can find a tutorial here: https://www.elegantthemes.com/blog/tips-tricks/how-to-create-shortcodes-in-wordpress

    There's also a shortcode generator here: https://generatewp.com/shortcodes/

    If you want to use shortcodes outside of Wordpress

    That's possible. There's a composer package that allows you to create wordpress like shortcodes in any PHP application: https://github.com/maiorano84/shortcodes

    You can find a tutorial here: https://www.codediesel.com/php/adding-wordpress-like-shortcodes-to-your-web-applications/