Skip to content

Instantly share code, notes, and snippets.

View utkrishta's full-sized avatar

Utkrishta Adhikari utkrishta

View GitHub Profile
@utkrishta
utkrishta / template.php
Last active June 15, 2020 23:42
Replacing ACF with default WordPress post meta
<!--Accordion Section-->
<?php
/*
* This is an example of ACF repeater field whose name is "Content Accordion"
* and has sub fields: "Accordion Title" & "Accordion Content"
*/
$accordions = get_post_meta( get_the_ID(), 'content_accordion', TRUE );
if ( $accordions ) {
?>
<div class="container">
@utkrishta
utkrishta / custom.php
Last active June 15, 2020 23:32
Hide Given ACF fields on custom post type
<?php
//Hide given ACF fields on "my_cpt" post_type
function my_custom_function() {
global $current_screen;
if ( 'my_cpt' == $current_screen->post_type ) {
?>
<script>
(function () {
var x = document.querySelectorAll("a[data-key='field_5ea76a5ac9b48']");
//change data-key as required
@utkrishta
utkrishta / admin.php
Last active July 27, 2020 00:16
Load/Enqueue custom CSS for WP Admin Dashboard
<?php
// Update CSS within Admin Dashboard
function admin_style() {
wp_enqueue_style( 'admin-styles', get_template_directory_uri() . '/css/admin.min.css' );
//change filename and/or url as required
}
add_action( 'admin_enqueue_scripts', 'admin_style' ); //load on admin pages
add_action('login_enqueue_scripts', 'admin_style'); //load on login page
?>
@utkrishta
utkrishta / admin.php
Last active June 15, 2020 23:37
Add CLASS to admin body if user is not an Administrator
<?php
//Add class to admin body if user is not an Administrator
if( ! current_user_can('administrator') ) {
add_filter( 'admin_body_class', 'rw_admin_only_body_class' );
function rw_admin_only_body_class( $classes )
{
$classes = 'admin-readonly';
return $classes;
}
}
@utkrishta
utkrishta / disable-acf.php
Created June 15, 2020 23:41
Disable ACF on front end of the website
<?php
/**
* Disable ACF on Frontend
*/
function disable_acf_on_frontend( $plugins ) {
if ( is_admin() ) {
return $plugins;
}
@utkrishta
utkrishta / acf-no-space.php
Created June 15, 2020 23:47
Remove WhiteSpace and '&nbsp;' at start and end from ACF fields
<?php
//Remove white space at start and end and remove &nbsp; from ACF fields
function my_acf_update_value( $value, $post_id, $field ) {
if( is_string($value) ) {
$value = rtrim($value);
$value = ltrim($value);
$value = preg_replace('/&nbsp;/', '', $value);
}
return $value;
}
@utkrishta
utkrishta / admin-notification.php
Created June 15, 2020 23:51
Custom Notification in WP Admin
<?php
//add custom notification in WP Admin
function general_admin_notice(){
global $pagenow;
if ( $pagenow == 'post.php' ) { // add/edit pages/files as required
//add any custom message here
$current_user = wp_get_current_user();
$user_name = $current_user->user_firstname;
echo '<div class="notice notice-warning">
<p>Hey <strong>'. $user_name . '.</strong> You are awesome!</p>
@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>';
}
@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 / 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>