banner
胡鹤仙的Blockchain Blog

胡鹤仙的Blockchain Blog

twitter
github
telegram
medium

Learn a skill: Use ChatGPT to help me write code.

I am considered one of the first group of people to use ChatGPT. I registered for it in December last year. So far, I have used it to help me write surveys, daily assignments, and also to ask some strange questions. Yesterday, I used it to help me write a piece of WordPress code.

At first, I saw on Lin Mumo's blog that he wrote "Which book have I finished writing" and I understood the principle: to get the total number of words in all articles on the website, and then see which range your word count falls into, and output the corresponding book. However, he used Hugo, so I asked him how to modify it for use on WordPress. He gave me a modified version for Typecho, which can be found in the article "Total Word Count of Blog". This confused me, so I immediately sought help from ChatGPT.

I first sent the code from Lin Mumo's blog to it and explained the code principle.

20230417084350.png

Then, I asked it to modify this code to make it usable for WordPress, but the modified code seemed to have no effect. So I sent it the Typecho version for modification, and it also explained the code principle to me.

20230417084410.png

Finally, I asked it to modify the code based on the above code to make it usable for WordPress. Although it couldn't generate the entire code at once due to the character limit, fortunately, it succeeded.

I posted the complete code and explained how to use it.

/*
 * Which book have I finished writing?
 * Original author: Lin Mumo
 * Modified by: Hu Hexian & ChatGPT
 */
function allwords() {
    global $wpdb;
    $chars = 0;
    $results = $wpdb->get_results("SELECT post_content FROM {$wpdb->posts} WHERE post_status = 'publish' AND post_type = 'post'");
    foreach ($results as $result) { $chars += mb_strlen(trim($result->post_content), 'UTF-8'); }
    if($chars<50000){
    echo 'The total word count of the website is '.$chars.', still working on updates.. Keep it up!';}
    elseif ($chars<70000 && $chars>50000){
    echo 'The total word count of the website is '.$chars.', finished reading "The Little Prince" by Antoine de Saint-Exupéry!';}
    elseif ($chars<90000 && $chars>70000){
    echo 'The total word count of the website is '.$chars.', finished reading "Outcry" by Lu Xun!';}
    elseif ($chars<100000 && $chars>90000){
    echo 'The total word count of the website is '.$chars.', finished reading "Old Tales of the South" by Lin Haiyin!';}
    elseif ($chars<110000 && $chars>100000){
    echo 'The total word count of the website is '.$chars.', finished reading "The Prince and the Pauper" by Mark Twain!';}
    elseif ($chars<120000 && $chars>110000){
    echo 'The total word count of the website is '.$chars.', finished reading "Hesitation" by Lu Xun!';}
    elseif ($chars<130000 && $chars>120000){
    echo 'The total word count of the website is '.$chars.', finished reading "To Live" by Yu Hua!';}
    elseif ($chars<140000 && $chars>130000){
    echo 'The total word count of the website is '.$chars.', finished reading "Thunderstorm" by Cao Yu!';}
    elseif ($chars<150000 && $chars>140000){
    echo 'The total word count of the website is '.$chars.', finished reading "The Destiny of Writing" by Shi Tiesheng!';}
    elseif ($chars<160000 && $chars>150000){
    echo 'The total word count of the website is '.$chars.', finished reading "The Secret Garden" by Frances Hodgson Burnett!';}
    elseif ($chars<170000 && $chars>160000){
    echo 'The total word count of the website is '.$chars.', finished reading "Sunrise" by Cao Yu!';}
    elseif ($chars<180000 && $chars>170000){
    echo 'The total word count of the website is '.$chars.', finished reading "The Adventures of Tom Sawyer" by Mark Twain!';}
    elseif ($chars<190000 && $chars>180000){
    echo 'The total word count of the website is '.$chars.', finished reading "Border Town" by Shen Congwen!';}
    elseif ($chars<200000 && $chars>190000){
    echo 'The total word count of the website is '.$chars.', finished reading "Education of Love" by Yami Chisaki!';}
    elseif ($chars<210000 && $chars>200000){
    echo 'The total word count of the website is '.$chars.', finished reading "Cold Night" by Ba Jin!';}
    elseif ($chars<220000 && $chars>210000){
    echo 'The total word count of the website is '.$chars.', finished reading "The Convenience Store of Solace" by Keigo Higashino!';}
    elseif ($chars<230000 && $chars>220000){
    echo 'The total word count of the website is '.$chars.', finished reading "A Life" by Guy de Maupassant!';}
    elseif ($chars<250000 && $chars>230000){
    echo 'The total word count of the website is '.$chars.', finished reading "Pride and Prejudice" by Jane Austen!';}
    elseif ($chars<280000 && $chars>250000){
    echo 'The total word count of the website is '.$chars.', finished reading "Fortress Besieged" by Qian Zhongshu!';}
    elseif ($chars<300000 && $chars>280000){
    echo 'The total word count of the website is '.$chars.', finished reading "Ancient Ship" by Zhang Wei!';}
    elseif ($chars<310000 && $chars>300000){
    echo 'The total word count of the website is '.$chars.', finished reading "Midnight" by Mao Dun!';}
    elseif ($chars<320000 && $chars>310000){
    echo 'The total word count of the website is '.$chars.', finished reading "Dust Settles" by Alai!';}
    elseif ($chars<340000 && $chars>320000){
    echo 'The total word count of the website is '.$chars.', finished reading "Wuthering Heights" by Emily Brontë!';}
    elseif ($chars<350000 && $chars>340000){
    echo 'The total word count of the website is '.$chars.', finished reading "The Hunchback of Notre-Dame" by Victor Hugo!';}
    elseif ($chars<400000 && $chars>350000){
    echo 'The total word count of the website is '.$chars.', finished reading "Journey Under the Midnight Sun" by Keigo Higashino!';}
    elseif ($chars<1000000 && $chars>400000){
    echo 'The total word count of the website is '.$chars.', finished reading the four famous classics of our country!';}
    elseif ($chars>1000000){
    echo 'The total word count of the website is '.$chars.', finished reading "War and Peace" by Leo Tolstoy!';}
} 

Add the above code to the function.php file in the theme file, and add the code where it needs to be called.

<?php echo allwords(); ?>

I currently put it in the footer.php file and it is displayed at the end of the page. The above code can be freely modified according to the actual situation, such as word count, book titles, etc., and more word count ranges and book titles can be added. This is the process of me using ChatGPT. The effect is shown in the following figure.

20230417085513.png

You can visit my blog (青山绿水) and scroll to the bottom to view it.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.