Skip to content

Commit 7150313

Browse files
committed
v0.1 - Updated custom post type
1 parent c24a0cf commit 7150313

2 files changed

Lines changed: 110 additions & 29 deletions

File tree

assets/inc/cpts/custom-metabox.php

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,41 @@ function myTheme_customMetabox() {
2121
// it is a callback function which actually displays the content of the meta box
2222
function myTheme_metaboxCB( $post ){
2323

24-
$seo_title = get_post_meta( $post->ID, 'seo_title', true );
25-
$seo_robots = get_post_meta( $post->ID, 'seo_robots', true );
24+
$client_name = get_post_meta( $post->ID, 'client_name', true );
25+
$client_url = get_post_meta( $post->ID, 'client_url', true );
26+
$is_external = get_post_meta( $post->ID, 'is_external', true );
27+
$nofollow = get_post_meta( $post->ID, 'nofollow', true );
28+
$noindex = get_post_meta( $post->ID, 'noindex', true );
2629

27-
// nonce, actually I think it is not necessary here
28-
wp_nonce_field( 'somerandomstr', '_mishanonce' );
30+
wp_nonce_field( 'project_zero', '_zerononce' );
2931

3032
echo '<table class="form-table">
3133
<tbody>
3234
<tr>
33-
<th><label for="seo_title">SEO title</label></th>
34-
<td><input type="text" id="seo_title" name="seo_title" value="' . esc_attr( $seo_title ) . '" class="regular-text"></td>
35+
<th><label for="client_name">Client name:</label></th>
36+
<td><input type="text" id="client_name" name="client_name" value="' . esc_attr( $client_name ) . '" class="regular-text"></td>
3537
</tr>
3638
<tr>
37-
<th><label for="seo_tobots">SEO robots</label></th>
39+
<th><label for="client_url">Website URL:</label></th>
40+
<td><input type="text" id="client_url" name="client_url" value="' . esc_attr( $client_url ) . '" class="regular-text"></td>
41+
</tr>
42+
<tr>
43+
<th><label for="is_external">Open in new window/tab:</label></th>
3844
<td>
39-
<select id="seo_robots" name="seo_robots">
40-
<option value="">Select...</option>
41-
<option value="index,follow"' . selected( 'index,follow', $seo_robots, false ) . '>Show for search engines</option>
42-
<option value="noindex,nofollow"' . selected( 'noindex,nofollow', $seo_robots, false ) . '>Hide for search engines</option>
45+
<select id="is_external" name="is_external">
46+
<option value="_self" ' . selected( '_self', $is_external, false ) . '>Open in the current window/tab</option>
47+
<option value="_blank" ' . selected( '_blank', $is_external, false ) . '>Open in a new window/tab</option>
4348
</select>
4449
</td>
4550
</tr>
51+
<tr>
52+
<th><label for="nofollow">Add nofollow:</label></th>
53+
<td><input type="checkbox" name="nofollow" value="1" ' . checked( $nofollow, 1, false ) . ' /></td>
54+
</tr>
55+
<tr>
56+
<th><label for="noindex">Add noindex:</label></th>
57+
<td><input type="checkbox" name="noindex" value="1" ' . checked( $noindex, 1, false ) . ' /></td>
58+
</tr>
4659
</tbody>
4760
</table>';
4861

@@ -53,7 +66,7 @@ function myTheme_metaboxCB( $post ){
5366
function myTheme_saveMetaData( $post_id, $post ) {
5467

5568
// nonce check
56-
if ( ! isset( $_POST[ '_mishanonce' ] ) || ! wp_verify_nonce( $_POST[ '_mishanonce' ], 'somerandomstr' ) ) {
69+
if ( ! isset( $_POST[ '_zerononce' ] ) || ! wp_verify_nonce( $_POST[ '_zerononce' ], 'project_zero' ) ) {
5770
return $post_id;
5871
}
5972

@@ -69,20 +82,30 @@ function myTheme_saveMetaData( $post_id, $post ) {
6982
return $post_id;
7083
}
7184

72-
// define your own post type here
73-
if( 'page' !== $post->post_type ) {
74-
return $post_id;
85+
if( isset( $_POST[ 'client_name' ] ) ) {
86+
update_post_meta( $post_id, 'client_name', sanitize_text_field( $_POST[ 'client_name' ] ) );
87+
} else {
88+
delete_post_meta( $post_id, 'client_name' );
7589
}
76-
77-
if( isset( $_POST[ 'seo_title' ] ) ) {
78-
update_post_meta( $post_id, 'seo_title', sanitize_text_field( $_POST[ 'seo_title' ] ) );
90+
if( isset( $_POST[ 'client_url' ] ) ) {
91+
update_post_meta( $post_id, 'client_url', sanitize_text_field( $_POST[ 'client_url' ] ) );
92+
} else {
93+
delete_post_meta( $post_id, 'client_url' );
94+
}
95+
if( isset( $_POST[ 'is_external' ] ) ) {
96+
update_post_meta( $post_id, 'is_external', $_POST[ 'is_external' ] );
97+
} else {
98+
delete_post_meta( $post_id, 'is_external' );
99+
}
100+
if( isset( $_POST[ 'nofollow' ] ) ) {
101+
update_post_meta( $post_id, 'nofollow', $_POST[ 'nofollow' ] );
79102
} else {
80-
delete_post_meta( $post_id, 'seo_title' );
103+
delete_post_meta( $post_id, 'nofollow' );
81104
}
82-
if( isset( $_POST[ 'seo_robots' ] ) ) {
83-
update_post_meta( $post_id, 'seo_robots', sanitize_text_field( $_POST[ 'seo_robots' ] ) );
105+
if( isset( $_POST[ 'noindex' ] ) ) {
106+
update_post_meta( $post_id, 'noindex', $_POST[ 'noindex' ] );
84107
} else {
85-
delete_post_meta( $post_id, 'seo_robots' );
108+
delete_post_meta( $post_id, 'noindex' );
86109
}
87110

88111
return $post_id;

assets/inc/cpts/custom-type.php

Lines changed: 65 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ function myTheme_customPost() {
6767
'capability_type' => 'post', // Used to build the read, edit, and delete capabilities.
6868
// 'map_meta_cap' => false, // Whether to use the internal default meta capability handling.
6969
'supports' => array( 'title', 'editor', 'comments', 'revisions', 'trackbacks', 'author', 'excerpt', 'page-attributes', 'thumbnail', 'custom-fields', 'post-formats' ), // Core feature(s) the post type supports. Add or remove as you require.
70-
'has_archive' => true, // Whether the post type has an archive. If this is a string, this will be the archive slug. E.g. the single slug may be 'project', but the archive slug could be 'portfolio'.
70+
'has_archive' => 'portfolio', // Whether the post type has an archive. If this is a string, this will be the archive slug. E.g. the single slug may be 'project', but the archive slug could be 'portfolio'.
7171
'rewrite' => array( // Triggers permalinks for the page. Defaults to true, but can be set to false
7272
'slug' => 'project', // Customises the slug to single posts
7373
'with_front' => true, // Whether the posts should be prepended with WP_Rewrite::$front.
@@ -99,9 +99,9 @@ function myTheme_customTag() {
9999
* A list of all available labels can be found at @link https://developer.wordpress.org/reference/functions/register_taxonomy/#arguments
100100
*/
101101
$labels = array(
102-
'name' => _x( 'Categories', 'Taxonomy general name', 'zero-theme' ),
103-
'singular_name' => _x( 'Category', 'Taxonomy singular name', 'zero-theme' ),
104-
'menu_name' => __( 'Categories', 'zero-theme' ),
102+
'name' => _x( 'Project Categories', 'Taxonomy general name', 'zero-theme' ),
103+
'singular_name' => _x( 'Project Category', 'Taxonomy singular name', 'zero-theme' ),
104+
'menu_name' => __( 'Project Categories', 'zero-theme' ),
105105
'all_items' => __( 'All Categories', 'zero-theme' ),
106106
'edit_item' => __( 'Edit Category', 'zero-theme' ),
107107
'view_item' => __( 'View Category', 'zero-theme' ),
@@ -176,9 +176,9 @@ function myTheme_customTags() {
176176
* A list of all available labels can be found at @link https://developer.wordpress.org/reference/functions/register_taxonomy/#arguments
177177
*/
178178
$labels = array(
179-
'name' => _x( 'Tags', 'Taxonomy general name', 'zero-theme' ),
180-
'singular_name' => _x( 'Tag', 'Taxonomy singular name', 'zero-theme' ),
181-
'menu_name' => __( 'Tags', 'zero-theme' ),
179+
'name' => _x( 'Project Tags', 'Taxonomy general name', 'zero-theme' ),
180+
'singular_name' => _x( 'Project Tag', 'Taxonomy singular name', 'zero-theme' ),
181+
'menu_name' => __( 'Project Tags', 'zero-theme' ),
182182
'all_items' => __( 'All Tags', 'zero-theme' ),
183183
'edit_item' => __( 'Edit Tag', 'zero-theme' ),
184184
'view_item' => __( 'View Tag', 'zero-theme' ),
@@ -246,9 +246,67 @@ function myTheme_customTags() {
246246
register_taxonomy( 'projecttags', array( 'portfolio' ), $args );
247247

248248
}
249+
250+
251+
/**
252+
* 'Services' taxonomy, to demonstrate how it would appear on the site
253+
*/
254+
function myTheme_projectServices() {
255+
256+
/**
257+
* @param array $labels Sets the text labels to appear in the taxonomy.
258+
* A list of all available labels can be found at @link https://developer.wordpress.org/reference/functions/register_taxonomy/#arguments
259+
*/
260+
$labels = array(
261+
'name' => _x( 'Project Services', 'Taxonomy general name', 'zero-theme' ),
262+
'singular_name' => _x( 'Project Service', 'Taxonomy singular name', 'zero-theme' ),
263+
'menu_name' => __( 'Project Services', 'zero-theme' ),
264+
'all_items' => __( 'All Services', 'zero-theme' ),
265+
'edit_item' => __( 'Edit Service', 'zero-theme' ),
266+
'view_item' => __( 'View Service', 'zero-theme' ),
267+
'update_item' => __( 'Update Service', 'zero-theme' ),
268+
'add_new_item' => __( 'Add New Service', 'zero-theme' ),
269+
'new_item_name' => __( 'New Service Name', 'zero-theme' ),
270+
'parent_item' => __( 'Parent Service', 'zero-theme' ),
271+
'parent_item_colon' => __( 'Parent Service:', 'zero-theme' ),
272+
'search_items' => __( 'Search Services', 'zero-theme' ),
273+
'not_found' => __( 'No services found.', 'zero-theme' ),
274+
'back_to_items' => __( '← Back to services', 'zero-theme' ),
275+
);
276+
277+
/**
278+
* @param array $args Sets the parameters to register the post type
279+
* All acceptable parameters are listed here. Those commented out can be safely removed, but are kept here for your reference.
280+
*/
281+
$args = array(
282+
'labels' => $labels,
283+
'description' => 'List of services that have been used for the project',
284+
'public' => true, // Whether a taxonomy is intended for use publicly either via the admin interface or by front-end users.
285+
'publicly_queryable' => true, // Whether the taxonomy is publicly queryable.
286+
'hierarchical' => true, // Whether the taxonomy is hierarchical.
287+
'show_ui' => true, // Whether to generate and allow a UI for managing terms in this taxonomy in the admin.
288+
'show_in_menu' => true, // Whether to show the taxonomy in the admin menu.
289+
'show_in_nav_menus' => false, // Makes this taxonomy available for selection in navigation menus.
290+
'show_in_rest' => true, // Whether to include the taxonomy in the REST API. Set this to true for the taxonomy to be available in the block editor.
291+
'show_in_quick_edit' => true, // Whether to show the taxonomy in the quick/bulk edit panel.
292+
'show_admin_column' => true, // Whether to display a column for the taxonomy on its post type listing screens.
293+
);
294+
295+
/**
296+
* Registers taxonomy and assigns it to custom post type
297+
*
298+
* @param string $taxonomy Taxonomy key, must not exceed 32 characters and may only contain lowercase alphanumeric characters, dashes, and underscores.
299+
* @param array|string $object_type Sets the post types the taxonomy can be assigned to.
300+
* @param array|string $args Array or string of arguments to register taxonomy
301+
*/
302+
register_taxonomy( 'project_services', array( 'portfolio' ), $args );
303+
304+
}
305+
249306
// hook into the init action and call create_book_taxonomies when it fires
250307
add_action( 'init', 'myTheme_customTag', 0 );
251308
add_action( 'init', 'myTheme_customTags', 0 );
309+
add_action( 'init', 'myTheme_projectServices', 0 );
252310

253311
function get_custom_post_type_template($archive_template){
254312
global $post;

0 commit comments

Comments
 (0)