File: /home/urgentelectrical/public_html/wp-content/themes/my-custom-theme/functions.php
<?php
/* Add Scripts & Styles */
function add_scripts() {
wp_register_style('google-fonts-1', 'https://fonts.googleapis.com/css2?family=Montserrat:ital,wght@0,100..900;1,100..900&display=swap" rel="stylesheet', array(), null);
wp_enqueue_style('google-fonts-1');
wp_register_style('bootstrap', 'https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.3.2/css/bootstrap.min.css', array(), null);
wp_enqueue_style( 'bootstrap');
wp_register_style('font-awesome', 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.7.1/css/all.min.css', array(), null);
wp_enqueue_style( 'font-awesome');
wp_register_style('slick', 'https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.8.1/slick.min.css', array(), null);
wp_enqueue_style( 'slick');
wp_register_style('slick-theme', 'https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.8.1/slick-theme.min.css', array(), null);
wp_enqueue_style( 'slick-theme');
wp_register_style('fancybox', 'https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.3.5/jquery.fancybox.min.css', array(), null);
wp_enqueue_style( 'fancybox');
wp_register_style('aos', 'https://cdnjs.cloudflare.com/ajax/libs/aos/2.3.4/aos.css', array(), null);
wp_enqueue_style( 'aos');
wp_register_style('animate', 'https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.7.2/animate.min.css', array(), null);
wp_enqueue_style( 'animate');
wp_register_style('main-style', get_stylesheet_directory_uri() . '/assets/css/main-style.css', array(), null);
wp_enqueue_style( 'main-style');
wp_register_style('media-style', get_stylesheet_directory_uri() . '/assets/css/main-media.css', array(), null);
wp_enqueue_style( 'media-style');
wp_register_style('style', get_stylesheet_directory_uri() . '/style.css', array(), null);
wp_enqueue_style( 'style');
// Scripts
wp_register_script('custom-jquery-defer', 'https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js', array(), null, true);
wp_enqueue_script( 'custom-jquery-defer' );
wp_register_script('bootstrap-defer', 'https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.3.2/js/bootstrap.bundle.min.js', array(), null, true);
wp_enqueue_script( 'bootstrap-defer' );
wp_register_script('slick-defer', 'https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.8.1/slick.min.js', array(), null, true);
wp_enqueue_script( 'slick-defer' );
wp_register_script('slick-animate-defer', 'https://alexandrebuffet.fr/codepen/slider/slick-animation.min.js', array(), null, true);
wp_enqueue_script( 'slick-animate-defer' );
wp_register_script('fancybox-defer', 'https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.3.5/jquery.fancybox.min.js', array(), null, true);
wp_enqueue_script( 'fancybox-defer' );
wp_register_script('waypoints-defer', 'https://cdnjs.cloudflare.com/ajax/libs/waypoints/4.0.1/jquery.waypoints.min.js', array(), null, true);
wp_enqueue_script( 'waypoints-defer' );
wp_register_script('counter-defer', 'https://cdnjs.cloudflare.com/ajax/libs/counterup2/2.0.2/index.min.js', array(), null, true);
wp_enqueue_script( 'counter-defer' );
wp_register_script('aos-defer', 'https://cdnjs.cloudflare.com/ajax/libs/aos/2.3.4/aos.js', array(), null, true);
wp_enqueue_script( 'aos-defer' );
wp_register_script('isotope.pkgd', 'https://unpkg.com/isotope-layout@3/dist/isotope.pkgd.min.js', array(), null, true);
wp_enqueue_script( 'isotope.pkgd' );
wp_register_script('custom-script-defer', get_stylesheet_directory_uri() . '/assets/js/main-script.js', array(), null, true);
wp_enqueue_script( 'custom-script-defer' );
}
add_action('wp_enqueue_scripts', 'add_scripts', 999);
/* Enable classic editor */
add_filter('use_block_editor_for_post','__return_false',10);
//Remove auto addition of P tag in Contact Form 7
add_filter('wpcf7_autop_or_not', '__return_false');
// Registering a menu location
function my_custom_menus() {
register_nav_menus( array(
'primary_menu' => __( 'Primary Menu' ),
'footer_menu' => __( 'Footer Menu' ),
'services_menu' => __( 'Services Menu' ),
) );
}
add_action( 'after_setup_theme', 'my_custom_menus' );
// Remove category base from URLs
function remove_category_base($string) {
return str_replace('/category/', '/', $string);
}
add_filter('category_link', 'remove_category_base');
// Add featured image support for posts
function theme_setup() {
add_theme_support('post-thumbnails');
}
add_action('after_setup_theme', 'theme_setup');
// Enable editor support for posts
function enable_editor_support_for_posts() {
add_post_type_support('post', 'editor');
}
add_action('init', 'enable_editor_support_for_posts');
function kelston_register_services_cpt() {
$labels = array(
'name' => 'Services',
'singular_name' => 'Service',
'add_new_item' => 'Add New Service',
'edit_item' => 'Edit Service',
'all_items' => 'All Services',
);
$args = array(
'labels' => $labels,
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'services'),
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'page-attributes'), // page-attributes for parent/child
'show_in_rest' => true,
'hierarchical' => true, // allow parent-child relationship
);
register_post_type('services', $args);
}
add_action('init', 'kelston_register_services_cpt');
function custom_breadcrumb() {
global $post;
if (is_front_page()) return;
$separator = ' <span class="sep">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16" fill="none">
<path fill-rule="evenodd" clip-rule="evenodd"
d="M9.96812 7.16653C10.0871 7.28553 10.1539 7.44692 10.1539 7.61519C10.1539 7.78346 10.0871 7.94485 9.96812 8.06385L6.37817 11.6538C6.31963 11.7144 6.24961 11.7628 6.17219 11.796C6.09476 11.8293 6.01149 11.8468 5.92723 11.8475C5.84296 11.8482 5.7594 11.8322 5.68141 11.8003C5.60342 11.7684 5.53256 11.7213 5.47298 11.6617C5.4134 11.6021 5.36627 11.5312 5.33437 11.4532C5.30246 11.3752 5.2864 11.2917 5.28713 11.2074C5.28787 11.1232 5.30537 11.0399 5.33863 10.9625C5.37189 10.885 5.42023 10.815 5.48085 10.7565L8.62213 7.61519L5.48085 4.47391C5.36525 4.35422 5.30128 4.19392 5.30273 4.02753C5.30418 3.86113 5.37092 3.70197 5.48858 3.58431C5.60624 3.46665 5.7654 3.39991 5.93179 3.39846C6.09818 3.39702 6.25849 3.46098 6.37817 3.57658L9.96812 7.16653Z"
fill="white"/>
</svg>
</span> ';
echo '<a href="' . home_url() . '">Home</a>' . $separator;
// 👉 Services CPT single
if (is_singular('services')) {
// CPT archive link
echo '<a href="' . get_post_type_archive_link('services') . '">Services</a>' . $separator;
// Parent service (agar hierarchical hai)
if ($post->post_parent) {
$ancestors = array_reverse(get_post_ancestors($post->ID));
foreach ($ancestors as $ancestor) {
echo '<a href="' . get_permalink($ancestor) . '">' . get_the_title($ancestor) . '</a>' . $separator;
}
}
// Current service
echo '<span class="current">' . get_the_title() . '</span>';
}
// 👉 Services archive page
elseif (is_post_type_archive('services')) {
echo '<span class="current">Services</span>';
}
// 👉 Normal pages
elseif (is_page()) {
if ($post->post_parent) {
$ancestors = array_reverse(get_post_ancestors($post->ID));
foreach ($ancestors as $ancestor) {
echo '<a href="' . get_permalink($ancestor) . '">' . get_the_title($ancestor) . '</a>' . $separator;
}
}
echo '<span class="current">' . get_the_title() . '</span>';
}
// Single blog post
elseif (is_single() && 'post' === get_post_type()) {
$blog_page_id = get_option('page_for_posts'); // The "Posts Page" set in Settings → Reading
if ($blog_page_id) {
echo '<a href="' . get_permalink($blog_page_id) . '">'. get_the_title($blog_page_id) . '</a>' . $separator;
} else {
echo '<a href="' . home_url() . '/blog">Blog</a>' . $separator; // fallback
}
echo '<span class="current">' . get_the_title() . '</span>';
}
}
// Allow SVG upload
add_filter('upload_mimes', function ($mimes) {
$mimes['svg'] = 'image/svg+xml';
return $mimes;
});
// Fix SVG upload validation
add_filter( 'wp_check_filetype_and_ext', function( $data, $file, $filename, $mimes ) {
$filetype = wp_check_filetype( $filename, $mimes );
return [
'ext' => $filetype['ext'],
'type' => $filetype['type'],
'proper_filename' => $data['proper_filename']
];
}, 10, 4 );
// SVG preview fix
add_action('admin_head', function () {
echo '<style>
img[src$=".svg"] {
width: 100%;
height: auto;
}
</style>';
});
// Add Duplicate link in post/page list
function custom_duplicate_post_link( $actions, $post ) {
if ( current_user_can( 'edit_posts' ) ) {
$actions['duplicate'] = '<a href="' .
wp_nonce_url(
admin_url( 'admin.php?action=duplicate_post_as_draft&post=' . $post->ID ),
'duplicate_post_nonce'
) .
'" title="Duplicate this item">Duplicate</a>';
}
return $actions;
}
add_filter( 'post_row_actions', 'custom_duplicate_post_link', 10, 2 );
add_filter( 'page_row_actions', 'custom_duplicate_post_link', 10, 2 );
function duplicate_post_as_draft() {
if (
! isset( $_GET['post'] ) ||
! isset( $_GET['_wpnonce'] ) ||
! wp_verify_nonce( $_GET['_wpnonce'], 'duplicate_post_nonce' )
) {
wp_die( 'Invalid request.' );
}
$post_id = absint( $_GET['post'] );
$post = get_post( $post_id );
if ( ! $post ) {
wp_die( 'Post not found.' );
}
$new_post_id = wp_insert_post( [
'post_title' => $post->post_title . ' (Copy)',
'post_content' => $post->post_content,
'post_excerpt' => $post->post_excerpt,
'post_status' => 'draft',
'post_type' => $post->post_type,
'post_author' => get_current_user_id(),
] );
// Copy taxonomies
$taxonomies = get_object_taxonomies( $post->post_type );
foreach ( $taxonomies as $taxonomy ) {
$terms = wp_get_object_terms( $post_id, $taxonomy, ['fields' => 'ids'] );
wp_set_object_terms( $new_post_id, $terms, $taxonomy );
}
// Copy meta fields
$meta = get_post_meta( $post_id );
foreach ( $meta as $key => $values ) {
foreach ( $values as $value ) {
add_post_meta( $new_post_id, $key, maybe_unserialize( $value ) );
}
}
wp_redirect( admin_url( 'post.php?action=edit&post=' . $new_post_id ) );
exit;
}
add_action( 'admin_action_duplicate_post_as_draft', 'duplicate_post_as_draft' );
// new code