Skip to content

Instantly share code, notes, and snippets.

@junaidtk
junaidtk / WP REST API Custom End Point
Created October 18, 2018 17:45
REST API greeting using custom end point
We can create custom end point by using the register_rest_route() function. This function is need to call in rest_api_init hooks.
For this function register_rest_route(), the first argument is namespace, 2nd one is resource path or resource base and 3rd one is array option.
In array optuion we specify what method the end point can use and what call back is happen when an end point is matched.
/**
* This is our callback function that embeds our phrase in a WP_REST_Response
*/
function prefix_get_endpoint_phrase() {
@junaidtk
junaidtk / WP REST API register_rest_field().
Created October 17, 2018 17:15
How to add meta field to REST API Response of a WordPress Posts using register_rest_field?
You can use the register_rest_field() function to add additional meta key to the REST API
eg:
add_action( 'rest_api_init', 'create_api_posts_meta_field' );
function create_api_posts_meta_field() {
// register_rest_field ( 'name-of-post-type', 'name-of-field-to-return', array-of-callbacks-and-schema() )
register_rest_field( 'post', 'banner_image', array(
'get_callback' => 'get_post_meta_for_api',
@junaidtk
junaidtk / WP REST API
Created October 17, 2018 17:03
How to add meta field to REST API Response of a WordPress Posts using register_meta ?
You can use two methods for adding the new meta field to Rest API. By using register_meta() function and register_rest_field() function.
1)register_meta() function.
By using this function you can add additional meta key to API response by adding the 'show_in_rest' => true, parameter to arguments.
eg:
$object_type = 'post';
$args1 = array( // Validate and sanitize the meta value.
// Note: currently (4.7) one of 'string', 'boolean', 'integer',
// 'number' must be used as 'type'. The default is 'string'.
@junaidtk
junaidtk / gist:f64ff6a8ccfa996593053df73691eddc
Created October 3, 2018 14:36
Convert http request to https
We need to add the below code in between <IfModule mod_rewrite.c>
# Rewrite HTTP to HTTPS
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*) https://%{SERVER_NAME}/$1 [R,L]
The above code will load the https url instead of http
@junaidtk
junaidtk / php.ini
Created July 10, 2018 06:25
To increase max upload size you can use following methods
1) You can use the .htaccess file to increase the file upload size of the server.
Add below codes in the server .htaccess file of the site
php_value upload_max_filesize 128M
php_value post_max_size 128M
php_value max_execution_time 300
php_value max_input_time 300
2)if htaccess trick does't work you edit the php.ini file of the server.
@junaidtk
junaidtk / How to edit
Last active July 9, 2018 17:43
How to configure htaccess file
Htaccess file is used to redirect the url rewriting and also for making the SEO friendly URL.
Commomly used htaccess codes are listed below.
RewriteEngine on # This is to make the rewrite engin ON
RewriteCond and RewriteRule: => are the commonly used commands to write the htaccess codes.
"RewriteRule" :- It will redirect the current url to the required url.
"RewriteRule ^test/?$ test.php [NC,L] " # it will redirect to the test to the test.php