Skip to content

Instantly share code, notes, and snippets.

View samkent's full-sized avatar
🤘
Coding my socks off!

Sam Kent samkent

🤘
Coding my socks off!
View GitHub Profile
@samkent
samkent / wordpress_get_post_timestamp.php
Last active August 13, 2021 08:38
How to get the published and modified date with the WordPress function get_post_timestamp() and then convert it into a readable format
<?php
/**
* Get published and modified date with the WordPress function get_post_timestamp() and convert it into a readable format
* https://developer.wordpress.org/reference/functions/get_post_timestamp
*/
function published_modified_date() {
// UNIX published date
$unix_published_date = get_post_timestamp( '', 'date' );
@samkent
samkent / add-google-fonts-to-wordpress.php
Last active November 4, 2019 12:04
Add Google Fonts to WordPress
<?php
/**
* Add Google Fonts to WordPress
* Add this to your functions.php file
*/
function add_google_fonts() {
wp_enqueue_style( 'textdomain-add-google-fonts', 'https://fonts.googleapis.com/css?family=Prata&display=swap', false );
}
add_action( 'wp_enqueue_scripts', 'add_google_fonts' );
@samkent
samkent / add-body-class-if-wordpress-page-template.php
Created July 16, 2019 19:56
Add body class if WordPress page template
<?php
/**
* Add body class if WordPress page template
* @link https://developer.wordpress.org/reference/functions/body_class/
*/
add_filter( 'body_class', 'custom_class' );
function custom_class( $classes ) {
if ( is_page_template( 'page-example.php' ) ) {
$classes[] = 'example';
}
@samkent
samkent / dequeue-tribe-events-styles-scripts.php
Last active April 29, 2024 22:17
Dequeue Tribe Events (The Events Calendar) scripts and styles if not calendar or event page
<?php
/**
* Detect Tribe Events page
* @link https://wordpress.stackexchange.com/questions/340515/writing-a-function-to-detect-an-event
*/
function is_tribe_calendar() {
if (tribe_is_event() || tribe_is_event_category() || tribe_is_in_main_loop() || tribe_is_view() || 'tribe_events' == get_post_type() || is_singular( 'tribe_events' )) {
return true;
}
else {
@samkent
samkent / functions.php
Last active March 24, 2020 16:04
Add Google Analytics to WordPress head (header.php)
<?php
/**
* Add Google Analytics
* Add this to your theme functions.php file
* @link https://gist.github.com/samkent/1df2429b6505c272c7a6bf5799844840
*/
add_action('wp_head', 'google_analytics');
function google_analytics() {
?>
@samkent
samkent / wordpress_the_title_methods.php
Last active July 17, 2016 12:25
Multiple methods for rendering the_title(); in WordPress
<!-- Method one -->
<h1><?php the_title(); ?></h1>
<!-- Method two -->
<?php the_title( '<h1>', '</h1>' ); ?>
@samkent
samkent / remove-wordpress-emojicons.php
Last active November 4, 2019 11:51
Remove WordPress emojicons
<?php
/**
* Remove emojicons
* Add the following to your theme functions.php file.
* @link http://wordpress.stackexchange.com/questions/185577/disable-emojicons-introduced-with-wp-4-2
*/
function disable_wp_emojicons() {
remove_action( 'admin_print_styles', 'print_emoji_styles' );
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
@samkent
samkent / wordpress_page_template_body_class.php
Last active June 28, 2018 19:27
WordPress add body class if page template
<?php
/**
* Add a custom body class for a specific page template
* Add the following to your theme functions.php file.
*/
add_filter( 'body_class', 'custom_class' );
function custom_class( $classes ) {
if ( is_page_template( 'templates/page-contact.php' ) ) {
$classes[] = 'contact';
}
@samkent
samkent / contact-form-7-cleanup.php
Last active August 29, 2015 14:14
WordPress Contact Form 7 only enqueue if page.
/**
* Contact Form 7 only enqueue if page
*
* @author Sam Kent
* @link http://www.samkent.com
*/
function contact_form_7_cleanup() {
// Dequeue scripts and styles
wp_dequeue_script( 'contact-form-7' );
wp_dequeue_style( 'contact-form-7' );