Friday, June 8, 2018

Wordpress: How to create plugin with admin menu options inputs forms

h1.  Step 1

Create plugin folder and add blank php file

h1. Step 2

Add the name and description of the plugin at the beginning of the code
/*

Plugin Name: Fusion Core on Custom Post Types

Description: Enables the ThemeFusion Core page builder on Custom Post Types

*/

h1. Step 3

Here's an example of the full code. *Please read the code description for details*
/*

Plugin Name: Fusion Core on Custom Post Types

Description: Enables the ThemeFusion Core page builder on Custom Post Types

*/


/** WP: Generating plugin menu */


if ( is_admin() ){ // admin actions

  add_action( 'admin_menu', 'fusion_on_cpt' );

  add_action( 'admin_init', 'register_mysettings' );

} else {

  // non-admin enqueues, actions, and filters

}


// Register fields

function register_mysettings() {

  register_setting( 'cpt-group', 'cpt_name' );

}


//Generate options page (to be displayed under settings)

function fusion_on_cpt() {

    add_options_page( 'Fusion on CPT', 'Fusion Core on Custom Post Types', 'manage_options', 'fusion-core-for-cpt', 'fusion_on_cpt_options' );

}


//Generate content for the options page

function fusion_on_cpt_options() {

    if ( !current_user_can( 'manage_options' ) )  {

        wp_die( __( 'You do not have sufficient permissions to access this page.' ) );

    }

    echo '
'; echo '

Fusion on cpt options

'; echo 'Please add the names of each custom post type separated by a comma (",")'; echo '
'; echo ''; settings_fields('cpt-group'); do_settings_sections('cpt-group'); submit_button(); echo '
'; } /** Mods/overrides for fusion builder presets */ add_action ( 'plugins_loaded', function () { if ( is_admin() ) { //call fusion builder classes require_once( WP_PLUGIN_DIR . '/fusion-core/admin/class-pagebuilder.php' ); require_once( WP_PLUGIN_DIR . '/fusion-core/admin/page-builder/classes/class-ui.php' ); //call fusion builder functions $instance = Fusion_Core_PageBuilder::get_instance(); $ui = Fusion_Core_PageBuilder_UI::get_instance(); //get input data by calling input name $a = get_option('cpt_name'); //assign input data into variable $variable_string = (isset($a)) ? $a : 'position'; //clean input data and create array for each $custom_post_types = explode(",", $variable_string); //inform fusion builder about content types to be altered foreach($custom_post_types as $allowed_cpt) { $instance->allowed_post_types[] = $allowed_cpt; $ui->settings['allowed_post_types'][] = $allowed_cpt; } } }, 9); // priority 9 to be called before the line of fusion-core.php
Plugin has been attached to this entry.

No comments:

Post a Comment