PlanMx

Develop your online brand | PlanMX Marketing Web

Creating a Packed Addons- Plugin for Word - Press.

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 - Logic

We 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 - Types

This 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.
In this article, we will not say too detailed and clear. But we will show and explain how to pack all those constituents into an addons plugin.

OK! Let’s Begin

Creating 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:

(Link: https://make.wordpress.org/core/handbook/best-practices/inline-documentation-standards/php/#docblock-formatting)

  1. /*
  2. Plugin Name: Your plugin name
  3. Plugin URI: your plugin url
  4. Text Domain: text-domain
  5. Domain Path: /languages/
  6. Description: Short description of the plugin
  7. Author: Author name
  8. Version: 1.0.0
  9. Author URI: author uri
  10. /
And here is the detail that we added:
  • 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 Configuration

In the main plugin file, paste this snippet after the header comment:
  1. if ( ! defined( 'ABSPATH' ) )
  2. exit; // Exit if accessed directly
Because of security, it blocks access to the plugin file directly.

After that, you can add this code:

  1. function your_addons_load_plugin_textdomain()
  2. load_plugin_textdomain( 'ninzio-addons', false, dirname( plugin_basename(FILE) ) . '/languages/' );
  3. add_action( 'plugins_loaded', 'your_addons_load_plugin_textdomain' );
In order to ensure that the function name is exact, we download our plugin text - domain. We advised you that use self-descriptive names with a prefix of your plugin. For instance of ninzio - addons (we can use underscores here because this is a .php function)

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 - Path

Let’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 - Panel

In 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:

  1. if ( ! class_exists( 'ReduxFramework' ) &&file_exists( your_addons . '/optionpanel/framework.php' ) )
  2. require_once('optionpanel/framework.php' );
  3. if ( ! isset( $redux_demo ) &&file_exists( your_addons . '/optionpanel/config.php' ) )
  4. require_once('optionpanel/config.php' );
We required 2 main files of redux - framework in this code, there are:
  • The “framework.php”: it manages the option - panel - function
  • The “config.php file”: it is responsible for configuring the option - panel.
Our option - panel files are placed in an “option - panel” folder which is located into the ninzio-addons- plugin folder. Done.

About The Custom - Functions

Let’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:

functionyour_addons_profile_social_links ()......

Then, requiring your custom - functions file:

require_once('includes/addons-functions.php' );

About The Custom - Widgets

Next, 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”.

require_once('widgets/ninzio-recent-tweets.php');

require_once('widgets/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

require_once('ninzio-projects/ninzio-projects.php');

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:

  1. single-projects.php
  2. archive-projects.php
  3. taxonomy-projects.php
Let’s add three functions to your main - addons file, like this:
  1. function your_addons_projects_single_template($single_template)
  2. global $post;
  3. if ($post->post_type == 'projects')
  4. if ( $theme_file = locate_template( array ( 'single-projects.php' ) ) )
  5. $single_template = $theme_file;
  6. else
  7. $single_template = your_addons . 'projects/single-projects.php';
  8. return $single_template;
  9. add_filter( "single_template", "your_addons_projects_single_template", 20 );
And this:
  1. function your_addons_projects_archive_template($archive_template)
  2. global $post;
  3. if ($post->post_type == 'projects')
  4. if ( $theme_file = locate_template( array ( 'archive-projects.php' ) ) )
  5. $archive_template = $theme_file;
  6. else
  7. $archive_template = your_addons . 'projects/archive-projects.php';
 
  1. return $archive_template;
  2. add_filter( "archive_template", "your_addons_projects_archive_template", 20 )
And this:
  1. function your_addons_projects_taxonomy_template($taxonomy_template)
  2. if (is_tax('projects-category'))
  3. if ( $theme_file = locate_template( array ( 'taxonomy-projects.php' ) ) )
  4. $taxonomy_template = $theme_file;
  5. else
  6. $taxonomy_template = your_addons . 'projects/taxonomy-projects.php';
  7. return $taxonomy_template;
  8. add_filter( "taxonomy_template", "your_addons_projects_taxonomy_template", 20 );
The important logic is to load the sample file of the custom post kind after testing if a copy of it exists in the threads directory. When your customers copy a custom post into a theme and expand or overwrite it, your addons- plugin will load the custom version of your client’s file. Therefore, in this case,your core files which into the addon- plugin are not change, they are merely expanded by your customer.

About The Custom - Scripts, “Shortcodes” and Styles

You 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:

require_once('shortcodes/ninzio-shortcodes.php' );

About The Languages

It 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:

  1. Start upPoedit
  2. From POT/PO file go to File > New
  3. From the languages folder, select and Open the pot file
  4. Type your name, email address, your language and country (example:. French fr_FR, German de_DE).
  5. Click the Update button in the main Poedit UI.
  6. Save the file, for your language and country, such as “filename-xx_XX.po” with “xx_XX”
That’s all. Your plugin is ready and done no

Also, I would like to highlight the What is Whitespace and why is it important? 8 website to inspire your web design.

Web Design