I have a website “https://talesofss.com/”, hereafter referred to as “my website”.
I want to create a random word generator, so that when a user comes to my website they can click on the button to create a random word, and then that random word would be searched in the website. This would lead to the relevant blogs being shown in the search. For this, the random word generator would have to be a keyword from my website.
How should I go about it?
1. Generate Random Article using Python - Web Scraping, NLP
Create a Python program for web scrapping, NLP, store the keyword and random word generator
Note: Jio has blocked GitHub raw links. So the
nltk.download('punkt') commands used to download the datasets/tokenizers may not work.
stopwords: This dataset provides a list of common words (like “and”, “the”, “is”, etc.) for various languages. In the program, we use the English stopwords to filter out these common words from the extracted keywords.
punkt: This is a pre-trained tokenizer model for English. It’s used by the
word_tokenize function to split the text into individual words (tokens)
The file size for the
punkt tokenizer is approximately 13 MB and for the
stopwords dataset it often less than 1 MB.
So instead of changing DNS, using VPN or proxy, simply download it from your mobile data.
1. Python Web Crawler - Web Scrapping and NLP
To scrape the entire website, you’ll need to implement a web crawler that starts from the homepage and then follows links to other pages within the same domain.
Here’s a step-by-step approach to achieve this:
- Start with the homepage: Fetch the content of the homepage.
- Extract all internal links: Identify all links that lead to other pages within the same domain.
- Visit each link: For each extracted link, fetch the content of that page.
- Repeat the process: Continue the process for each new page, ensuring not to revisit already scraped pages.
But the the program was scrapping the whole website and not just the titles of the posts, you’ll need to modify the web scraping function to specifically target the HTML tags that contain the titles.
Typically, titles of pages or posts are contained within
<h3>, etc., tags, or sometimes within
- Remember to exclude URLs containing
/tag/by adding a condition in the
- Use the
urlparsefunction from the
urllib.parsemodule to extract the domain of the current URL. If the domain contains “talesofss.com”, the function will proceed to extract the titles from the
<h3>tags. Otherwise, it will skip the title extraction for that URL.
This ensures that titles are only extracted from URLs that belong to your website.
Also, you would want to start with a fresh database each time you run the program, otherwise, every time you run the program, the keywords extracted from the titles are added to the SQLite database. The database itself is not recreated from scratch, so the keywords from previous runs will still be present in the database.
- To ensure a completely fresh start every time you run the program, you can drop the table and recreate it at the beginning of each run.
- By using the
setdata structure, you ensure that only unique keywords and titles are passed to the database functions.
- And by using the
INSERT OR IGNOREcommand in SQLite along with the
UNIQUEconstraint, you ensure that the database will ignore any duplicates that might still be attempted to be inserted. This combination ensures that no duplicates are stored in the database.
- Finally, create a function
get_random_title()that fetches a random title from the
titlestable in the SQLite database
2. Integrate Python Program with Website
After creating this Python program, the question of the hour is how do I integrate this with my website? I want to open the home page and create a button where the user will click on “random article” Once the user clicks on random article, the code runs and it selects 1 random title. Then the title is searched on the website.
Since the website is built on WordPress, here’s a step-by-step guide to implement this:
Integrating custom Python scripts into a WordPress website is a bit more involved than with frameworks like Flask or Django. WordPress is primarily PHP-based, so you’ll need to find a way to execute your Python script from within PHP. Here’s a step-by-step guide to help you:
Host Your Python Script on a Server:
- One of the cleanest ways to do this is by setting up a Flask or Django microservice that runs your Python script. This service can be hosted on the same server as your WordPress site or a different one.
- This microservice will expose an endpoint (e.g.,
/random-article) that, when hit, will run the Python script and return the random title.
Call the Python Microservice from WordPress:
- You can use WordPress’s built-in
wp_remote_getfunction to make a GET request to your Python microservice.
- Once you get the response (i.e., the random title), you can then use it as needed within WordPress.
- You can use WordPress’s built-in
Integrate with WordPress:
- Create a custom page template or modify an existing one in your WordPress theme.
- Add a button labeled “Random Article”.
- In your theme’s
functions.phpfile, set up the custom AJAX action to call the Python microservice using
wp_remote_getand then search for the article in WordPress using WP_Query or direct SQL queries.
2. Create a WordPress Plugin to Search Random Article
Implementing a “Random Article” feature like Wikipedia’s “Special:Random” for your website “talesofss.com” can be a great way to engage users and introduce them to different content on your site.
1. Create a Custom WordPress Plugin:
Step 1.1: Navigate to your WordPress plugins directory, usually located at
Step 1.2: Create a new directory for your plugin, e.g.,
Step 1.3: Inside this directory, create a PHP file, e.g.,
Step 1.4: Add the following basic plugin information at the top of the file:
2. Implement the Random Article Redirect
Step 2.1: In the same
random-article.php file, add the following code:
3. Create a Shortcode to Place the Button Anywhere:
Step 3.1: Still in the
random-article.php file, add the following code: This creates a shortcode
[random_article_button] that you can place anywhere on your site to display the “Random Article” button.
4. Trigger the Redirect:
Step 4.1: Add the following code to
random-article.php: This checks if the
random query parameter is set in the URL and triggers the redirect to a random post.
5. Activate the Plugin:
Step 5.1: Log in to your WordPress admin dashboard.
Step 5.2: Navigate to
Plugins > Installed Plugins.
Step 5.3: Find “Random Article Generator” in the list and activate it.
6. Add the Button to Your Homepage:
Step 6.1: Edit the page or post where you want the “Random Article” button to appear.
Step 6.2: Add the shortcode
[random_article_button] where you want the button.
Step 6.3: Update or publish the page.
Now, when users visit your site and click the “Random Article” button, they’ll be redirected to a random post on your website.
3. Random Article Button - Edit WordPress Theme - PHP, Java, CSS
Creating a simple button that leads to a random article is a straightforward solution and can be implemented without the need for a staging site. Here’s a general approach to achieve this in WordPress:
Random Article Selection Logic:
- You’ll need a mechanism to randomly select an article from your WordPress posts. This can be achieved with a simple SQL query that fetches a random post.
- You can add a button to your homepage (or any other page) using the WordPress editor. This button’s action will be to redirect the user to the randomly selected article.
- You can use a WordPress shortcode to embed the functionality directly into a post or page. When the shortcode is processed, it will generate the button and handle the redirection logic.
1. Shortcode for the Random Article Button - PHP
Creating a shortcode in WordPress is a straightforward process. Here’s a step-by-step guide to creating a shortcode for the random article button:
1. Access Your WordPress Files:
You can edit your WordPress files in two ways:
Using the WordPress Dashboard: Navigate to
Theme Editor. This will allow you to edit your theme’s files directly from the dashboard.
Using FTP: Connect to your website using an FTP client like FileZilla. Navigate to the
2. Edit the
Once you’ve accessed your theme’s files, you’ll want to edit the
functions.php file. This file contains functions and definitions used by your theme.
3. Add the Shortcode Function:
Paste the following code at the end of your
4. Save the File:
If you’re using the WordPress dashboard, click the “Update File” button.
If you’re using FTP, save the file on your local machine and then upload it to the server, overwriting the existing
5. Use the Shortcode:
Now, you can use the
READ A RANDOM ARTICLE shortcode in any post, page, or widget to display the “Random Article” button. Simply paste the shortcode where you want the button to appear.
2. Style the "Random Article" Button - CSS
To style the “Random Article” button, you’ll need to add some CSS to your theme. Here’s how you can do it:
Modify the Shortcode Function: First, let’s add a specific class to the button in your shortcode function so that we can target it with CSS. Modify the return line in your shortcode function to include a class, say
Add Custom CSS:
- Go to your WordPress dashboard.
- Navigate to
- In the Customizer, look for the
Additional CSSsection and click on it.
- Add the following CSS to style the button:
- After adding the CSS, click the “Publish” button in the Customizer to save your changes.
Check Your Site:
- Visit the post where you added the shortcode and ensure the “Random Article” button is styled as desired.
If the “Random Article” button has a fixed link, it means the PHP code for generating a random post is not being executed each time the button is clicked. Instead, it’s generating a link once and then using that same link every time.
To ensure that the button fetches a new random article every time it’s clicked, you need to make sure the PHP code is executed dynamically each time the button is rendered.
Here’s a step-by-step approach to ensure the button works dynamically:
Shortcode Functionality: Ensure that the shortcode function you added to your theme’s
functions.phpfile is correct. The function should query the database for a random post each time it’s called.
Caching: WordPress sites often use caching plugins to speed up their performance. If you have a caching plugin installed, it might be caching the result of the shortcode, causing the same link to appear every time. You might need to clear the cache or configure the plugin to exclude the page with the random article button from being cached.
Theme Customizer: If you added the shortcode directly to a theme file or widget, ensure that it’s being executed dynamically. If you added it to a static text widget or a similar static area, it might not be re-evaluated on each page load.
- Setting up a REST API endpoint in WordPress that returns a random post URL.
Here’s a step-by-step guide to implement this method:
1. Setting up a REST API Endpoint:
Add the following code to your theme’s
functions.php file to register a new REST API endpoint:
This code creates a new REST API endpoint at
/wp-json/myplugin/v1/random-post that returns a random post URL.
2. Modifying the Shortcode:
random_post_button_shortcode function in the
3. Add the Shortcode to Your Page:
Now, you can add the
READ A RANDOM ARTICLE shortcode to any post or page where you want the “Random Article” button to appear.
4. Style the Button (Optional):
If you’ve previously added CSS styles for the button, they should still apply. If not, you can add the following CSS to your theme’s customizer or a custom CSS plugin to style the button:
Note: If the REST API endpoint is consistently returning the same URL, it suggests that the issue might be with the server-side code that generates the random post URL. Let’s troubleshoot this:
Database Caching: Some hosting providers or WordPress plugins implement database caching. This means that the result of a database query (like fetching a random post) might be cached for a certain period. If you have a caching plugin installed, try disabling it temporarily to see if that affects the behavior.