Monthly Archives: September 2019

Admin dashboard notes and help tab content


// dashboard notes
function add_custom_dashboard_widgets() {
wp_add_dashboard_widget(
'wpexplorer_dashboard_widget', // Widget slug.
'Admin notes', // Title.
'custom_dashboard_widget_content' // Display function.
);
}
add_action( 'wp_dashboard_setup', 'add_custom_dashboard_widgets' );
function custom_dashboard_widget_content() {
echo "

Hello, please remember to stay away from the plugins menu.

Here are some important notes:

  • Datatables The data table ID must be set to the same as the product ID it is linked to.

If you have any need of assistance, please don't hesitate to contact us under:

  • https://www.d2creative.co.uk/
  • 01392 877116

";
}
// add help tab content
function setup_help_tab() {
$screen = get_current_screen();
if ( 'post' == $screen->post_type ) {
get_current_screen()->add_help_tab( array(
'id' => 'post',
'title' => ( 'How to Publish a Blog Post' ),
'content' => '

To publish a blog post, please follow the steps below.

  • Locate big, blue Publish button.
  • Click it.
  • Well done.

',
) );
}
}
// add more here
add_action( 'admin_head', 'setup_help_tab' );

Change “Posts” to “News” in WP Admin menu

function revcon_change_post_label() {
global $menu;
global $submenu;
$menu[5][0] = 'News';
$submenu['edit.php'][5][0] = 'News';
$submenu['edit.php'][10][0] = 'Add News';
$submenu['edit.php'][16][0] = 'News Tags';
}
function revcon_change_post_object() {
global $wp_post_types;
$labels = &$wp_post_types['post']->labels;
$labels->name = 'News';
$labels->singular_name = 'News';
$labels->add_new = 'Add News';
$labels->add_new_item = 'Add News';
$labels->edit_item = 'Edit News';
$labels->new_item = 'News';
$labels->view_item = 'View News';
$labels->search_items = 'Search News';
$labels->not_found = 'No News found';
$labels->not_found_in_trash = 'No News found in Trash';
$labels->all_items = 'All News';
$labels->menu_name = 'News';
$labels->name_admin_bar = 'News';
}

add_action( 'admin_menu', 'revcon_change_post_label' );
add_action( 'init', 'revcon_change_post_object' );

Custom post type with custom taxonomy in permalink

Custom post type

// Case studies custom post type
// based on https://wisdmlabs.com/blog/add-taxonomy-term-custom-post-permalinks-wordpress/
add_action('init', 'projects_cpt');
function projects_cpt() {
$labels = array(
'name' => 'Case studies',
'singular_name' => 'Case study',
'add_new' => 'Add New',
'add_new_item' => 'Add New Case study',
'edit_item' => 'Edit Case study',
'new_item' => 'New Case study',
'all_items' => 'All Case studies',
'view_item' => 'View Case study',
'search_items' => 'Search Case studies',
'not_found' => 'No Case studies found',
'not_found_in_trash' => 'No Case studies found in Trash',
'parent_item_colon' => '',
'menu_name' => 'Case studies'
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'taxonomies' => array('industrytype'),
'rewrite' => array('slug' => 'industries/%casestudycategory%/case-studies', 'with_front' => false),
//Adding custom rewrite tag
'capability_type' => 'post',
//'has_archive' => 'projectsarchives',
'has_archive' => false,
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt'),
);
register_post_type('casestudy', $args);

$labels = array(
‘name’ => ‘Case study Categories’,
‘singular_name’ => ‘Case studies’,
‘search_items’ => ‘Search Case study Categories’,
‘all_items’ => ‘All Case study Categories’,
‘parent_item’ => ‘Parent Case study Category’,
‘parent_item_colon’ => ‘Parent Case study Category:’,
‘edit_item’ => ‘Edit Case study Category’,
‘update_item’ => ‘Update Case study Category’,
‘add_new_item’ => ‘Add New Case study Category’,
‘new_item_name’ => ‘New Case study Category’,
);

$args = array(
‘hierarchical’ => true,
‘rewrite’ => array(‘slug’ => ‘case-studies’),
‘show_in_nav_menus’ => true,
‘show_admin_column’ => true,
‘labels’ => $labels
);

register_taxonomy(‘casestudycategory’, ‘casestudy’, $args);

unset($labels);
unset($args);
}

add_filter(‘post_type_link’, ‘projectcategory_permalink_structure’, 10, 4);
function projectcategory_permalink_structure($post_link, $post, $leavename, $sample) {
if (false !== strpos($post_link, ‘%casestudycategory%’)) {
// get all terms
$casestudycategory_type_term = get_the_terms($post->ID, ‘casestudycategory’);

if (!empty($casestudycategory_type_term)){
// find primary term
$primarycat = ‘x’;
$primarray = get_post_meta($post->ID,’_yoast_wpseo_primary_casestudycategory’);
foreach ( $casestudycategory_type_term as $term ) {
if( $primarray[0] == $term->term_id ) {
// this is a primary category
$primarycat = $term->slug;
}
}
if ($primarycat == ‘x’) {
$primarycat = array_pop($casestudycategory_type_term)->slug;
}
$post_link = str_replace(‘%casestudycategory%’, $primarycat, $post_link);
} else {
$post_link = str_replace(‘%casestudycategory%’, ‘uncategorized’, $post_link);
}
}
return $post_link;
}
// redirect automatic-casestudy-archive pages to actual pages
function custom_event_rewrite_one() {
add_rewrite_rule(    ‘^industries/([^/]+)/case-studies/?$’,
‘index.php?pagename=industries%2F$matches[1]%2Fcase-studies’,
‘top’);
add_rewrite_rule(    ‘^industries/([^/]+)/?$’,
‘index.php?pagename=industries%2F$matches[1]’,
‘top’);
}
add_action(‘init’,’custom_event_rewrite_one’);

Shortcode to display list of custom posts

// shortcode to list case studies with option to filter by casestudy category [listcasestudies cscat="automotive"]
function cdk_get_casestudies( $atts ) {

$atts = shortcode_atts( array(
‘cscat’ => ”
), $atts );

$args = [
‘post_type’      => ‘casestudy’,
‘posts_per_page’ => ‘-1’,
‘order’          => ‘DESC’,
‘orderby’        => ”,
];

if ( $atts[‘cscat’] ) {
$args[‘tax_query’] = [
[
‘taxonomy’ => ‘casestudycategory’,
‘field’    => ‘slug’,
‘terms’     => array( sanitize_title( $atts[‘cscat’] ) )
]
];
}

$the_query2 = new WP_Query( $args );
$output2 = ”;

if ( $the_query2->have_posts() ) {
while ( $the_query2->have_posts() ) {
$the_query2->the_post();

$terms = get_the_terms( get_the_ID(), ‘casestudycategory’ );
$all_cats = array();

$primarycat = ‘x’;
$primarray = get_post_meta(get_the_ID(),’_yoast_wpseo_primary_casestudycategory’);

if ( $terms && !is_wp_error( $terms ) ) {
foreach ( $terms as $term ) {
$all_cats[] = $term->name . ‘(‘.$term->term_id.’)’;

if( $primarray[0] == $term->term_id ) {
// this is a primary category
$primarycat = $term->slug;
}
}
}
$all_cats_str = join( “, “, $all_cats );
$first_cat_slug = $terms[0]->slug;
if ($primarycat == ‘x’) {
$primarycat = $first_cat_slug;
}

if ( $atts[‘cscat’] ) { // display for specific category
$output2 .= ‘<p> * <a href=”‘ . get_permalink() .'”>’. get_the_title() .'</a></p>’;
} else {
$output2 .= ‘<p> * <a href=”‘ . get_permalink() .'”>’. get_the_title() .'</a></p>’;
}
$output2 .= ‘<p>primary cat = ‘.$primarycat.’
<br>all = ‘.$all_cats_str.'</p>’;
}
wp_reset_postdata();
} else {
$output2 .= ‘<p>No case studies…</p>’;
}

return $output2;

}
add_shortcode(‘listcasestudies’, ‘cdk_get_casestudies’);