arc-hive-wordpress-plugin/includes/arcHIVE-custom-post-type-re...

141 lines
5.9 KiB
PHP
Raw Normal View History

2022-03-29 20:53:36 +02:00
<?php
2022-03-30 13:20:45 +02:00
/**
* Custom post type Resources.
*
* @link https://arc-hive.zone
* @since 1.1.0
*
* @package archive_wpplugin
* @subpackage archive_wpplugin/includes
*/
/**
* Custom post type Resources.
*
* This class defines all code necessary to run during the plugin's activation.
*
* @since 1.1.0
* @package archive_wpplugin
* @subpackage archive_wpplugin/includes
* @author Jorge - vitrubio.net <jorge@vitrubio.net>
*/
2022-03-29 20:53:36 +02:00
/**
* Start custom post types
* ----------------------------------------------------------------------------
2022-03-30 13:20:45 +02:00
* https://developer.wordpress.org/plugins/post-types/registering-custom-post-types/
* https://developer.wordpress.org/reference/functions/register_post_type/
2022-03-29 20:53:36 +02:00
*/
2022-03-30 13:20:45 +02:00
2022-03-29 20:53:36 +02:00
// Register Custom Post Type
2022-03-30 13:20:45 +02:00
function archive_wpplugin_custom_post_resource() {
2022-03-29 20:53:36 +02:00
$labels = array(
'name' => _x( 'All resources', 'Post Type General Name', 'text_domain' ),
'singular_name' => _x( 'Resource', 'Post Type Singular Name', 'text_domain' ),
'menu_name' => __( 'Resources', 'text_domain' ),
'parent_item_colon' => __( 'Parent resources:', 'text_domain' ),
'all_items' => __( 'All the resources', 'text_domain' ),
'view_item' => __( 'Show the resource', 'text_domain' ),
'add_new_item' => __( 'Add a new resource', 'text_domain' ),
'add_new' => __( 'Add new resources', 'text_domain' ),
'edit_item' => __( 'Edit the resource', 'text_domain' ),
'update_item' => __( 'Save the resource', 'text_domain' ),
'search_items' => __( 'Search the resource', 'text_domain' ),
'not_found' => __( 'We couldn\'t find any resource ', 'text_domain' ),
'not_found_in_trash' => __( 'We couldn\'t find any resource in the bin', 'text_domain' ),
);
$rewrite = array(
2022-03-30 13:20:45 +02:00
'slug' => 'resources/%archive_wpplugin_resource_type%',
2022-03-29 20:53:36 +02:00
'with_front' => true,
'hierarchical' => true,
);
$args = array(
2022-03-30 13:20:45 +02:00
'label' => __( 'archive_wpplugin_resource', 'text_domain' ),
2022-03-29 20:53:36 +02:00
'description' => __( 'Resources arcHIVE', 'text_domain' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'thumbnail', 'trackbacks', 'revisions', 'custom-fields', 'page-attributes' ),
2022-03-30 13:20:45 +02:00
'taxonomies' => array('category','post_tag','archive_wpplugin_resource_type'),
2022-03-29 20:53:36 +02:00
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
2022-03-30 13:20:45 +02:00
'menu_position' => 5,
2022-03-29 20:53:36 +02:00
'menu_icon' => 'dashicons-book',
'can_export' => true,
'has_archive' => 'resources',
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
'rewrite' => $rewrite,
);
2022-03-30 13:20:45 +02:00
register_post_type( 'archive_wpplugin_resource', $args );
2022-03-29 20:53:36 +02:00
}
// Hook into the 'init' action
2022-03-30 13:20:45 +02:00
add_action( 'init', 'archive_wpplugin_custom_post_resource', 0 );
2022-03-29 20:53:36 +02:00
/**
* Start custom taxonomies
* ----------------------------------------------------------------------------
2022-03-30 13:20:45 +02:00
* https://developer.wordpress.org/plugins/taxonomies/working-with-custom-taxonomies/
2022-03-29 20:53:36 +02:00
*/
2022-03-30 13:20:45 +02:00
2022-03-29 20:53:36 +02:00
// Register Custom Taxonomy
2022-03-30 13:20:45 +02:00
function archive_wpplugin_custom_taxonomy_resource() {
2022-03-29 20:53:36 +02:00
$labels = array(
'name' => _x( 'Type of resources arcHIVE', 'Taxonomy General Name', 'text_domain' ),
'singular_name' => _x( 'Type of resource arcHIVE', 'Taxonomy Singular Name', 'text_domain' ),
'menu_name' => __( 'Type resources arcHIVE', 'text_domain' ),
'all_items' => __( 'All type resources arcHIVE', 'text_domain' ),
'parent_item' => __( 'Parent type resource arcHIVE', 'text_domain' ),
'parent_item_colon' => __( 'Parent type resource arcHIVE:', 'text_domain' ),
'new_item_name' => __( 'New type resource arcHIVE', 'text_domain' ),
'add_new_item' => __( 'Add New type resource arcHIVE', 'text_domain' ),
'edit_item' => __( 'Edit type resource arcHIVE', 'text_domain' ),
'update_item' => __( 'Update type of resource', 'text_domain' ),
'separate_items_with_commas' => __( 'Separate type resource arcHIVE with commas', 'text_domain' ),
'search_items' => __( 'Search type of resource', 'text_domain' ),
'add_or_remove_items' => __( 'Add or remove type of resource arcHIVE', 'text_domain' ),
'choose_from_most_used' => __( 'Choose from the most used type of resource arcHIVE', 'text_domain' ),
'not_found' => __( 'Type of resource arcHIVE Not Found', 'text_domain' ),
);
$rewrite = array(
'slug' => 'resources',
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
2022-03-30 13:20:45 +02:00
'rewrite' => $rewrite,
2022-03-29 20:53:36 +02:00
);
2022-03-30 13:20:45 +02:00
register_taxonomy( 'archive_wpplugin_resource_type', array( 'archive_wpplugin_resource' ), $args );
2022-03-29 20:53:36 +02:00
}
// Hook into the 'init' action
2022-03-30 13:20:45 +02:00
add_action( 'init', 'archive_wpplugin_custom_taxonomy_resource', 0 );
2022-03-29 20:53:36 +02:00
// changing the permalink
// http://wordpress.stackexchange.com/questions/108642/permalinks-custom-post-type-custom-taxonomy-post
2022-03-30 13:20:45 +02:00
function archive_wpplugin_show_permalinks( $post_link, $id = 0 ){
2022-03-29 20:53:36 +02:00
$post = get_post($id);
2022-03-30 13:20:45 +02:00
if ( is_object( $post ) && $post->post_type == 'archive_wpplugin_resource' ){
$terms = wp_get_object_terms( $post->ID, 'archive_wpplugin_resource_type' );
2022-03-29 20:53:36 +02:00
if( $terms ){
2022-03-30 13:20:45 +02:00
return str_replace( '%archive_wpplugin_resource_type%' , $terms[0]->slug , $post_link );
2022-03-29 20:53:36 +02:00
}
}
return $post_link;
}
2022-03-30 13:20:45 +02:00
add_filter( 'post_type_link', 'archive_wpplugin_show_permalinks', 1, 2 );