This article introduces a generator that creates WordPress's `add_shortcode` from CSV files. This tool should be helpful for efficiently generating WordPress shortcodes.
How to Use
First, clone the repository for this generator using the following command.
git clone https://github.com/yuis-ice/wp_shortcode_genelater.git
Then, refer to the README for detailed usage instructions.
Generator Code
The code for this generator is written in Ruby and generates WordPress’s add_shortcode from CSV files. Below is a summary of the code.
require 'csv'
@csvFilePath = File.expand_path('../../src.csv', __FILE__)
@functions = ''
CSV.foreach(@csvFilePath, headers: true) do |csvText|
# Read and format data from CSV file
# Format each column: ASP, CATEGORY, REMARKS, PRODUCT, TAG, AD
if csvText['ASP'] then csvText['ASP'].gsub!(/\n/, '') end
if csvText['CATEGORY'] then csvText['CATEGORY'].gsub!(/\n/, '') end
if csvText['REMARKS'] then csvText['REMARKS'].gsub!(/\n/, '') end
if csvText['PRODUCT'] then csvText['PRODUCT'].gsub!(/\n/, '') end
if csvText['TAG'] then csvText['TAG'].gsub!(/\n/, '') end
if csvText['AD'] then csvText['AD'].gsub!(/\n/, '') end
# Generate shortcode
@functions += "
// ASP: #{csvText['ASP']}
// CATEGORY: #{csvText['CATEGORY']}
// REMARKS: #{csvText['REMARKS']}
// PRODUCT: #{csvText['PRODUCT']}
function #{csvText['TAG']}(){
return '#{csvText['AD']}' ;
}
add_shortcode('#{csvText['TAG']}', '#{csvText['TAG']}') ;
"
end
# Display and save generated code to file
puts @functions
@resultFilePath = File.expand_path('../../output/result.txt', __FILE__)
File.open(@resultFilePath, 'w') { |file|
file.puts @functions
}
This generator reads data from a CSV file and generates WordPress’s add_shortcode functions based on that data. It generates shortcodes based on each row’s data and displays them with comments.
By using this tool, you can efficiently generate large numbers of WordPress shortcodes. For detailed usage instructions, refer to the README. This should be a useful tool for WordPress developers.
Summary
In summary, the WordPress add_shortcode generator is a very useful tool for generating shortcodes easily and quickly from CSV files. By using this generator, managing large-scale projects and websites becomes easier, saving time and energy.
WordPress shortcodes are an important means of extending and customizing content, and are indispensable elements in plugin and theme development. However, manually generating many shortcodes is very tedious and time-consuming.
By using this generator, you can generate a huge number of shortcodes just by filling in data in a CSV file and running a script. This enables an efficient development process and minimizes the possibility of errors.