Tag: Wordpress

  • Dart/Flutter code highlighting for WordPress

    Dart/Flutter code highlighting for WordPress

    The WordPress’s SyntaxHighligter Evolved is a great plugin for syntax highlighting. It supports a ton of syntaxes and has a lot of customization. However, when I started making flutter tutorials, the plugin did not have support for dart language. For a while, I have tried using swift and javascript syntaxes for the same. But it didn’t work well. So, I decided to work on adding support for the dart language to the plugin. In this tutorial, we will see how we can add support for dart/flutter code syntax highlighting to the SyntaxHighligter Evolved plugin.

    You can follow the procedure described in this tutorial to add support for other languages as well. You can easily extend what is done here for any language you need to support.

    Prerequisites

    1. This process require adding some files to the WordPress installation directory. You need to have access to your WordPress installation via FTP.
    2. Take a full backup of your site. Even though this procedure is safe and relatively easy, keep a backup ready in case something goes wrong.

    Step 1: Create a new directory inside WordPress plugin folder

    We need to create a new plugin folder inside “wp-content/plugins”. Any name can be given for the directory. I am going to name it as “dartBrushSyntaxHighlighter“.

    Dart syntax highlight plugin directory inside wordpress installation
    Dart syntax highlight plugin directory inside wordpress installation

    Inside the directory, we will have to add 2 files. One JavaScript file that defines the dart language-specific things (like keywords, comment syntax, data-types etc.) and one PHP file that register the dart syntax brush.

    Let’s add those files in the next steps.

    Step 2: Add dart highlighter brush code

    Let’s name the file as “shBrushDart.js“. Inside this file, we need to specify the language-specific things. For dart, you can just use the following file. For other languages, you have to do your research and use appropriate keywords and data-types etc.

    ; (function () {
    
        SyntaxHighlighter = SyntaxHighlighter || (typeof require !== 'undefined' ? require('shCore').SyntaxHighlighter : null);
    
        SyntaxHighlighter.brushes.Dart = function () {
            var funcs = 'function';
            var keywords = 'abstract as assert async await break case catch class const continue covariant default deferred do dynamic else enum export extends external factory false final finally for Function get hide if implements import in interface is library mixin new null on operator part rethrow return set show static super switch sync this throw true try typedef var void while with yield';
            var datatypes = 'int double num String bool List Map void';
    
            var r = SyntaxHighlighter.regexLib;
            this.regexList = [
                { regex: r.multiLineDoubleQuotedString, css: 'string' },            		// double quoted strings
                { regex: r.multiLineSingleQuotedString, css: 'string' },	            	// single quoted strings
                { regex: r.singleLineCComments, css: 'comments' },		                   	// one line comments
                { regex: r.multiLineCComments, css: 'comments' },			                // multiline comments
                { regex: new RegExp(this.getKeywords(funcs), 'gmi'), css: 'color2' },       // functions
                { regex: new RegExp(this.getKeywords(datatypes), 'gmi'), css: 'variable' }, // datatypes
                { regex: new RegExp(this.getKeywords(keywords), 'gm'), css: 'keyword' },    // keywords
            ];
        };
    
        SyntaxHighlighter.brushes.Dart.prototype = new SyntaxHighlighter.Highlighter();
        SyntaxHighlighter.brushes.Dart.aliases = ['dart', 'Dart', 'flutter'];
    
        typeof (exports) != 'undefined' ? exports.Brush = Brush : null;
    
    })();
    

    Step 3: Register dart brush plugin

    Now we need to register the above JS file to be used with SyntaxHighligher plugin. For this, we have to add the following PHP file into the plugin directory we created in step 1. Let’s name this file as “dartBrush.php“.

    <?php
    /*
    Plugin Name: Dart Brush For SyntaxHighlighter Evolved
    Description: Adds support for the dart language to the SyntaxHighlighter Evolved plugin.
    Author: Genuine Coder
    Version: 1.0.0
    Author URI: https://genuinecoder.com/
    */
    
    // SyntaxHighlighter Evolved doesn't do anything until early in the "init" hook, so best to wait until after that.
    add_action( 'init', 'syntaxhighlighter_dart_register' );
    
    // Tell SyntaxHighlighter Evolved about this new language/brush and name it.
    add_filter( 'syntaxhighlighter_brushes', 'syntaxhighlighter_dart_addlang' );
    add_filter( 'syntaxhighlighter_brush_names', 'syntaxhighlighter_dart_add_brush_name' );
    
    /**
     *  Register the brush file with WordPress
     */
    function syntaxhighlighter_dart_register() {
    	wp_register_script( 'syntaxhighlighter-brush-dart', plugins_url( 'shBrushDart.js', __FILE__ ), array( 'syntaxhighlighter-core' ), '1.2.0', true );
    }
    
    function syntaxhighlighter_dart_addlang( $brushes ) {
    	$brushes['dart'] = 'dart';
    	$brushes['Dart'] = 'dart';
    	$brushes['Flutter'] = 'dart';
    	$brushes['flutter'] = 'dart';
        
    	return $brushes;
    }
    
    function syntaxhighlighter_dart_add_brush_name( $names ) {
    	$names['dart'] = 'Dart / Flutter';
    	return $names;
    }
    ?>
    
    Dart language syntax highlighter plugin files
    Make sure that the two files are added into the newly created directory.

    Step 4: Enable the custom plugin and use it!

    Now, go to the plugin panel of your WordPress and you will see the newly added plugin there. The plugin name and description is set on the php file. Currently, the plugin name is “Dart Brush For SyntaxHighlighter Evolved”. If you want to change it, please change it on the PHP file.

    Once the plugin is enabled, you can start adding dart or flutter code snippet using [ dart ] or [ flutter ] tag. An example dart code highlight is given below.

    void main() {
      final hello = "Hello there!";
      print('Message: $hello');
    }
    

    Conclusion

    In this tutorial, we have learned how to setup code snippet syntax highlighting for dart. For this purpose, we have created a custom plugin that works with SyntaxHighlighter evolved plugin. If you have liked this article, you might also like other WordPress tutorials I have written.

    > Dart syntax highlight, flutter syntax highlight, dart code highlight, dart syntax highlight <

  • How to update all WordPress post content URLs with SQL

    How to update all WordPress post content URLs with SQL

    Updating all the URLs and resources in the WordPress site after migrating to a new domain or migrating to a domain from IP address is a painstaking task. Luckily, there is an easy way!

    In this tutorial, I will show you the easy way to update all URLs thanks to SQL commands. The first thing you need is access to PhpMyAdmin tool or direct shell access to the database server. Once you have the database accesses, you can follow the below-mentioned steps to update all domains/IP address in your posts, comments, etc. to the new domain address.

    PhpMyAdmin giving option for running SQL query
    PhpMyAdmin giving option for running SQL query

    Find and update all the posts with old domain in the contents

    The following SQL query lists all the posts that contain the old domain. This is only for seeing the posts that have the old domain. 

    SELECT * FROM wp_posts WHERE post_content LIKE '%YOUR_OLD_DOMAIN%'

    Now, let’s replace all those old domain URLs with the new one with the following SQL command.

    UPDATE wp_posts SET post_content = replace(post_content, 'YOUR_OLD_DOMAIN', 'YOUR_NEW_DOMAIN') 
    WHERE post_content LIKE '%YOUR_OLD_DOMAIN%'
    

    Find and update all the comments with old domain

    Similar to what we have done with post content, we can replace the old domain in the comments too.

    The following SQL query lists all the comments that contain the old domain.

    SELECT * FROM wp_comments WHERE comment_content LIKE '%YOUR_OLD_DOMAIN%'
    

    Then to replace all those old domains in the comment, use the following SQL command

    UPDATE wp_comments SET comment_content = replace(comment_content, 'YOUR_OLD_DOMAIN', 'YOUR_NEW_DOMAIN') 
    WHERE comment_content LIKE '%YOUR_OLD_DOMAIN%'
    
    This should make all the URLs use your new domain.