[Tutorial] How to create an extension with a plugin
A particular file/folder structure:
[pluginName]
..[extensions][yourextension]/class-fw-extension-yourextension.php
..[extensions][yourextension]/options.php
..[languages]
yourPlugin.php
Here's your plugin's main file
<?php
/*
Plugin Name: yourPLUGIN
Plugin URI: http://codecanyon.net/item/YOURITEM
Description: Super awesome description.
Version: 1.0.0
Author: yourNAME
Author URI: http://yoursite.com
Text Domain: yourextension
Domain Path: /languages/
*/
//plugin language file
function yourextension_lang(){
load_plugin_textdomain('yourextension', false, basename( dirname( __FILE__ ) ) . '/languages');
}
add_action('admin_head','yourextension_lang');
function _filter_yourextension_extension($locations) {
$locations[dirname(__FILE__) . '/extensions'] = dirname( __FILE__ ) . 'extensions';
return $locations;
}
add_filter('fw_extensions_locations', '_filter_yourextension_extension');
Notice your plugin should have the extensions folder right in the root of the plugin, it doesn't work other way. This folder holds your extension in it's own folder yourextension, now, here's the extension class class-fw-extension-yourextension.php inside your extension's folder:
<?php if ( ! defined( 'FW' ) ) { die( 'Forbidden' ); }
class FW_Extension_Yourextension extends FW_Extension {
private $post_type = 'yourposttype';
private $slug = 'yourslug';
/**
* @internal
*/
public function _init() {
$this->define_slugs();
add_action( 'init', array( $this, '_action_register_post_type' ) );
if ( is_admin() ) {
$this->add_admin_actions();
$this->add_admin_filters();
}
}
private function define_slugs() {
$this->slug = apply_filters( 'fw_ext_yourextension_post_slug', $this->slug );
}
public function add_admin_actions() {
add_action( 'admin_menu', array( $this, '_action_admin_rename_posts' ) );
add_action( 'admin_enqueue_scripts', array( $this, '_action_admin_add_static' ) );
}
public function add_admin_filters() {
add_filter( 'fw_post_options', array( $this, '_filter_admin_add_post_options' ), 10, 2 );
}
/**
* @internal
*/
public function _action_admin_add_static() {
$listing_screen = array(
'only' => array(
array(
'post_type' => $this->post_type,
'base' => array( 'edit' )
)
)
);
$add_edit_screen = array(
'only' => array(
array(
'post_type' => $this->post_type,
'base' => 'post'
)
)
);
if ( fw_current_screen_match( $listing_screen ) ) {
wp_enqueue_style(
'fw-extension-' . $this->get_name() . '-listing',
plugin_dir_URL(__FILE__) . '/static/css/admin-listing.css',
array(),
fw()->manifest->get_version()
);
}
if ( fw_current_screen_match( $add_edit_screen ) ) {
wp_enqueue_style(
'fw-extension-' . $this->get_name() . '-add-edit',
plugin_dir_URL(__FILE__).'/static/css/admin-add-edit.css',
array(),
fw()->manifest->get_version()
);
wp_enqueue_script(
'fw-extension-' . $this->get_name() . '-add-edit',
plugin_dir_URL(__FILE__) . '/static/js/admin-add-edit.js',
array( 'jquery' ),
fw()->manifest->get_version(),
true
);
}
}
/**
* @internal
*/
public function _action_register_post_type() {
$post_names = apply_filters( 'fw_ext_sliders_post_type_name', array(
'singular' => __( 'YOUR POST TYPE SINGULAR', 'fw' ),
'plural' => __( 'YOUR POST TYPE PLURAL', 'fw' )
) );
register_post_type( $this->post_type, array(
'labels' => array(
'name' => $post_names['plural'], //__( 'Portfolio', 'fw' ),
'singular_name' => $post_names['singular'], //__( 'Portfolio project', 'fw' ),
'add_new' => __( 'Add New', 'fw' ),
'add_new_item' => sprintf( __( 'Add New %s', 'fw' ), $post_names['singular'] ),
'edit' => __( 'Edit', 'fw' ),
'edit_item' => sprintf( __( 'Edit %s', 'fw' ), $post_names['singular'] ),
'new_item' => sprintf( __( 'New %s', 'fw' ), $post_names['singular'] ),
'all_items' => sprintf( __( 'All %s', 'fw' ), $post_names['plural'] ),
'view' => sprintf( __( 'View %s', 'fw' ), $post_names['singular'] ),
'view_item' => sprintf( __( 'View %s', 'fw' ), $post_names['singular'] ),
'search_items' => sprintf( __( 'Search %s', 'fw' ), $post_names['plural'] ),
'not_found' => sprintf( __( 'No %s Found', 'fw' ), $post_names['plural'] ),
'not_found_in_trash' => sprintf( __( 'No %s Found In Trash', 'fw' ), $post_names['plural'] ),
'parent_item_colon' => '' /* text for parent types */
),
'description' => __( 'Create a POST TYPE SINGULAR', 'fw' ),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'publicly_queryable' => false,
/* queries can be performed on the front end */
'has_archive' => true,
'rewrite' => array(
'slug' => $this->slug
),
'menu_position' => 4,
'show_in_nav_menus' => false,
'menu_icon' => 'dashicons-media-code',
'hierarchical' => false,
'query_var' => true,
/* Sets the query_var key for this post type. Default: true - set to $post_type */
'supports' => array(
'title', /* Text input field to create a post title. */
// 'editor',
// 'thumbnail', /* Displays a box for featured image. */
),
'capabilities' => array(
'edit_post' => 'edit_pages',
'read_post' => 'edit_pages',
'delete_post' => 'edit_pages',
'edit_posts' => 'edit_pages',
'edit_others_posts' => 'edit_pages',
'publish_posts' => 'edit_pages',
'read_private_posts' => 'edit_pages',
'read' => 'edit_pages',
'delete_posts' => 'edit_pages',
'delete_private_posts' => 'edit_pages',
'delete_published_posts' => 'edit_pages',
'delete_others_posts' => 'edit_pages',
'edit_private_posts' => 'edit_pages',
'edit_published_posts' => 'edit_pages',
),
) );
}
/**
* @internal
*
* @param array $options
* @param string $post_type
*
* @return array
*/
public function _filter_admin_add_post_options( $options, $post_type ) {
if ( $post_type === $this->post_type ) {
$path = dirname(__FILE__).'/options.php';
$variables = fw_get_variables_from_file($path, array('options' => array()), $options );
$options[] = array( $variables['options'] );
}
return $options;
}
/**
* internal
*/
public function _action_admin_rename_posts() {
global $menu;
foreach ( $menu as $key => $menu_item ) {
if ( $menu_item[2] == 'edit.php?post_type=' . $this->post_type ) {
$menu[ $key ][0] = __( 'YOUR EXTENSION NAME', 'fw' );
}
}
}
/**
* @internal
*
* @return string
*/
public function _get_link() {
return self_admin_url('edit.php?post_type=' . $this->post_type);
}
public function get_settings() {
$response = array(
'post_type' => $this->post_type,
'slug' => $this->slug,
);
return $response;
}
public function get_post_type_name() {
return $this->post_type;
}
}
The issues generally come from
_action_admin_add_static, you need to link the static resources using Wordpress plugin linking;_filter_admin_add_post_optionsthis is where the you link your post type options, so for this example theoptions.phpin your extension folder, but you can create more of them, then update the _filter_admin_add_post_options function accordingly
Progress so far:
- your extension should be listed in the Unyson extensions list
- now you should be able to have a new
custom post typewith full back-end editing, listing - you should be able to edit options for your extension
To DO:
- front-end view
- some sample options
- a link to the page builder would be awesome, still have to dig into that
- perhaps some plugin zip with all sample code ready to
find->replace
I've also created a gist as suggested. Try and let me know how it works, please also jump in with any suggestions, findings and ways to improve the code. This code is a simplistic fork and adaptation of the Portfolio extension, feel free to check it our and adapt more features for your extension.
Good luck.
define( 'SP_DIR', plugin_dir_path(__FILE__) ) ;
define( 'SP_URL', plugin_dir_URL(__FILE__) );
Avoid constants, the code must be flexible to be able to test it in different environments. Initially Unyson also had a few constants, then we get criticized by some developers and we removed them.
P.S. Thank you for teaching the community!
P.P.S. You can create a gist https://gist.github.com/ and keep improving it (a gist is like a very simplified repository)
Thanks for the suggestion, I updated the example.
I would rather have a repository within Unyson so everyone can contribute.
Its will be great If unyson open a repository like EDD library
https://github.com/easydigitaldownloads/library
So everyone can contribute
Hi guys, I have a question.
from this documentation:
we know that we can create metabox options through a plugin.
But I don't understand this point:
options/ - A directory containing option files: post types, taxonomies or custom options. The framework will not automatically pick them (like theme options), only the extension decides how to use these options. You can access them through the $ext->get_[...]_options() methods. Users can overwrite in the theme any file from the options/ directory, by creating
How do we exactly load an option for post meta?
thank you.
@azharijelek Please tell more detailed what you want to do.
Hi @moldcraft , nevermind. I've found out the solution already. Thanks
Hi,
I tried to create an extension thru plugin using the steps above but after activating in WP Dashboard, I don't see it in Unyson Extension list nor has the post type as expected.
Well, it just needs to add the manifest.php file and it's good to go.