How to explore within the program source code

A star forming region in the sky

I will describe my own method of exploring and modifying the program source code.

Page Language Swicher

Benefits of Open Source

The term open source became widely used when I was a student.

Even before that, the source codes of BSD un*x and GNU products had been released to the public, but it was not until Linux became widely used 30 years ago that licenses were decided and the source codes were more actively released.

Needless to say, the advantage of open source is that the source code can be viewed by anyone.When there is a question about the behavior of a program, if the source code is publicly available, the cause can be investigated, and with a little knowledge, it can be corrected.

The article I published last night (or rather, the date had changed) was only able to be modified because the source code was publicly available.

Personally, I prefer open source software to closed source.

How to explore the source code

From this case study, I will describe how I explore the source code and my methodology.

Keyword Selection

To begin, look for keywords.

This time there was a problem with the catchphrase block of the site.

Most of the source code is commented in English, and when I turned on the WordPress admin page to English and looked at the English description of the block in question, I saw “Site Tagline”.

In Japanese it is a “catchphrase,” but in English it is a “tagline.”

Now we know that the keyword we are looking for is “tagline”.

I also googled “site tagline” and found that bloginfo(‘description’) can be used to get the tagline.

Add “bloginfo” and “description’ to the keywords.

Search recursively (grep -r)

Once the keywords are determined, the system looks for files that contain the keywords in all files in the source code.

Enter the rental server via ssh and run the grep command in the directory where WordPress is installed.

Bash
grep -r 'tagline'
grep -r 'bloginfo'
grep -r 'description'
Bash

The ‘-r’ option searches each file recursively.

Executing the grep command will display the name of the file containing each keyword and the corresponding line.

The results of the grep command are piped to less (the pager), which then searches for additional keywords.

Bash
grep -r 'tagline' | less
Bash

In the case of WordPress, themes and plug-ins are located in the wp-content directory, and WordPress itself is located in the wp-includes directory.You can find the files in the block directory.

As a result, we arrived at the wp-includes/blocks/site-tagline.php file.

Correction and operation check

Once you have located the relevant file, make a backup first.

Bash
cp site-tagline.php site-tagline.php.orig
Bash

This will allow you to undo any problems.

Open site-tagline.php in an editor (in this case vim) and search for the appropriate line by “bloginfo” or “description”.

PHP
function render_block_core_site_tagline( $attributes ) {
        $site_tagline = get_bloginfo( 'description' );
        if ( ! $site_tagline ) {
               return;
        }
site-tagline.php

I found that get_bloginfo(‘description’) was getting the tagline, so I changed it to get it with bloginfo(‘description’).

Since it is not possible to check if the cache remains, clear the cache of the LiteSpeed Cache plugin, and clear the inline CSS and inline JS.Then load the page and check the operation.

In this case, we were able to confirm that the modification made the site tagline multilingual to match the locale of the page.

This completes the correction.

Musubi

I described how I usually explore and modify the source code.

We are not doing anything special.But it takes time to understand the contents of all the source code from scratch, and it takes a lot of effort to get to the source of the problem.

By focusing the investigation on a specific point, the effort to deal with the problem can be greatly reduced.

Although some experience is required, repeating this process will lower the hurdle to modifying the source code.

I was able to fix it this way this time, but originally it would be best to be able to fix it in functions.php instead of modifying the source code. php doesn’t seem to be able to redefine functions, but I will look for a way around it.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *