forked from pixelgrade/patch-child
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
executable file
·131 lines (92 loc) · 3.83 KB
/
functions.php
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
/**
* Patch Child functions and definitions
*
* Bellow you will find several ways to tackle the enqueue of static resources/files
* It depends on the amount of customization you want to do
* If you either wish to simply overwrite/add some CSS rules or JS code
* Or if you want to replace certain files from the parent with your own (like style.css or main.js)
*
* @package PatchChild
*/
/**
* Setup Patch Child Theme's textdomain.
*
* Declare textdomain for this child theme.
* Translations can be filed in the /languages/ directory.
*/
function patch_child_theme_setup() {
load_child_theme_textdomain( 'patch-child-theme', get_stylesheet_directory() . '/languages' );
}
add_action( 'after_setup_theme', 'patch_child_theme_setup' );
/**
*
* 1. Add a Child Theme "style.css" file
* ----------------------------------------------------------------------------
*
* If you want to add static resources files from the child theme, use the
* example function written below.
*
*/
function patch_child_enqueue_styles() {
$theme = wp_get_theme();
// use the parent version for cachebusting
$parent = $theme->parent();
if ( !is_rtl() ) {
wp_enqueue_style( 'patch-style', get_template_directory_uri() . '/style.css', array(), $parent->get( 'Version' ) );
} else {
wp_enqueue_style( 'patch-style', get_template_directory_uri() . '/rtl.css', array(), $parent->get( 'Version' ) );
}
// Here we are adding the child style.css while still retaining
// all of the parents assets (style.css, JS files, etc)
wp_enqueue_style( 'patch-child-style',
get_stylesheet_directory_uri() . '/style.css',
array('patch-style') //make sure the the child's style.css comes after the parents so you can overwrite rules
);
}
add_action( 'wp_enqueue_scripts', 'patch_child_enqueue_styles' );
/**
*
* 2. Overwrite Static Resources (eg. style.css or main.js)
* ----------------------------------------------------------------------------
*
* If you want to overwrite static resources files from the parent theme
* and use only the ones from the Child Theme, this is the way to do it.
*
*/
/*
function patch_child_overwrite_files() {
// 1. The "main.js" file
//
// Let's assume you want to completely overwrite the "main.js" file from the parent
// First you will have to make sure the parent's file is not loaded
// See the parent's function.php -> the patch_scripts_styles() function
// for details like resources names
//
// Remember that the rest of the static resources will still get loaded like
// patch-imagesloaded, patch-hoverintent and patch-velocity
wp_dequeue_script( 'patch-scripts' );
// We will add the main.js from the child theme (located in assets/js/main.js)
// with the same dependecies as the main.js in the parent
// This is not required, but I assume you are not modifying that much :)
wp_enqueue_script( 'patch-child-scripts',
get_stylesheet_directory_uri() . '/assets/js/main.js',
array( 'jquery', 'masonry', 'patch-imagesloaded', 'patch-hoverintent', 'patch-velocity' ),
'1.0.0', true );
// 2. The "style.css" file
//
// First, remove the parent style files
// see the parent's function.php -> the patch_scripts_styles() function for details like resources names
wp_dequeue_style( 'patch-style' );
// Now you can add your own, modified version of the "style.css" file
wp_enqueue_style( 'patch-child-style',
get_stylesheet_directory_uri() . '/style.css',
array('patch-font-awesome-style') //use the same dependencies as the parent because we want to still use FontAwesome icons
);
}
// Load the files from the function mentioned above:
add_action( 'wp_enqueue_scripts', 'patch_child_overwrite_files', 11 );
// Notes:
// The 11 priority parameter is need so we do this after the function in the parent so there is something to dequeue
// The default priority of any action is 10
*/