forked from EvanAgee/vuejs-wordpress-theme-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.php
147 lines (122 loc) · 5.22 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
// Remove all default WP template redirects/lookups
remove_action('template_redirect', 'redirect_canonical');
// Redirect all requests to index.php so the Vue app is loaded and 404s aren't thrown
function remove_redirects() {
add_rewrite_rule('^/(.+)/?', 'index.php', 'top');
}
add_action('init', 'remove_redirects');
// Load scripts
function load_vue_scripts() {
wp_enqueue_style('blankslate/app.css', get_template_directory_uri() . '/dist/styles/app.css', false, null);
wp_enqueue_script('blankslate/manifest.js', get_template_directory_uri() . '/dist/scripts/manifest.js', null, null, true);
wp_enqueue_script('blankslate/vendor.js', get_template_directory_uri() . '/dist/scripts/vendor.js', null, null, true);
wp_enqueue_script('blankslate/app.js', get_template_directory_uri() . '/dist/scripts/app.js', null, null, true);
}
add_action('wp_enqueue_scripts', 'load_vue_scripts', 100);
function acf_rest_api_admin_notice__warning() {
if (!is_plugin_active('acf-to-rest-api/class-acf-to-rest-api.php')) { ?>
<div class="notice error is-dismissible">
<p><?php esc_html_e( 'The theme', 'act-to-rest-api' ); ?> <?php esc_html_e( 'depends on ACF to REST API to work!', 'acf-to-rest-api' ); ?></p>
<p><a href="<?php echo esc_url(wp_nonce_url( self_admin_url( 'update.php?action=install-plugin&plugin=acf-to-rest-api' ))); ?>" class="button button-primary"<?php if ( $target ) : ?> target="_blank"<?php endif; ?>><?php esc_html_e( $action . 'Install ACF to REST API', 'acf-to-rest-api' ); ?></a></p>
</div>
<?php }
}
add_action( 'admin_notices', 'acf_rest_api_admin_notice__warning' );
function menu_rest_api_admin_notice__warning() {
if (!is_plugin_active('wp-api-menus/wp-api-menus.php')) { ?>
<div class="notice error is-dismissible">
<p><?php esc_html_e( 'The theme', 'act-to-rest-api' ); ?> <?php esc_html_e( 'depends on WP API Menus to work!', 'wp-api-menus' ); ?></p>
<p><a href="<?php echo esc_url(wp_nonce_url( self_admin_url( 'update.php?action=install-plugin&plugin=wp-api-menus' ))); ?>" class="button button-primary"<?php if ( $target ) : ?> target="_blank"<?php endif; ?>><?php esc_html_e( $action . ' Install WP API Menus', 'wp-api-menus' ); ?></a></p>
</div>
<?php }
}
add_action( 'admin_notices', 'menu_rest_api_admin_notice__warning' );
add_theme_support( 'menus' );
register_nav_menus( array(
'header_menu' => __( 'Header Menu' ),
'footer_menu' => __( 'Footer Menu' )
));
// Custom Logo
function theme_prefix_setup() {
add_theme_support( 'custom-logo', array(
'flex-width' => true,
) );
}
add_action( 'after_setup_theme', 'theme_prefix_setup' );
function theme_prefix_the_custom_logo() {
if ( function_exists( 'the_custom_logo' ) ) {
the_custom_logo();
}
}
add_action( 'rest_api_init', function () {
register_rest_route( 'theme/v1', '/logo/url', array(
'methods' => 'GET',
'callback' => 'get_logo_url',
) );
register_rest_route( 'theme/v1', '/logo/width', array(
'methods' => 'GET',
'callback' => 'get_logo_width',
) );
register_rest_route( 'theme/v1', '/logo/height', array(
'methods' => 'GET',
'callback' => 'get_logo_height',
) );
register_rest_route( 'static/v1', '/frontpage', array(
'methods' => 'GET',
'callback' => 'get_frontpage',
) );
register_rest_route( 'static/v1', '/posts_page', array(
'methods' => 'GET',
'callback' => 'get_posts_page',
) );
} );
function get_logo( $data ) {
$custom_logo_id = get_theme_mod( 'custom_logo' );
$image = wp_get_attachment_image_src( $custom_logo_id , 'full' );
if ( empty( $image ) ) {
return new WP_Error( 'no_logo', 'No logo uploaded. Please upload a theme logo: Wp Admin > Appearance > Customize', array( 'status' => 404 ) );
}
return $image;
}
function get_logo_url( $data ) {
$custom_logo_id = get_theme_mod( 'custom_logo' );
$image = wp_get_attachment_image_src( $custom_logo_id , 'full' );
if ( empty( $image ) ) {
return new WP_Error( 'no_logo', 'No logo uploaded. Please upload a theme logo: Wp Admin > Appearance > Customize', array( 'status' => 404 ) );
}
$url = $image[0];
return $url;
}
function get_logo_width( $data ) {
$custom_logo_id = get_theme_mod( 'custom_logo' );
$image = wp_get_attachment_image_src( $custom_logo_id , 'full' );
if ( empty( $image ) ) {
return new WP_Error( 'no_logo', 'No logo uploaded. Please upload a theme logo: Wp Admin > Appearance > Customize', array( 'status' => 404 ) );
}
$width = $image[1];
return $width;
}
function get_logo_height( $data ) {
$custom_logo_id = get_theme_mod( 'custom_logo' );
$image = wp_get_attachment_image_src( $custom_logo_id , 'full' );
if ( empty( $image ) ) {
return new WP_Error( 'no_logo', 'No logo uploaded. Please upload a theme logo: Wp Admin > Appearance > Customize', array( 'status' => 404 ) );
}
$height = $image[2];
return $height;
}
function get_frontpage($data) {
$page = get_option( 'page_on_front' );
if ( empty( $page ) ) {
return new WP_Error( 'no_frontpage', 'No frontpage. Please select a static Homepage via: Wp Admin > Settings > Reading', array( 'status' => 404 ) );
}
return $page;
}
function get_posts_page($data) {
$page = get_option( 'page_for_posts' );
if ( empty( $page ) ) {
return new WP_Error( 'no_posts_page', 'No posts page. Please select a static Posts page via: Wp Admin > Settings > Reading', array( 'status' => 404 ) );
}
return $page;
}