Skip to content

Instantly share code, notes, and snippets.

View utkrishta's full-sized avatar

Utkrishta Adhikari utkrishta

View GitHub Profile
@utkrishta
utkrishta / scripts.js
Created May 30, 2022 05:58
Pure Javascript to limit(hide) characters and show on clikc
window.addEventListener('load', function() {
const testimonialItems = document.querySelectorAll('.testimonial-item');
testimonialItems.forEach(testimonialItem => {
var maxLength = 200;
const textLength = testimonialItem.innerText.length;
if(textLength > maxLength) {
var shortContent = testimonialItem.innerText.substring(0, maxLength); /*split the content in two parts*/
var longContent = testimonialItem.innerText.substring(maxLength);
testimonialItem.innerHTML = shortContent + '<i class="read_more">...<strong> Read More</strong></i>';
const readbtn = testimonialItem.querySelectorAll('.read_more');
@utkrishta
utkrishta / functions.php
Created April 8, 2022 01:49
Create a custom shortcode that calls and display content from another file/tempalate part
<?php
/*
* Create a custom shortcode that calls and display content from another file/tempalate part
*
* We have a file called custom-form.php inside inc folder which contains our custom form codes
*
*/
function custom_form( $atts, $content, $shortcode_tag ){
ob_start();
get_template_part('inc/custom-form');
@utkrishta
utkrishta / contact.php
Created March 3, 2022 23:36
Display Days of the week and highlight current day
<div class="opening-hours">
<div class="row">
<div class="col-12"><h4>Our Business Hours</h4></div>
</div>
<div class="row" id="Sunday">
<div class="col-6 day">Sunday</div>
<div class="col-6 text-md-end hours">Open 24 Hrs</div>
</div>
<div class="row" id="Monday">
<div class="col-6 day">Monday</div>
<?php
/*
* We are using rank_math_locations custom post type. (or any other)
* Meta Box to create custom fields which saves in its own database table
* We have location metabox custom field saved in table "mbox_locations" and
* service type custom field saved in table "mbox_services"
*
* mbox_locations contains a sub-field(toggle) called capital-cities
* mbox_services contains a sub-field(radio) called service-type
*/
@utkrishta
utkrishta / metabox-repeater-fields.php
Created February 14, 2022 23:35
Displaying field values from metabox cloneable(repeater) fields
<?php
//get repeater field called 'services'
$services = rwmb_meta( 'services' );
if ( ! empty( $services ) ) {
foreach ( $services as $service ) {
//retrieve each values from subfield inside the services field
echo wp_get_attachment_image( $service['services_image'], 'full' );
echo $service['services_title'];
echo $service['services_description'];
//there is a repeater subfield within this called 'services_links'
@utkrishta
utkrishta / krish-related-posts.php
Created June 30, 2021 06:59
Plugin to add related posts within the contents
@utkrishta
utkrishta / get_filetime.php
Created October 14, 2020 00:48
Functions to get filetime in PHP and Javasript
<?php
$fileName = get_template_directory() . '/css/theme.css';
$modifiedDate = filemtime($fileName);
echo '<script>console.log(' . $modifiedDate . ')</script>';
?>
<script>
var fileName = "<?=home_url()?>/wp-content/themes/jims-aircon/css/theme.css";
var req = new XMLHttpRequest();
req.open("HEAD", fileName, false);
req.send(null);
@utkrishta
utkrishta / custom-cpt.php
Created July 27, 2020 00:33
Custom Post Types with taxonomy in the permalink
<?php
/*
In this example, I've created custom post-types called "locations" and custom taxonomy called "state"
By default the generated permalink structure would look like this:
Locations: <base-url>/locations/<post-slug>/
State: <base-url>/state/<taxonomy-slug>/
But here is what I wanted:
1. Replace "locations" with "service-areas" in the permalink for locaitons.
2. Permalink structure for "locations" to be: <base-url>/service-areas/<state-slug>/<locations-slug>
@utkrishta
utkrishta / remove-wp-bloat.php
Created June 16, 2020 00:16
Remove WordPress Bloat
<?php
/*
* Remove WP Bloat
* Please Note: This removes WP bloat for better performance. Remove or add feaures as necessary
*/
remove_action( 'wp_head', 'wp_shortlink_wp_head', 10 );
remove_action( 'template_redirect', 'wp_shortlink_header', 11 );
remove_action('wp_head', 'rsd_link'); //removes EditURI/RSD (Really Simple Discovery) link.
remove_action('wp_head', 'wlwmanifest_link'); //removes wlwmanifest (Windows Live Writer) link.
@utkrishta
utkrishta / dashboard.php
Created June 15, 2020 23:53
Add custom message box in Dashboard
<?php
//add custom message box in Dashboard
add_action('wp_dashboard_setup', 'custom_dashboard_widgets');
function custom_dashboard_widgets() {
wp_add_dashboard_widget('custom_dashboard_id', 'Custom Theme Support', 'custom_dashboard_message');
}
function custom_dashboard_message() {
echo '<p>Welcome to your Awesome WordPress Theme.</p>';
}