Simon Pollard
Web developer
the code way
@smp303
function custom_post_charity() {
register_post_type( 'charity',
array(
'labels' => array(
'name' => 'Charities',
'singular_name' => 'Charity',
'all_items' => 'All Charities',
'add_new' => 'Add New',
'add_new_item' => 'Add New Charity',
'edit' => 'Edit',
'edit_item' => 'Edit Charity',
'new_item' => 'New Charity',
'view_item' => 'View Charity',
'search_items' => 'Search Charity',
'not_found' => 'Nothing found in the Database.',
'not_found_in_trash' => 'Nothing found in Trash',
'parent_item_colon' => ''
),
'public' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'show_ui' => true,
'query_var' => true,
'menu_position' => 8,
'menu_icon' => 'dashicons-heart',
'rewrite' => array( 'slug' => 'charity', 'with_front' => false ),
'has_archive' => 'charity',
'capability_type' => 'post',
'hierarchical' => false,
'supports' => array( 'title', 'editor', 'thumbnail', 'revisions'),
'show_in_rest' => true
)
);
}
add_action( 'init', 'custom_post_charity');
https://codex.wordpress.org/Post_Types
@smp303
@smp303
add_action('admin_init', 'velo_charity_init');
function velo_charity_init() {
add_meta_box("charity-meta", "Details", "charity_meta", "charity", "normal", "core");
}
Add a meta box to the post type
https://developer.wordpress.org/reference/functions/add_meta_box/
@smp303
function charity_meta($post) {
$custom = get_metadata('post',$post->ID);
$output = '<table class="form-table">';
$output .= '<tbody>';
$fields = array('entry_fee','min_target','website');
foreach($fields as $field) {
$data = isset($custom[$field][0]) ? $custom[$field][0] : null;
$output .= '<tr><td valign="top" style="width:15%"><label for="'.$field.'">'.ucfirst(str_replace('_', ' ', $field)).'</label></td>';
$output .= '<td><input value="'.$data.'" name="'.$field.'" id="'.$field.'" type="text" class="large-text" /></td></tr>';
}
$output .= '</tbody>';
$output .= '</table>';
echo $output;
$output = '';
}
Create a function to display a form
@smp303
Create a function to display a form
@smp303
add_action('save_post', 'custom_charity_save');
function custom_charity_save() {
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
global $post;
// Check that it is a charity
if(isset($post->post_type) && $post->post_type=="charity") {
// List of values
$values = array('entry_fee','min_target','website');
// Update the values
foreach ($values as $value) {
// Add/Update the custom meta data
$data = (isset($_POST[$value])) ? $_POST[$value] : null;
update_post_meta($post->ID, $value, $data);
}
}
}
Add an action to the save post hook
@smp303
$custom = get_metadata('post',$post->ID);
$fields = array('entry_fee','min_target','website');
foreach($fields as $field) {
$$field = isset($custom[$field][0]) ? $custom[$field][0] : null;
}
?>
<ul class="imggrid__list">
<?php if($entry_fee != '') { ?>
<li class="imggrid__fee">
<p class="imggrid__fee-title"><?= _e('Entry fee:','velo'); ?></p>
<p class="imggrid__fee-cost"><?= $entry_fee; ?></p>
</li>
<?php } ?>
<?php if($min_target != '') { ?>
<li class="imggrid__fund">
<p class="imggrid__fund-title"><?= _e('Minimum fundraising target:','velo'); ?></p>
<p class="imggrid__fund-cost"><?= $min_target; ?></p>
</li>
<?php } ?>
<?php if($website != '') { ?>
<li><a class="button-base" href="<?= $website; ?>" target="_blank"><?= _e('More Info','velo'); ?></a></li>
<?php } ?>
</ul>
Displaying The Meta Data
@smp303
add_action('init', 'add_custom_client_type');
function add_custom_client_type() {
$labels = array(
'name' => __( 'Client' ),
'singular_name' => __( 'Client' ),
'add_new_item' => __('Add Client'),
'edit_item' => __('Edit Client'),
'view_item' => __('View Client'),
'new_item' => __('New Client'),
'menu_name' => 'Clients'
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => false,
'show_in_nav_menus' => false,
'exclude_from_search' => false,
'supports' => array('title','author','thumbnail'),
'taxonomies' => array(),
'menu_icon' => 'dashicons-groups'
);
register_post_type('client', $args);
}
And repeat...
@smp303
Info
https://codex.wordpress.org/Post_Types
https://developer.wordpress.org/reference/functions/add_meta_box/
By Simon Pollard
Code is easy