WP Snippets
Enqueue styles in the footer
Used for critical css
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function my_deregister_styles() {
wp_deregister_style('parent');
wp_deregister_style( 'divi-style' );
wp_deregister_style( 'tt-widget-css' );
}
add_action('wp_enqueue_scripts', 'my_deregister_styles', 100);
//load style on footer
function pxs_add_footer_styles() {
$parent_style = 'parent-style';
wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' );
wp_enqueue_style( 'child-style', get_stylesheet_directory_uri() . '/style.css', array('parent-style') );
wp_enqueue_style( 'tt-widget-css', get_stylesheet_directory_uri() . '/wp-content/plugins/ticket-tailor/tt-widget.css' );
};
add_action( 'get_footer', 'pxs_add_footer_styles' );
Move Yoast to bottom
1
2
3
4
5
function yoasttobottom() {
return 'low';
}
add_filter( 'wpseo_metabox_prio', 'yoasttobottom');
redirect user on failed login
1
2
3
4
5
6
7
8
9
10
11
add_action( 'wp_login_failed', 'my_callback' );
function my_callback() {
if (! is_user_logged_in()) {
wp_redirect( 'https://example.com.au', 301 );
exit();
} else {
wp_redirect( 'https://example.com.au/login/', 301 );
exit();
}
}
style header info
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/*
Theme Name: Twenty Seventeen
Theme URI: https://wordpress.org/themes/twentyseventeen/
Author: the WordPress team
Author URI: https://wordpress.org/
Description: Twenty Seventeen brings your site to life with immersive featured images and subtle animations. With a focus on business sites, it features multiple sections on the front page as well as widgets, navigation and social menus, a logo, and more. Personalize its asymmetrical grid with a custom color scheme and showcase your multimedia content with post formats. Our default theme for 2017 works great in many languages, for any abilities, and on any device.
Version: 1.0
License: GNU General Public License v2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Text Domain: twentyseventeen
Tags: one-column, two-columns, right-sidebar, flexible-header, accessibility-ready, custom-colors, custom-header, custom-menu, custom-logo, editor-style, featured-images, footer-widgets, post-formats, rtl-language-support, sticky-post, theme-options, threaded-comments, translation-ready
This theme, like WordPress, is licensed under the GPL.
Use it to make something cool, have fun, and share what you've learned with others.
*/
wp_schedule_event
secure area
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php
/* include via page template
Pipe all knowledge base users through the one page template to easily test if user is logged in or not.
*/
?>
<section class = "secure_area">
<div class = "container">
<div class = "secure_area__wrapper">
<?php if (is_user_logged_in()) { ?>
<h2>Search</h2>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; else: ?>
<?php get_template_part('notfound');
endif; ?>
<?php } else { ?>
<?php echo '<div class = "notification"><h2 class = "text">You will need to <a href = "/wp/wp-login.php?action=register">Register</a> or <a href = "/login">Login</a> to view the secure area</h2></div>'; ?>
<?php } ?>
</div>
</div>
</section>
date counter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
function DateCounter($atts){
$startDate = $atts["startdate"];
$endDate = strtolower($atts["enddate"]);
switch (strtolower($atts["format"])) {
case "year":
case "years":
$startDate = new DateTime($startDate);
$endDate = new DateTime($endDate);
$difference = $endDate->diff($startDate);
$result = $difference->y;
break;
case "month":
case "months":
$startDate = strtotime($startDate);
$endDate = strtotime($endDate);
$min_date = min($startDate, $endDate);
$max_date = max($startDate, $endDate);
$i = 0;
while (($min_date = strtotime("+1 MONTH", $min_date)) <= $max_date) {
$i++;
}
$result = $i;
break;
case "day":
case "days":
$startDate = strtotime($startDate);
$endDate = strtotime($endDate);
$difference = $endDate - $startDate;
$result = floor($difference / (60*60*24));
break;
case "currentyear":
$result = date("Y");
break;
case "currentmonth":
$result = date("m");
break;
case "currentday":
$result = date("d");
break;
}
return $result;
}
add_shortcode( 'DateCounter', 'DateCounter' );
?>
featured image
1
2
3
4
5
<?php if ( has_post_thumbnail()) {
$image_src = wp_get_attachment_image_src( get_post_thumbnail_id(), $pxs_img_size );
echo '<img src="' . $image_src[0] . '" width="100%" />'; ?>
<?php }
This post is licensed under
CC BY 4.0
by the author.