Helper Plugin
<?php
/*
Plugin Name: Philosophy Helper
Plugin URI:
Description: Helper Plugin for the philosophy theme
Version: 0.1.0
Author: Safe Syntax
Author URI: safesyntax.com
Text Domain: philosophy_helper
*/
function philosophy_helper_register_my_cpts_book() {
/**
* Post Type: Books.
*/
$labels = array(
"name" => __( "Books", "philosophy_helper" ),
"singular_name" => __( "Book", "philosophy_helper" ),
"menu_name" => __( "Books", "philosophy_helper" ),
"all_items" => __( "All Books", "philosophy_helper" ),
"add_new" => __( "Add New", "philosophy_helper" ),
"add_new_item" => __( "Add New Book", "philosophy_helper" ),
"edit_item" => __( "Edit Book", "philosophy_helper" ),
);
$args = array(
"label" => __( "Books", "philosophy_helper" ),
"labels" => $labels,
"description" => "",
"public" => true,
"publicly_queryable" => true,
"show_ui" => true,
"delete_with_user" => false,
"show_in_rest" => true,
"rest_base" => "",
"rest_controller_class" => "WP_REST_Posts_Controller",
"has_archive" => "books",
"show_in_menu" => true,
"show_in_nav_menus" => true,
"exclude_from_search" => false,
"capability_type" => "post",
"map_meta_cap" => true,
"hierarchical" => false,
"rewrite" => array( "slug" => "book", "with_front" => false ),
"query_var" => true,
"menu_position" => 6,
"menu_icon" => "dashicons-book-alt",
"supports" => array( "title", "editor", "thumbnail" ),
"taxonomies" => array( "category" ),
);
register_post_type( "book", $args );
}
add_action( 'init', 'philosophy_helper_register_my_cpts_book' );
// Search Form
add_filter('get_search_form', 'philosophy_search_form');
function philosophy_search_form($form) {
$homedir = home_url('/');
$label = __('Search For:', 'philosophy');
$button_label = __('Search', 'philosophy');
$post_type =<<<PT
<input type="hidden" name="post_type" value="post">
PT;
if(is_post_type_archive('book')) {
$post_type =<<<PT
<input type="hidden" name="post_type" value="book">
PT;
}
$newform = <<<FORM
<form role="search" method="get" class="header__search-form" action="{$homedir}">
<label>
<span class="hide-content">{$label}</span>
{$post_type}
<input type="search" class="search-field" placeholder="Type Keywords" value="" name="s" title="{$label}" autocomplete="off">
</label>
<input type="submit" class="search-submit" value="{$button_label}">
</form>
FORM;
return $newform;
}
function philosophy_cpt_slug_fix($post_link, $id) {
$p = get_post($id);
if(is_object($p) && 'chapter' == get_post_type($id)) {
$parent_post_id = get_field('parent_book');
$parent_post = get_post($parent_post_id);
if($parent_post){
$post_link = str_replace('%book%',$parent_post->post_name, $post_link);
}
}
return $post_link;
}
add_filter( 'post_type_link', 'philosophy_cpt_slug_fix', 1, 2);