Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
QuickTip: Make an SVG Library for Your Custom Post Type Icons
<?php
// Create your SVG library function
// Put this at the bottom of your functions.php file,
// or out of the way wherever you like
function menu_icons($icon){
// Store your SVGs in an associative array
$svgs = array(
'microphone' => 'data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iaXNvLTg4NTktMSI/Pg0KPCEtLSBHZW5lcmF0b3I6IEFkb2JlIElsbHVzdHJhdG9yIDE2LjAuMCwgU1ZHIEV4cG9ydCBQbHVnLUluIC4gU1ZHIFZlcnNpb246IDYuMDAgQnVpbGQgMCkgIC0tPg0KPCFET0NUWVBFIHN2ZyBQVUJMSUMgIi0vL1czQy8vRFREIFNWRyAxLjEvL0VOIiAiaHR0cDovL3d3dy53My5vcmcvR3JhcGhpY3MvU1ZHLzEuMS9EVEQvc3ZnMTEuZHRkIj4NCjxzdmcgdmVyc2lvbj0iMS4xIiBpZD0iQ2FwYV8xIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHhtbG5zOnhsaW5rPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L3hsaW5rIiB4PSIwcHgiIHk9IjBweCINCgkgd2lkdGg9IjY0cHgiIGhlaWdodD0iMTAwcHgiIHZpZXdCb3g9IjAgLTIwIDY0IDEyMCIgc3R5bGU9ImVuYWJsZS1iYWNrZ3JvdW5kOm5ldyAwIDAgNjQgMTAwOyIgeG1sOnNwYWNlPSJwcmVzZXJ2ZSI+DQo8Zz4NCgk8Zz4NCgkJPHBhdGggZD0iTTYyLDM2LjIxNWgtM2MtMS4xLDAtMiwwLjktMiwyVjUyYzAsNi42ODYtNS4yNjYsMTgtMjUsMThTNyw1OC42ODYsNyw1MlYzOC4yMTVjMC0xLjEtMC45LTItMi0ySDJjLTEuMSwwLTIsMC45LTIsMlY1Mg0KCQkJYzAsMTEuMTg0LDguMjE1LDIzLjE1MiwyNywyNC44MDFWOTBIMTRj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40MzdsMS43MDgsMS44MTZsMS43MDgtMS44MTYNCgkJYzIuMTQ1LTIuMjgxLDIwLjk2LTIyLjYyMywyMC45Ni0zNC40MzdDNzMuMTAzLDMwLjQ0LDYyLjkzNCwyMC4yNzEsNTAuNDM0LDIwLjI3MXogTTUwLjQzNCw1Mi4zMmMtNS4xNzIsMC05LjM4LTQuMjA4LTkuMzgtOS4zOA0KCQlzNC4yMDgtOS4zOCw5LjM4LTkuMzhjNS4xNzMsMCw5LjM4LDQuMjA4LDkuMzgsOS4zOFM1NS42MDcsNTIuMzIsNTAuNDM0LDUyLjMyeiIvPg0KPC9nPg0KPC9zdmc+DQo=',
);
// Return the chosen icon's SVG string
return $svgs[$icon];
}
<?php
// Register Custom Post Type
add_action( 'init', 'create_recording_cpt' );
function create_recording_cpt() {
register_post_type( 'recording',
array(
'labels' => array(
'name' => __( 'Recordings' ),
'singular_name' => __( 'Recording' ),
'add_new' => __( 'Add New' ),
'add_new_item' => __( 'Add New Recording' ),
'edit' => __( 'Edit' ),
'edit_item' => __( 'Edit Recording' ),
'new_item' => __( 'New Recording' ),
'view' => __( 'View Recording' ),
'view_item' => __( 'View Recording' ),
'search_items' => __( 'Search Recordings' ),
'not_found' => __( 'No Recordings found' ),
'not_found_in_trash' => __( 'No Recordings found in Trash' ),
'parent' => __( 'Parent Recording' ),
),
'public' => true,
'show_ui' => true,
'hierarchical' => true,
'menu_icon' => menu_icons('microphone'),
)
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment