array(
'name' => __('Snippets', 'add-custom-codes'),
'singular_name' => __('Snippet', 'add-custom-codes'),
'add_new' => __('Add New Snippet', 'add-custom-codes'),
'add_new_item' => __('Add New Snippet', 'add-custom-codes'),
'edit_item' => __('Edit Snippet', 'add-custom-codes'),
'new_item' => __('New Snippet', 'add-custom-codes'),
'view_item' => __('View Snippet', 'add-custom-codes'),
'search_items' => __('Search Snippets', 'add-custom-codes'),
'not_found' => __('No snippets found', 'add-custom-codes'),
'not_found_in_trash' => __('No snippets found in trash', 'add-custom-codes'),
'all_items' => __('All Snippets', 'add-custom-codes'),
'menu_name' => __('Add Custom Codes', 'add-custom-codes'),
'name_admin_bar' => __('Snippet', 'add-custom-codes'),
),
'public' => false,
'show_ui' => true,
'show_in_menu' => true,
'capability_type' => 'post',
'supports' => array('title'),
'menu_icon' => plugins_url('add-custom-codes/assets/images/accodes-menu-icon.svg'),
'menu_position' => 60,
'taxonomies' => array('accodes_tag'),
));
}
add_action('init', 'accodes_register_snippets_cpt');
//register tags
function accodes_register_snippet_tags_taxonomy() {
register_taxonomy('accodes_tag', 'accodes_snippets', array(
'label' => __('Snippet Tags', 'add-custom-codes'),
'labels' => array(
'name' => __('Snippet Tags', 'add-custom-codes'),
'singular_name' => __('Snippet Tag', 'add-custom-codes'),
'search_items' => __('Search Tags', 'add-custom-codes'),
'all_items' => __('All Tags', 'add-custom-codes'),
'edit_item' => __('Edit Tag', 'add-custom-codes'),
'update_item' => __('Update Tag', 'add-custom-codes'),
'add_new_item' => __('Add New Tag', 'add-custom-codes'),
'new_item_name' => __('New Tag Name', 'add-custom-codes'),
'menu_name' => __('Snippet Tags', 'add-custom-codes'),
),
'hierarchical' => false, // set to true if you want category-style nesting
'show_ui' => true,
'show_admin_column' => true,
'show_in_rest' => true,
'rewrite' => array('slug' => 'snippet-tag'),
'show_in_quick_edit' => false,
'meta_box_cb' => false,
));
}
add_action('init', 'accodes_register_snippet_tags_taxonomy');
//remove default tags box on edit screen
add_action('admin_menu', function () {
remove_meta_box('tagsdiv-accodes_tag', 'accodes_snippets', 'side');
});
//add submeanu
add_action('admin_menu', function () {
//global codes page
add_submenu_page(
'edit.php?post_type=accodes_snippets',
'Global Codes',
'Global Codes',
'manage_options',
'accodes-global',
'accodes_global_page_callback'
);
// Import / Export
add_submenu_page(
'edit.php?post_type=accodes_snippets',
'Import / Export Snippets',
'Import / Export',
'manage_options',
'accodes-import-export',
'accodes_import_export_page_callback'
);
//about page
add_submenu_page(
'edit.php?post_type=accodes_snippets',
'About Add Custom Codes',
'About',
'manage_options',
'accodes-about',
'accodes_about_page_callback'
);
});
// Disable block editor and hide content editor
add_filter('use_block_editor_for_post_type', function($use, $type){
return $type === 'accodes_snippets' ? false : $use;
}, 10, 2);
add_action('admin_head', function () {
$screen = get_current_screen();
if ($screen && $screen->post_type === 'accodes_snippets') {
remove_post_type_support('accodes_snippets', 'editor');
}
});
// place Snippet UI right below the title
add_action('edit_form_after_title', function () {
$screen = get_current_screen();
if ($screen->post_type === 'accodes_snippets') {
global $post;
accodes_render_snippet_editor($post);
}
});
// Render snippet UI
function accodes_render_snippet_editor($post) {
$content = get_post_meta($post->ID, '_accodes_code', true);
$language = get_post_meta($post->ID, '_accodes_snippet_language', true) ?: 'php';
$location = get_post_meta($post->ID, '_accodes_snippet_location', true) ?: 'site';
$active = get_post_meta($post->ID, '_accodes_snippet_active', true);
if ($active === '') {
$active = '1'; // default checked for new snippets
}
// If it's a new draft (not published yet), default to active
if ($active === '0' && $post->post_status !== 'publish') {
$active = '1';
}
$notes = get_post_meta($post->ID, '_accodes_snippet_notes', true);
$tags_raw = get_post_meta($post->ID, '_accodes_snippet_tags', true);
$tags_array = $tags_raw ? explode(',', $tags_raw) : [];
$error = get_post_meta($post->ID, '_accodes_snippet_error', true);
if ($error) {
echo '
';
echo esc_html($error);
echo '
';
// Echo JS to uncheck the status toggle
echo "";
}
?>
How do you want to use the snippet?
post_status === 'publish') ? '' : 'style="display:none;"'; ?>>
Shortcode (Click to copy)
[accodes_snippet id="ID); ?>"]
Shortcode Copied!
post_status === 'publish') ? 'style="display:none;"' : ''; ?>>
Shortcode will be displayed here after you Publish.
Use this field to describe what this snippet does. This won't be shown anywhere publicly.
Classify your snippets using tags. For example, if you are using multiple snippets for Woocommerce, use "Woocommerce" as a tag.
'accodes_snippets',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => [
[
'key' => '_accodes_snippet_active',
'value' => '1',
],
[
'key' => '_accodes_snippet_location',
'value' => $location,
]
],
];
$snippets = get_posts($args);
foreach ($snippets as $snippet) {
$language = get_post_meta($snippet->ID, '_accodes_snippet_language', true);
$code = get_post_meta($snippet->ID, '_accodes_code', true);
if (empty($code)) continue;
switch ($language) {
case 'php':
if (!is_admin()) {
$cleaned = preg_replace('/^\s*<\?(php)?/', '', trim($code));
try {
eval($cleaned);
delete_post_meta($snippet->ID, '_accodes_snippet_error'); // clear old errors
} catch (Throwable $e) {
// Auto-deactivate the snippet
update_post_meta($snippet->ID, '_accodes_snippet_active', '0');
// Log error
update_post_meta(
$snippet->ID,
'_accodes_snippet_error',
'PHP Error on line ' . $e->getLine() . ': ' . $e->getMessage()
);
}
}
break;
case 'css':
echo "\n";
break;
case 'javascript':
echo "\n";
break;
case 'htmlmixed':
default:
echo $code;
break;
}
}
}
// Declare Columns on snippets listing page
function accodes_snippet_columns($columns) {
$columns['accodes_type'] = __('Type', 'add-custom-codes');
$columns['accodes_health'] = __('Snippet Health', 'add-custom-codes');
$columns['accodes_status'] = __('Status', 'add-custom-codes');
return $columns;
}
add_filter('manage_accodes_snippets_posts_columns', 'accodes_snippet_columns');
// Render the columns in snippets listing page
function accodes_snippet_column_content($column, $post_id) {
if ($column === 'accodes_type') {
$lang = get_post_meta($post_id, '_accodes_snippet_language', true);
$badge = '';
switch ($lang) {
case 'php':
$badge = 'PHP';
break;
case 'css':
$badge = 'CSS';
break;
case 'javascript':
$badge = 'JS';
break;
case 'htmlmixed':
default:
$badge = 'HTML';
break;
}
echo $badge;
}
if ($column === 'accodes_health') {
$error = get_post_meta($post_id, '_accodes_snippet_error', true);
if (!empty($error)) {
echo 'i';
} else {
echo '';
}
}
if ($column === 'accodes_status') {
$active = get_post_meta($post_id, '_accodes_snippet_active', true);
$checked = $active == '1' ? 'checked' : '';
echo '
';
}
}
add_action('manage_accodes_snippets_posts_custom_column', 'accodes_snippet_column_content', 10, 2);
//change Snippet status on Status toggle click (List snippets page)
add_action('wp_ajax_accodes_toggle_snippet_status', function () {
check_ajax_referer('accodes_toggle_snippet');
$post_id = intval($_POST['post_id'] ?? 0);
$active = $_POST['active'] === '1' ? '1' : '0';
if ($post_id && get_post_type($post_id) === 'accodes_snippets') {
update_post_meta($post_id, '_accodes_snippet_active', $active);
wp_send_json_success(['status' => $active]);
}
wp_send_json_error(['message' => 'Invalid post ID or type']);
});
//create shortcode option for html
add_action('init', function () {
add_shortcode('accodes_snippet', function ($atts) {
$atts = shortcode_atts(['id' => 0], $atts);
$post_id = intval($atts['id']);
if (!$post_id || get_post_type($post_id) !== 'accodes_snippets') return '';
$location = get_post_meta($post_id, '_accodes_snippet_location', true);
$active = get_post_meta($post_id, '_accodes_snippet_active', true);
$content = get_post_meta($post_id, '_accodes_code', true);
$language = get_post_meta($post_id, '_accodes_snippet_language', true);
if ($location === 'shortcode' && $active === '1' && $language === 'htmlmixed') {
return $content;
}
return ''; // fallback: do nothing
});
});
//put snippets on site
add_action('admin_footer', function () {
if (is_admin()) accodes_inject_snippets('admin');
});
add_action('wp_body_open', function () {
if (!is_admin()) accodes_inject_snippets('site');
});
add_action('wp_footer', function () {
if (!is_admin()) accodes_inject_snippets('footer');
});
add_action('wp_head', function () {
accodes_inject_snippets('head');
});