Last active
April 7, 2023 21:05
-
-
Save jasontucker/a4b5ecbb02e16748bd02ca504469851d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Plugin Title Truncator | |
Plugin URI: https://yourwebsite.com/plugin-title-truncator | |
Description: This plugin truncates plugin titles in the WordPress plugin directory to a maximum of 4 words. | |
Version: 1.0 | |
Author: Your Name | |
Author URI: https://yourwebsite.com | |
License: GPLv2 or later | |
Text Domain: plugin-title-truncator | |
*/ | |
/* | |
Prompt: Write a WordPress plugin that manipulates the results of the WordPress plugin directory that truncates the title of the plugin returned and limits it to 4 words | |
To create a WordPress plugin that manipulates the results of the WordPress plugin directory by truncating the titles of the returned plugins and limiting them to 4 words, follow these steps: | |
Create a new folder in your WordPress installation's wp-content/plugins directory. Name the folder something unique like plugin-title-truncator. | |
In the newly created folder, create a new PHP file named plugin-title-truncator.php. This will be the main plugin file. | |
Open the plugin-title-truncator.php file in a text editor and add the following code to define your plugin | |
Add the following function to plugin-title-truncator.php. This function truncates a given string to a specified number of words: | |
*/ | |
function truncate_title_to_n_words($title, $n_words = 4) { | |
$words = explode(' ', $title); | |
$truncated_title = implode(' ', array_slice($words, 0, $n_words)); | |
if (count($words) > $n_words) { | |
$truncated_title .= '...'; | |
} | |
return $truncated_title; | |
} | |
/* | |
To modify the plugin titles in the WordPress plugin directory, we will use the plugin_row_meta filter. Add the following code to the plugin-title-truncator.php file: | |
*/ | |
function truncate_plugin_titles_in_directory($plugin_meta, $plugin_file, $plugin_data, $status) { | |
$new_title = truncate_title_to_n_words($plugin_data['Name']); | |
if ($new_title !== $plugin_data['Name']) { | |
$plugin_meta[0] = preg_replace( | |
'/' . preg_quote($plugin_data['Name'], '/') . '/', | |
$new_title, | |
$plugin_meta[0] | |
); | |
} | |
return $plugin_meta; | |
} | |
add_filter('plugin_row_meta', 'truncate_plugin_titles_in_directory', 10, 4); | |
/* | |
Save the changes to the plugin-title-truncator.php file. | |
Go to the WordPress admin dashboard, navigate to the Plugins page, and activate the "Plugin Title Truncator" plugin. | |
Now, when you browse the WordPress plugin directory, the plugin titles should be truncated to a maximum of 4 words. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment