Ok, so I couldn't find a plugin to do what I wanted, so I set out to create my own. I've done PHP coding before, though it's been some time. Grabbed a bunch of code of the internet to modify and do what I want, threw it all together in a single class.
The goal at the moment is to have a new post type, with custom fields, and be able to edit those fields in the admin site. What I have instead is a new post type, with a bunch of boxes with the right label, but no form being shown.
This may just be needing a second set of eyes on it, but I don't see what's wrong here. The add_meta_box
calls seem to be working, they're just not echoing anything in their callback functions. What am I missing? Any help is greatly appreciated.
If needed, whole repo is https://github.com/steev42/WrestleEFedManager ; relevant file is copied below.
<?php
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) exit;
if ( ! class_exists( 'Wrestleefedmanager_Federation' ) ) :
class Wrestleefedmanager_Federation {
/**
* Constructor
*/
public function __construct() {
// Hooking up our function to theme setup
add_action( 'init', array( $this, 'create_federation_post_type' ) );
add_action( 'add_meta_boxes', array( $this, 'initialize_federation_post_type') );
add_action( 'save_post', array( $this, 'save_fed') );
}
// Our custom post type function
function create_federation_post_type() {
$fedlabels = array(
'name' => _x( 'Federations', 'Post Type General Name'),
'singular_name' => _x( 'Federation', 'Post Type Singular Name'),
'menu_name' => __( 'Federations'),
'parent_item_colon' => __( 'Parent Fed'),
'all_items' => __( 'All Feds'),
'view_item' => __( 'View Fed'),
'add_new_item' => __( 'Add New Fed'),
'add_new' => __( 'Add New'),
'edit_item' => __( 'Edit Fed'),
'update_item' => __( 'Update Fed'),
'search_items' => __( 'Search Feds'),
'not_found' => __( 'Not Found'),
'not_found_in_trash' => __( 'Not found in Trash'),
);
$fedargs = array(
'label' => __( 'feds' ),
'description' => __( 'Federations' ),
'labels' => $fedlabels,
// Features this CPT supports in Post Editor
'supports' => array( 'title', 'editor', 'author', ),
// You can associate this CPT with a taxonomy or custom taxonomy.
//'taxonomies' => array( 'genres' ),
/* A hierarchical CPT is like Pages and can have
* Parent and child items. A non-hierarchical CPT
* is like Posts.
*/
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
);
register_post_type( 'feds', $fedargs);
}
function initialize_federation_post_type() {
/*
add_meta_box( string $id, string $title, callable $callback, string|array|WP_Screen $screen = null,
string $context = 'advanced', string $priority = 'default', array $callback_args = null )*/
add_meta_box("abbreviation", "Abbreviation", "fed_abbr", "feds", "normal", "low");
add_meta_box("founded", "Founded", "fed_founded", "feds", "side", "low");
add_meta_box("closed", "Closed", "fed_closed", "feds", "side", "low");
add_meta_box("parentfed", "Parent Federation", "fed_parent", "feds", "normal", "low");
add_meta_box("logo", "Logo", "fed_logo", "feds", "normal", "low");
add_meta_box("owner", "Owner", "fed_owner", "feds", "side", "low");
}
function fed_abbr(){
global $post;
$custom = get_post_custom($post->ID);
$abbr = $custom["abbreviation"][0];
?>
<label>Abbr:</label>
<input name="fed_abbreviation" type="text" value="<?php echo $abbr; ?>" />
<?php
}
function fed_founded(){
global $post;
$custom = get_post_custom($post->ID);
$founded = $custom["founded"][0];
?>
<label>Founded:</label>
<input name="fed_founddate" value="<?php echo $founded; ?>" />
<?php
}
function fed_closed(){
global $post;
$custom = get_post_custom($post->ID);
$closed = $custom["closed"][0];
?>
<label>Closed:</label>
<input name="fed_closedate" value="<?php echo $closed; ?>" />
<?php
}
function fed_parent(){
}
function fed_logo(){
}
function fed_owner(){
global $post;
$custom = get_post_custom($post->ID);
$owner = $custom["owner"][0];
?>
<label>Owner:</label>
<input name="fed_owner" value="<?php echo $owner; ?>" />
<?php
}
function save_fed(){
global $post;
update_post_meta($post->ID, "abbreviation", $_POST["fed_abbreviation"]);
update_post_meta($post->ID, "founded", $_POST["fed_founddate"]);
update_post_meta($post->ID, "closed", $_POST["fed_closedate"]);
update_post_meta($post->ID, "owner", $_POST["fed_owner"]);
}
}
endif;
Ok, so with a bit of trial and error and scouring the googles, I was able to make this work.
First off, because this in a class, the callbacks in the add_meta_box need the array ($this, <cb>)
format rather than just the original string.
Secondly, instead of using the add_meta_boxes
action callback, the args for register_post_type
have their own option, which I used thusly: 'register_meta_box_cb' => array( $this, 'initialize_federation_post_type')
So, after getting that to work, and adding in a bit of code to get the fields to display in view mode, the file looks like this:
<?php
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) exit;
if ( ! class_exists( 'Wrestleefedmanager_Federation' ) ) :
class Wrestleefedmanager_Federation {
/**
* Constructor
*/
public function __construct() {
// Hooking up our function to theme setup
add_action( 'init', array( $this, 'create_federation_post_type' ) );
//add_action( 'add_meta_boxes', array( $this, 'initialize_federation_post_type') );
add_action( 'save_post', array( $this, 'save_fed') );
add_filter ('template_include', array($this, 'display_federation_template' ) );
}
// Our custom post type function
function create_federation_post_type() {
$fedlabels = array(
'name' => _x( 'Federations', 'Post Type General Name'),
'singular_name' => _x( 'Federation', 'Post Type Singular Name'),
'menu_name' => __( 'Federations'),
'parent_item_colon' => __( 'Parent Fed'),
'all_items' => __( 'All Feds'),
'view_item' => __( 'View Fed'),
'add_new_item' => __( 'Add New Fed'),
'add_new' => __( 'Add New'),
'edit_item' => __( 'Edit Fed'),
'update_item' => __( 'Update Fed'),
'search_items' => __( 'Search Feds'),
'not_found' => __( 'Not Found'),
'not_found_in_trash' => __( 'Not found in Trash'),
);
$fedargs = array(
'label' => __( 'Federations' ),
'description' => __( 'Wrestling Companies' ),
'labels' => $fedlabels,
// Features this CPT supports in Post Editor
'supports' => array( 'title', 'editor', 'author', ),
// You can associate this CPT with a taxonomy or custom taxonomy.
//'taxonomies' => array( 'genres' ),
/* A hierarchical CPT is like Pages and can have
* Parent and child items. A non-hierarchical CPT
* is like Posts.
*/
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 17,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
'register_meta_box_cb' => array( $this, 'initialize_federation_post_type'),
);
register_post_type( 'feds', $fedargs);
}
function initialize_federation_post_type() {
/*
add_meta_box( string $id, string $title, callable $callback, string|array|WP_Screen $screen = null,
string $context = 'advanced', string $priority = 'default', array $callback_args = null )*/
add_meta_box("abbreviation", "Abbreviation", array( $this, 'fed_abbr'), "feds", "normal", "low");
add_meta_box("founded", "Founded", array( $this, 'fed_founded'), "feds", "side", "low");
add_meta_box("closed", "Closed", array( $this, 'fed_closed'), "feds", "side", "low");
add_meta_box("parentfed", "Parent Federation", array( $this, 'fed_parent'), "feds", "normal", "low");
add_meta_box("logo", "Logo", array( $this, 'fed_logo'), "feds", "normal", "low");
add_meta_box("owner", "Owner", array( $this, 'fed_owner'), "feds", "side", "low");
}
function fed_abbr(){
global $post;
$custom = get_post_custom($post->ID);
$abbr = $custom["abbreviation"][0];
?>
<label>Abbr:</label>
<input name="fed_abbreviation" type="text" value="<?php echo $abbr; ?>" />
<?php
}
function fed_founded(){
global $post;
$custom = get_post_custom($post->ID);
$founded = $custom["founded"][0];
?>
<label>Founded:</label>
<input name="fed_founddate" value="<?php echo $founded; ?>" />
<?php
}
function fed_closed(){
global $post;
$custom = get_post_custom($post->ID);
$closed = $custom["closed"][0];
?>
<label>Closed:</label>
<input name="fed_closedate" value="<?php echo $closed; ?>" />
<?php
}
function fed_parent(){
}
function fed_logo(){
}
function fed_owner(){
global $post;
$custom = get_post_custom($post->ID);
$owner = $custom["owner"][0];
?>
<label>Owner:</label>
<input name="fed_owner" value="<?php echo $owner; ?>" />
<?php
}
function save_fed(){
global $post;
update_post_meta($post->ID, "abbreviation", $_POST["fed_abbreviation"]);
update_post_meta($post->ID, "founded", $_POST["fed_founddate"]);
update_post_meta($post->ID, "closed", $_POST["fed_closedate"]);
update_post_meta($post->ID, "owner", $_POST["fed_owner"]);
}
function display_federation_template ($template_path) {
if ( get_post_type() == 'feds' ) {
if ( is_single() ) {
// checks if the file exists in the theme first,
// otherwise serve the file from the plugin
if ( $theme_file = locate_template( array ( 'single-feds.php' ) ) ) {
$template_path = $theme_file;
} else {
$template_path = plugin_dir_path( __FILE__ ) . '/single-feds.php';
}
}
}
return $template_path;
}
}
endif;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.