Skip to content

Instantly share code, notes, and snippets.

@GarySwift
Last active October 22, 2023 14:33
Show Gist options
  • Select an option

  • Save GarySwift/ade9f08feca92df2c08705f23c26f707 to your computer and use it in GitHub Desktop.

Select an option

Save GarySwift/ade9f08feca92df2c08705f23c26f707 to your computer and use it in GitHub Desktop.
WordPress Testimonials Custom Post Type #cpt #acf

WordPress Testimonials Custom Post Type

Add a custum post type for testimonails in WordPress.

This includes Advanced Custom Fields field groups for some extra fields.

Usage

Create a directory to store all files.

mkdir -p library/custom-post-types/testimonial

Place all files in this directory and include require.php.

require_once 'library/custom-post-types/testimonial/require.php';

Requirements

Advanced Custom Fields plugin installed.

Admin style requires the directory structure to be 'library/custom-post-types/testimonial/'. This can be edited in _add-custom-admin-css.php to something more suitable.

[
{
"key": "group_5702c29056caf",
"title": "Testimonial Additonal Fields",
"fields": [
{
"key": "field_570404a1dc20c",
"label": "Position\/Status",
"name": "position",
"type": "text",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"placeholder": "",
"prepend": "",
"append": "",
"maxlength": "",
"readonly": 0,
"disabled": 0
},
{
"key": "field_570404d4dc20d",
"label": "Organisation",
"name": "organisation",
"type": "text",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"placeholder": "",
"prepend": "",
"append": "",
"maxlength": "",
"readonly": 0,
"disabled": 0
},
{
"key": "field_57040531dc20e",
"label": "Website",
"name": "website",
"type": "url",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"placeholder": ""
}
],
"location": [
[
{
"param": "post_type",
"operator": "==",
"value": "testimonial"
}
]
],
"menu_order": 0,
"position": "acf_after_title",
"style": "seamless",
"label_placement": "top",
"instruction_placement": "label",
"hide_on_screen": "",
"active": 1,
"description": ""
}
]
<?php
if( function_exists('acf_add_local_field_group') ):
acf_add_local_field_group(array (
'key' => 'group_5702c29056caf',
'title' => 'Testimonial Additonal Fields',
'fields' => array (
array (
'key' => 'field_570404a1dc20c',
'label' => 'Position/Status',
'name' => 'position',
'type' => 'text',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => '',
'readonly' => 0,
'disabled' => 0,
),
array (
'key' => 'field_570404d4dc20d',
'label' => 'Organisation',
'name' => 'organisation',
'type' => 'text',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => '',
'readonly' => 0,
'disabled' => 0,
),
array (
'key' => 'field_57040531dc20e',
'label' => 'Website',
'name' => 'website',
'type' => 'url',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
),
),
'location' => array (
array (
array (
'param' => 'post_type',
'operator' => '==',
'value' => 'testimonial',
),
),
),
'menu_order' => 0,
'position' => 'acf_after_title',
'style' => 'seamless',
'label_placement' => 'top',
'instruction_placement' => 'label',
'hide_on_screen' => '',
'active' => 1,
'description' => '',
));
endif;
<?php
/**
* Add stylesheet to WordPress backend
*/
function add_stylesheet_to_admin() {
wp_enqueue_style( 'prefix-style', get_template_directory_uri().'/functions-includes/custom-post-types/testimonial/_admin-style.css', __FILE__ );
}
add_action( 'admin_enqueue_scripts', 'add_stylesheet_to_admin' );
th.column-pos_org, td.column-pos_org {
width: 150px;
}
th.column-testimonial_content, td.column-testimonial_content {
width: 550px;
}
{"testimonial":{"name":"testimonial","label":"Testimonials","singular_label":"Testimonial","description":"","public":"false","show_ui":"true","show_in_nav_menus":"true","show_in_rest":"false","rest_base":"","has_archive":"false","has_archive_string":"","exclude_from_search":"true","capability_type":"post","hierarchical":"false","rewrite":"true","rewrite_slug":"","rewrite_withfront":"true","query_var":"true","query_var_slug":"","menu_position":"10","show_in_menu":"true","show_in_menu_string":"","menu_icon":"dashicons-testimonial","supports":["title","editor"],"taxonomies":[],"labels":{"menu_name":"","all_items":"","add_new":"","add_new_item":"","edit":"","edit_item":"","new_item":"","view":"","view_item":"","search_items":"","not_found":"","not_found_in_trash":"","parent":"","featured_image":"","set_featured_image":"","remove_featured_image":"","use_featured_image":"","archives":"","insert_into_item":"","uploaded_to_this_item":"","filter_items_list":"","items_list_navigation":"","items_list":""},"custom_supports":""}}
<?php
add_action( 'init', 'cptui_register_my_cpts_testimonial' );
function cptui_register_my_cpts_testimonial() {
$labels = array(
"name" => __( 'Testimonials', '' ),
"singular_name" => __( 'Testimonial', '' ),
);
$args = array(
"label" => __( 'Testimonials', '' ),
"labels" => $labels,
"description" => "",
"public" => false,
"show_ui" => true,
"show_in_rest" => false,
"rest_base" => "",
"has_archive" => false,
"show_in_menu" => true,
"exclude_from_search" => true,
"capability_type" => "post",
"map_meta_cap" => true,
"hierarchical" => false,
"rewrite" => array( "slug" => "testimonial", "with_front" => true ),
"query_var" => true,
"menu_position" => 10,"menu_icon" => "dashicons-testimonial",
"supports" => array( "title", "editor" ),
);
register_post_type( "testimonial", $args );
// End of cptui_register_my_cpts_testimonial()
}
<?php
function manage_columns_for_testimonial($columns){
//remove columns
unset($columns['date']);
unset($columns['title']);
//add new columns
$columns['testimonial_content'] = 'Testimonial';
$columns['title'] = _x('Attribution', 'column name');
$columns['pos_org'] = 'Position';
$columns['date'] = _x('Date', 'column name');
return $columns;
}
add_action('manage_testimonial_posts_columns','manage_columns_for_testimonial');
function populate_testimonial_columns($column,$post_id){
global $post;
//get the testimonial based on its post_id
$testimonial = get_post($post_id);
//testimonial content column
if($column == 'testimonial_content'){
if($testimonial){
//get the main content area
$testimonial_content = apply_filters('the_content', $testimonial->post_content);
$testimonial_content = strip_tags($testimonial_content);
$testimonial_content = truncate($testimonial_content, $length=300);
echo $testimonial_content;
}
}
if($column == 'pos_org'){
$pos_org='';
if(get_field('position')){
// echo 'Yes';
$pos_org = get_field('position');
}
if(get_field('organisation')){
if($pos_org){
// echo 'Yes';
$pos_org .= ', '.get_field('organisation');
}
else {
$pos_org = get_field('organisation');
}
}
echo $pos_org;
}
}
add_action('manage_testimonial_posts_custom_column','populate_testimonial_columns',10,2);
if (!function_exists('truncate')) {
function truncate($string,$length=100,$append="&hellip;") {
$string = trim($string);
if(strlen($string) > $length) {
$string = wordwrap($string, $length);
$string = explode("\n", $string, 2);
$string = $string[0] . $append;
}
return $string;
}
}
<?php
/*
Replace the default "Enter title here" placeholder text in the title input box
with something more descriptive can be helpful for custom post types
*/
function change_default_title_testimonial( $title ){
$screen = get_current_screen();
if ( 'testimonial' == $screen->post_type ){
$title = "Enter testimonial attribution here";
}
return $title;
}
add_filter( 'enter_title_here', 'change_default_title_testimonial' );
<?php
$show_testimonials=true;
if($show_testimonials) {
require_once '_cpt-testimonial.php';
require_once '_acf-field-group_testimonial-additonal-fields.php';
require_once '_testimonial-title-placeholder.php';
require_once '_posts-screen-columns-testimonial.php';
require_once '_add-custom-admin-css.php';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment