Site icon Experience Wordpress Developer Online – Design Develop and Maintenance Support

How to Create Custom Plugin in WordPress

A WordPress plugin can consist of as little as a single PHP file however for consistency I always create folder and at least the single PHP file within it. The folder and the file should have the same name with the exception of the file extension.

To Create Custom Plugin, all you need to do is create a folder and then create a single file with one line of content. Navigate to the wp-content/plugins folder, and create a new folder named music-reviews. Inside this new folder, create a file named music-reviews.php. Open the file in a text editor, and paste the following code in it:


When we’re done we’re going to upload this newly created folder with the file inside it to our wp-content/plugins directory on our server.

With that out of the way, you can go into the back end to activate your plugin. That’s all there is to it! Of course, this plugin doesn’t do anything; but strictly speaking, it is an active, functioning plugin.

With our file all set up and plugin active. we can now add the inner workings of the plugin.

Add the code below in your wp-content/plugins/music-reviews/music-reviews.php file:

function register_music_review() { 
register_post_type( 'music_review', array( 'labels' => array(
						'name' => 'Music Reviews',
                                                 'singular_name' => 'music'
						   ),
					'public'      => true,
				        'has_archive' => true,
				        'rewrite'     => array('slug' => 'music','with_front' => false),
					'supports'    => array('title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', 'page-attributes', 'post-formats'),
					'can_export'  => true,
									)
					);
register_taxonomy('music_review_cat', 'music_review', array('hierarchical' => true, 'label' => 'Categories', 'query_var' => true, 'rewrite' =>array( 'slug' => 'music-review-categories' )));
register_taxonomy('music_review_tag', 'music_review', array('hierarchical' => false, 'label' => 'Tags', 'query_var' => true, 'rewrite' => true));
}
add_action( 'init', 'register_music_review' );

It's custom post type code.In this case our custom post type is called music-review.The code is essentially telling WordPress to establish a new type of post within your theme. The post type has parameters that go along with it such as labels, arguments, and more.

I won’t go into that of detail on how to create custom post types in wordpress because I’ve already covered it within another article. Be sure to read it to gain a full understanding.

With our post type set up you can already see it active within the WordPress admin area.

Our custom post type is successfully implemented.

If you’ve made it this far you can now visit your WordPress admin area and see your new custom post type and taxonomy(categories and tag) present. Below I’ll try adding a new music review. You should see the new categories and tag section that we set up with our custom Taxonomy. With some data entered I’ll publish the post.

At this point we have our functionality set up within our plugin.Getting Our Code To Output.The page will be referenced by our plugin.


Exit mobile version