membuat custom post type Wordpress

/*mendaftarkan type post*/
add_action( 'init', function() {
    $type = 'study';
    $label = 'Studies';
    $arguments = [
        'public' => true, // Allow access to post type
        'description' => 'Case studies for portfolio.', // Add a description
        'label'  => $label // Set the primary label
    ];
    register_post_type( $type, $arguments);
});


/*labeling / menganti label*/
function xcompile_post_type_labels($singular = 'Post', $plural = 'Posts') {
    $p_lower = strtolower($plural);
    $s_lower = strtolower($singular);

    return [
        'name' => $plural,
        'singular_name' => $singular,
        'add_new_item' => "New $singular",
        'edit_item' => "Edit $singular",
        'view_item' => "View $singular",
        'view_items' => "View $plural",
        'search_items' => "Search $plural",
        'not_found' => "No $p_lower found",
        'not_found_in_trash' => "No $p_lower found in trash",
        'parent_item_colon' => "Parent $singular",
        'all_items' => "All $plural",
        'archives' => "$singular Archives",
        'attributes' => "$singular Attributes",
        'insert_into_item' => "Insert into $s_lower",
        'uploaded_to_this_item' => "Uploaded to this $s_lower",
    ];
}

add_action( 'init', function() {
    $type = 'study';

    // Call the function and save it to $labels
    $labels = xcompile_post_type_labels('Study', 'Studies');

    $arguments = [
        'public' => true,
        'description' => 'Case studies for portfolio.',
        'labels'  => $labels // Changed to labels
    ];
    register_post_type( $type, $arguments);
});


/*menganti icon*/
add_action( 'init', function() {
    $type = 'study';
    $labels = xcompile_post_type_labels('Study', 'Studies');

    $arguments = [
        'public' => true,
        'description' => 'Case studies for portfolio.',
        'menu_icon' => 'dashicons-desktop', // Set icon
        'labels'  => $labels
    ];
    register_post_type( $type, $arguments);
});

/*enable / disable element post */
/*
title - For the title field.
editor - For the WordPress editor.
author - For the author meta box.
thumbnail - For the featured image.
excerpt - For the excerpt of text on an archive page for example.
trackbacks - For the trackback meta box.
custom-fields - For the custom fields.
comments - For comments.
revisions - For revisions and restoring old versions of the editor field.
page-attributes - For enabling the menu order and parent content when the post types hierarchy is enabled.
post-formats - For enabling the different post formats.
*/
// Add theme support for featured image / thumbnails
add_theme_support('post-thumbnails');

add_action( 'init', function() {
    $type = 'study';
    $labels = xcompile_post_type_labels('Study', 'Studies');

    // Declare what the post type supports
    $supports = ['title', 'editor', 'revisions', 'page-attributes', 'thumbnail'];

    $arguments = [
        'supports' => $supports, // Apply supports
        'public' => true,
        'description' => 'Case studies for portfolio.',
        'menu_icon' => 'dashicons-desktop',
        'labels'  => $labels,
    ];
    register_post_type( $type, $arguments);
});



/*mengganti placeholder post title*/
add_filter( 'enter_title_here', function( $title ) {
    $screen = get_current_screen();

    if  ( 'study' == $screen->post_type ) {
        $title = 'masukan judul disini';
    }

    return $title;
} );


/*Bulk Post Messages*/
add_filter( 'bulk_post_updated_messages', function( $bulk_messages, $bulk_counts ) {
    $bulk_messages['study'] = array(
        'updated'   => _n( "%s study updated.", "%s studies updated.", $bulk_counts["updated"] ),
        'locked'    => _n( "%s study not updated, somebody is editing it.", "%s studies not updated, somebody is editing them.", $bulk_counts["locked"] ),
        'deleted'   => _n( "%s study permanently deleted.", "%s studies permanently deleted.", $bulk_counts["deleted"] ),
        'trashed'   => _n( "%s study moved to the Trash.", "%s studies moved to the Trash.", $bulk_counts["trashed"] ),
        'untrashed' => _n( "%s study restored from the Trash.", "%s studies restored from the Trash.", $bulk_counts["untrashed"] ),
    );

    return $bulk_messages;
}, 10, 2 );


/*memasukan custom metaBOX*/
function study_meta_box(WP_Post $post) {
    add_meta_box('study_meta', 'Study Details', function() use ($post) {
        $field_name = 'your_field';
        $field_value = get_post_meta($post->ID, $field_name, true);
        wp_nonce_field('study_nonce', 'study_nonce');
        ?>
        <table class="form-table">
            <tr>
                <th> <label for="<?php echo $field_name; ?>">Your Field</label></th>
                <td>
                    <input id="<?php echo $field_name; ?>"
                           name="<?php echo $field_name; ?>"
                           type="text"
                           value="<?php echo esc_attr($field_value); ?>"
                    />
                </td>
            </tr>
        </table>
        <?php
    });
}



add_action( 'init', function() {
    $type = 'study';
    $labels = xcompile_post_type_labels('Study', 'Studies');

    // Declare what the post type supports
    $supports = ['title', 'editor', 'revisions', 'page-attributes', 'thumbnail'];

    $arguments = [
        'register_meta_box_cb' => 'study_meta_box', // Register a meta box
        'has_archive' => true,
        'rest_base' => 'studies',
        'show_in_rest' => true,
        'hierarchical' => false,
        'supports' => $supports,
        'public' => true,
        'description' => 'Case studies for portfolio.',
        'menu_icon' => 'dashicons-desktop',
        'labels'  => $labels,
         'rewrite' => [ 'slug' => 'studies' ]
    ];
    register_post_type( $type, $arguments);
});


// Check for empty string allowing for a value of `0`
function empty_str( $str ) {
    return ! isset( $str ) || $str === "";
}

// Save and delete meta but not when restoring a revision
add_action('save_post', function($post_id){
    $post = get_post($post_id);
    $is_revision = wp_is_post_revision($post_id);
    $field_name = 'your_field';

    // Do not save meta for a revision or on autosave
    if ( $post->post_type != 'study' || $is_revision )
        return;

    // Do not save meta if fields are not present,
    // like during a restore.
    if( !isset($_POST[$field_name]) )
        return;

    // Secure with nonce field check
    if( ! check_admin_referer('study_nonce', 'study_nonce') )
        return;

    // Clean up data
    $field_value = trim($_POST[$field_name]);

    // Do the saving and deleting
    if( ! empty_str( $field_value ) ) {
        update_post_meta($post_id, $field_name, $field_value);
    } elseif( empty_str( $field_value ) ) {
        delete_post_meta($post_id, $field_name);
    }
});

/*membuat toxonomy tag*/

add_action( 'init', function() {
    $type = 'study';
    $labels = xcompile_post_type_labels('Study', 'Studies');

    $arguments = [
        'taxonomies' => ['post_tag'], // And post tags
        'register_meta_box_cb' => 'study_meta_box',
        'labels'  => $labels,
        'description' => 'Case studies for portfolio.',
        'menu_icon' => 'dashicons-desktop',
        'public' => true,
        'has_archive' => true,
        'hierarchical' => false,
        'show_in_rest' => true,
        'rest_base' => 'studies',
        'supports' => ['title', 'editor', 'revisions', 'page-attributes', 'thumbnail'],
        'rewrite' => [ 'slug' => 'studies' ]
    ];

    register_post_type( $type, $arguments);
});

/*membuat toxonomy category*/
add_action( 'init', function() {
    $type = 'study';
    $labels = xcompile_post_type_labels('Study', 'Studies');

    $arguments = [
        'taxonomies' => ['category'], // And post tags
        'register_meta_box_cb' => 'study_meta_box',
        'labels'  => $labels,
        'description' => 'Case studies for portfolio.',
        'menu_icon' => 'dashicons-desktop',
        'public' => true,
        'has_archive' => true,
        'hierarchical' => false,
        'show_in_rest' => true,
        'rest_base' => 'studies',
        'supports' => ['title', 'editor', 'revisions', 'page-attributes', 'thumbnail'],
        'rewrite' => [ 'slug' => 'studies' ]
    ];

    register_post_type( $type, $arguments);
});

Komentar