-
Notifications
You must be signed in to change notification settings - Fork 18
/
functions.php
150 lines (116 loc) · 3.67 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
148
149
150
<?php
/**
* THIS IS TO UPDATE THE POST / PAGE PREVIEW
* USES THE COMPONENT "PREVIEW"
*/
//change preview post links - to match wpvue route
function new_preview_link() {
$slug = basename(get_the_ID());
$mydir = '/preview/';
$mynewpurl = "$mydir$slug";
return "$mynewpurl";
}
add_filter( 'preview_post_link', 'new_preview_link' );
//adding custom route for preview
add_action( 'rest_api_init', 'my_custom_endpoints' );
function my_custom_endpoints() {
register_rest_route( 'wpvue', '/preview', array(
'methods' => 'GET',
'callback' => 'post_preview_callback',
));
}
function post_preview_callback( $request_data ) {
// endpoint looks like: /wp-json/wpvue/preview?id=98
$parameters = $request_data->get_params();
$preview = wp_get_post_autosave($parameters[id]);
$url = get_bloginfo('url').'/wp-json/wp/v2/posts?id='. $parameters[id];
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$postarray = json_decode($result);
if(count($postarray) > 0){
$postarray[0]->title->rendered = $preview->post_title;
$postarray[0]->content->rendered = $preview->post_content;
}else{
throw new Exception('No record found');
}
return $postarray;
}
/**
* END POST / PAGE PREVIEW
*
*/
/**
* CHANGES URL PREFIX
* TO MATCH OUR VUE ROUTE
* SO A PAGE WILL LOOK LIKE
* /post/post-name/
* /page/page-name/
* https://stackoverflow.com/questions/17613789/wordpress-rewrite-add-base-prefix-to-pages-only *
*
*/
function change_base_permalinks() {
global $wp_rewrite;
// $wp_rewrite->permalink_structure = 'post/%postname%/';
$wp_rewrite->page_structure = 'page/%pagename%';
// $wp_rewrite->extra_permastructs['category']['struct'] = 'category/%category%';
$wp_rewrite->flush_rules();
// var_dump($wp_rewrite);exit;
//look into this:
//https://wordpress.stackexchange.com/questions/152306/change-permalinks-structure-for-specific-category
}
add_action('init','change_base_permalinks');
function prepare_rest($data,$post,$request){
$_data = $data->data;
//Categories
$cats = get_the_category($post->ID);
$_data['cats'] = $cats;
//Back to data
$data->data = $_data;
return $data;
}
add_filter('rest_prepare_post', 'prepare_rest', 10,3);
//ADDS FILTER TO QUERY PARAMETER
add_action( 'rest_api_init', 'rest_api_filter_add_filters' );
/**
* Add the necessary filter to each post type
**/
function rest_api_filter_add_filters() {
foreach ( get_post_types( array( 'show_in_rest' => true ), 'objects' ) as $post_type ) {
add_filter( 'rest_' . $post_type->name . '_query', 'rest_api_filter_add_filter_param', 10, 2 );
}
}
/**
* Add the filter parameter
*
* @param array $args The query arguments.
* @param WP_REST_Request $request Full details about the request.
* @return array $args.
**/
function rest_api_filter_add_filter_param( $args, $request ) {
// Bail out if no filter parameter is set.
if ( empty( $request['filter'] ) || ! is_array( $request['filter'] ) ) {
return $args;
}
$filter = $request['filter'];
if ( isset( $filter['posts_per_page'] ) && ( (int) $filter['posts_per_page'] >= 1 && (int) $filter['posts_per_page'] <= 100 ) ) {
$args['posts_per_page'] = $filter['posts_per_page'];
}
global $wp;
$vars = apply_filters( 'query_vars', $wp->public_query_vars );
foreach ( $vars as $var ) {
if ( isset( $filter[ $var ] ) ) {
$args[ $var ] = $filter[ $var ];
}
}
return $args;
}
/**
* below will allow comments via rest api
*/
function allow_anonymous_comments() {
return true;
}
add_filter('rest_allow_anonymous_comments','allow_anonymous_comments');
?>