In this post, we will learn the way to keep especial function from presentation which is a WordPress theme the best development practice.
Theme – Development - LogicWe learnt many things which help us success in the market through selling WordPress themes on Theme- Forest for more than four years.
The production speed is the main benefit of separating development logic: creating quality themes is quicker, you can generate more income. Keeping function in a special plugin is helpful when it comes to updates. Suppose that: on Theme - Forest, you have 10 themes and you want to add new function to make an update, when you have all functions which are in a simple plugin, you just need to update once in the board.
Therefore, you should attend to Word - Press’ plugin and theme system if you want to create powerful themes and earn more money.
Functionality - TypesThis is a list of key constituents which we typically keep separate from theme files:
- Theme - options – panel.
- Page/Post extended (custom fields).
- Custom for widgets.
- Custom for short - codes.
- Custom for post types.
- Custom for extra functions.
OK! Let’s BeginCreating a new folder with the name of your packed plugin in your “wp-content > plugins”. You should use a self-explanatory name, for instance, our addons plugin has name “ninzio-addons” (“Ninzio” is the name of my company.)
Note: pay attention toavoid use an underscore in the folder name! If needed, let use a dash.
Next, you have to create a “.php” file with the same name like your folder in that plugin folder,such as in above example, there would be “ninzio-addons.php” (note: no underscore). Open that file and add Doc- Block header comment:
- Plugin Name: Your plugin name
- Plugin URI: your plugin url
- Text Domain: text-domain
- Domain Path: /languages/
- Description: Short description of the plugin
- Author: Author name
- Version: 1.0.0
- Author URI: author uri
- Plugin - Name: here you should the name which is short and descriptive.
- Plugin - URI: here you can paste your website address here.
- Text - Domain: this is an extremely important parameter. You should name it the like your plugin folder and main file. You and your users can translate plugin strings. After that we consider how to translate a packed addons plugin.
- Domain - Path: for plugin translation, this parameter is very important, too. It is relative to the language file folder. Go to your addons plugin folder and create an empty folder which is called “languages”. Let’s it empty and we will create the language file later.
- Besides, there are other parameters called “Author”, “Author URI” and “Version”, which are reasonably self-explanatory.
About The ConfigurationIn the main plugin file, paste this snippet after the header comment:
- if ( ! defined( 'ABSPATH' ) )
- exit; // Exit if accessed directly
After that, you can add this code:
- function your_addons_load_plugin_textdomain()
- load_plugin_textdomain( 'ninzio-addons', false, dirname( plugin_basename(FILE) ) . '/languages/' );
- add_action( 'plugins_loaded', 'your_addons_load_plugin_textdomain' );
Let’s ensure that you are right when copying or typing the “load_plugin_textdomain” function, for exact domain parameter that we defined earlier, and for the relative path parameter plugin, enter the path to the language folder that we made earlier. Please remember that.
About The Plugin - Directory - PathLet’s add this code in order to express your plugin directory path:
define( 'your_addons', plugin_dir_path( FILE ));
Your “addons” are used in order to make plugin - directory - path. Now, our plugin is created, let’s fill it with custom - function. OK?
About The Theme - Options - PanelIn here, you can create a custom - board, or you can use the theme - panel - frame on the cover, because we do not use this step in order to guide the way to create an option - panel for a theme.
Link about option - panel - frameworks (the articles of “BonangSalemane” on Redux - Framework theme integration: https://code.tutsplus.com/tutorials/getting-started-with-redux-framework-introducing-it-and-integrating-it-into-your-theme--cms-22240?_ga=2.158820281.2066407086.1508118696-1356864547.1505722856)
Besides, to add a theme - option - panel so that your addons copy the panel - options - folder to the plugin - addons- directory, we need to request some files to activate it like this:
- if ( ! class_exists( 'ReduxFramework' ) &&file_exists( your_addons . '/optionpanel/framework.php' ) )
- require_once('optionpanel/framework.php' );
- if ( ! isset( $redux_demo ) &&file_exists( your_addons . '/optionpanel/config.php' ) )
- require_once('optionpanel/config.php' );
- The “framework.php”: it manages the option - panel - function
- The “config.php file”: it is responsible for configuring the option - panel.
About The Custom - FunctionsLet’s create a file which is into your addons- plugin - directory and give a name, as: “addons-functons.php”. After that, give all your functions in this file.
You use the function - naming - convention, using the description - function name with a unique prefix, like this:
Then, requiring your custom - functions file:
About The Custom - WidgetsNext, to create a new folder which called “widgets” into addons- plugin folder, located all custom - widgets files into the file which you created, you should use prefixes and dashes instead of underscores.
For instance, we have custom Twitter - widget file with name “ninzio – recent-tweets.php”, similarly, our Mailchimp- widget is named “ninzio-mailchimp.php”.
You can check the post of BonangSalemane here: https://code.tutsplus.com/tutorials/creating-your-own-widgets-using-various-wordpress-apis-introduction--cms-22460?_ga=2.96243615.2066407086.1508118696-1356864547.1505722856
About The Custom - Post - Types and “Taxonomies”Now you should use custom post kinds if you add a portfolio, events or anything which is similar to regular post of Word - Press, but separated from the theme.
This is a useful post of “Tom McFarlin” that I think you should read: https://code.tutsplus.com/articles/quick-tip-post-types-taxonomies-and-permalinks--cms-20345?_ga=2.91004953.2066407086.1508118696-1356864547.1505722856
You also should make a special folder for custom post types,all your files which relate to your custom post types are put in this folder. The most important include the main files, unique post files and archive files. Giving a name your custom post type main file as you have named your custom post type folder, like “ninzio-projects.php”. Locate your custom post type code into that file, you need to have the main file in order to activate your custom post type
As you split function, you should consider about the customers, how they can extend or rewrite custom registry - template files. Suppose that we have our custom post type which called “projects”, the single post files will be named “single-projects.php” and the archive file will have name “archive-projects.php”.
You should create a separate file for your custom post kinds if they also has custom rules. We have 3 files now:
- function your_addons_projects_single_template($single_template)
- global $post;
- if ($post->post_type == 'projects')
- if ( $theme_file = locate_template( array ( 'single-projects.php' ) ) )
- $single_template = $theme_file;
- $single_template = your_addons . 'projects/single-projects.php';
- return $single_template;
- add_filter( "single_template", "your_addons_projects_single_template", 20 );
- function your_addons_projects_archive_template($archive_template)
- global $post;
- if ($post->post_type == 'projects')
- if ( $theme_file = locate_template( array ( 'archive-projects.php' ) ) )
- $archive_template = $theme_file;
- $archive_template = your_addons . 'projects/archive-projects.php';
- return $archive_template;
- add_filter( "archive_template", "your_addons_projects_archive_template", 20 )
- function your_addons_projects_taxonomy_template($taxonomy_template)
- if (is_tax('projects-category'))
- if ( $theme_file = locate_template( array ( 'taxonomy-projects.php' ) ) )
- $taxonomy_template = $theme_file;
- $taxonomy_template = your_addons . 'projects/taxonomy-projects.php';
- return $taxonomy_template;
- add_filter( "taxonomy_template", "your_addons_projects_taxonomy_template", 20 );
About The Custom - Scripts, “Shortcodes” and StylesYou should create a special folder and paste the files in your theme for custom styles and scripts. You must create include your code files within your folder - addons if you plan to add custom - shortcuts. Repeat our example, create a folder which is named “shortcodes” and create the file “yourprefix-shortcodes.php” inside that folder. You should set all your custom - shortcodes in the “-shortcodes.php” file.
Let read the Tutorial of “Siddharth” here: https://code.tutsplus.com/tutorials/wordpress-shortcodes-the-right-way--wp-17165?_ga=2.159229753.2066407086.1508118696-1356864547.1505722856
Our custom - shortcodes file:
About The LanguagesIt is time to make the language file in order to make your plugin able to translate, of course that after having checked all the custom - functions.
Go to the addons- plugin folder> languages and use the Po - Edit software in order to create language file. This is main language file which contains the strings within your text - domain. Other language files can be made from this main file.
The steps to translate your language file:
- Start upPoedit
- From POT/PO file go to File > New
- From the languages folder, select and Open the pot file
- Type your name, email address, your language and country (example:. French fr_FR, German de_DE).
- Click the Update button in the main Poedit UI.
- Save the file, for your language and country, such as “filename-xx_XX.po” with “xx_XX”
Also, I would like to highlight the What is Whitespace and why is it important? 8 website to inspire your web design.