Using WordPress as a headless system, is nothing new. You can easily build out your own REST API or use the long available HTTP REST API 1.0+ for WordPress.
But with the new HTTP REST API 2.0+ its getting really easy to build out your own REST API Namespace and assign routes for all your JSON needs.
The new REST API will make its appearance in WordPress 4.4, but you can start now by adding the plugin to your system. When the plugin detects 4.4+ it will only load functionality not already present in the core.
A route tells the API to respond to a given request with a specific function (endpoint). This adds the permalink structure to WordPress so that your functionality can be accessed via an url like this: http://yourdoman.com/wp-json/yourplugin/v1/myfunction/your_parameter
1 2 3 4 5 6 |
add_action( 'rest_api_init', function () { register_rest_route( 'your_namespace/v1', '/your_endpoint/(?P<your_parameter>\d+)', array( 'methods' => 'GET', 'callback' => 'your_endpoint', ) ); } ); |
The Endpoint is the callback handled by the route. Each endpoint can have additional parameters attached:
The return values from the endpoint are converted into JSON. You can use the WP_REST_Response object , this wraps normal body data and allows you to return a custom status code.
1 2 3 4 5 6 7 8 9 10 11 |
function your_endpoint( $data ) { $posts = get_posts( array( 'author' => $data['your_parameter'], ) ); if ( empty( $posts ) ) { return new WP_Error( 'your_endpoint_no_data', 'No Data', array( 'status' => 404 ) ); } return new WP_REST_Response( $posts, 200 ); } |
Really powerful stuff, that makes it easy to detach WordPress and the actual client using the data provided by it.
You can read more about it here.
Some options to get those translatable strings out of your templates. I will be adding new things, as I discover them :)
Timber / Twig templates are normally not recognized by Poedit. Its just a matter of tweaking the C/C++ extractor within the settings panel.
Add TWIG to the filetypes: *.c;*.cpp;*.h;*.hpp;*.cc;*.C;*.cxx;*.hxx;*.twig
Gettext calls, like {{__(„Points Conversion Rate“, „wplms-extras“)}} in Timber, are being extracted from your templates after that.
Easier than using the gettext extractor :)
1 2 3 4 5 |
$function = new Twig_SimpleFunction('__', function ($args) { return __($args); }); $twig->addFunction($function); |
I am currently working on a WPLMS enhancement for a customer, that allows to simplify the payout of instructor commissions. The whole system runs on the MyCred Points System and students pay for courses with Points. The problem is how to easily payout the instructor commissions via PayPal.
There is currently no addon for MyCred available that does that magic, so I build one myself.
At the moment the payment process via PayPal is completely manual, due to budget constraints. I am basically generating a custom „Send Money“ link that prefills the PayPal email and amount to send.
The interface itself handles the payout sessions, tracks the instructor balance, paid and unpaid points.
Here some images to illustrate the admin dashboard:
This list the instructors and their point balance and allows to start the payment process.
Payout sessions make sure, that only one session can be started per instructor, as the instructor could earn new points during the process. The points converted can be changed, allowing you to payout a fixed amount of points.
Its a 3 step process. Login at PayPal. Open the „Send money“ dialog and send money to instructor. Confirm that you manually send the money and than register the payment and payout points in the system.
The session can be cancelled at any point. You can also leave the session open and continue at a later point.
On the frontend I added an interface to the BuddyPress Profile, that allows the instructor to track his payouts and balance.
The whole setup could be updated using PayPal Adaptive Payments, to make the whole process completely automated. Something to consider for the future :) Pretty happy with the manual process so far and it will be a great help for my customer to keep track of the commission payouts.
The whole setup is currently targeted for WPLMS, but can easily be adapted to other setups using the MyCred Points System.
HyperDB is a plugin for spreading your websites load across several servers and databases. Its currently used in production on WordPress.com.
Just started experimenting with it :)
Since version 5.6+ PHP is verifying peer certificates and host names by default when using SSL/TLS. This is causing problems on some servers / websites, where the config has not been setup correctly. If you can not fix the setup yourself, make sure to talk to your server host to fix that issue.
For PHPMailer (Github) there is a workaround:
1 2 3 4 5 6 7 |
$mail->SMTPOptions = array( 'ssl' => array( 'verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true ) ); |
This should only be a workaround until your configuration has been fixed. You are suppressing certificate verification and compromising your security!
As WordPress is using PHPMailer as its main email library, this can be tweaked by using the phpmailer_init hook:
1 2 3 4 5 6 7 8 9 10 11 |
add_action( 'phpmailer_init', 'configure_smtp' ); function configure_smtp( $phpmailer ){ $phpmailer->SMTPOptions = array( 'ssl' => array( 'verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true ) ); } |
Add this to your themes functions.php.
1 2 3 4 5 6 7 8 9 |
$phpmailer = new PHPMailer(); $phpmailer->isSMTP(); $phpmailer->CharSet = 'UTF-8'; $phpmailer->SMTPSecure = "ssl"; // or tls $phpmailer->SMTPAuth = true; $phpmailer->Username = _APP_EMAIL_FROM; $phpmailer->Password = _APP_EMAIL_PASS; $phpmailer->XMailer =' '; $phpmailer->Host= "correct email host"; |
And here is how phpmailer->smtpOptions should be used, on a properly configured server:
1 2 3 4 5 6 7 8 9 |
phpmailer->smtpOptions = array( 'ssl' => array( 'peer_name' => 'your.domain.com', 'verify_peer_name' => true, 'capath' => '/path/to/authority/certificates/directory' # Usually /etc/ssl/certs or /usr/lib/ssl/certs/ 'local_cert' => '/path/to/your.domain.com.crt', # Should be a combined cert & key in pem format 'verify_peer' => true, ) ); |
SSL changes in PHP 5.6: http://php.net/manual/en/migration56.openssl.php
SSL context options in PHP: http://php.net/manual/en/context.ssl.php
Enjoy coding…
Chrome 45+ is glitching on WordPress admin menus.
1 2 3 4 5 6 7 8 9 |
function chromefix_inline_css() { if ( strpos( $_SERVER['HTTP_USER_AGENT'], 'Chrome' ) !== false ) { wp_add_inline_style( 'wp-admin', '#adminmenu { transform: translateZ(0); }' ); } } add_action('admin_enqueue_scripts', 'chromefix_inline_css'); |
WooCommerce provides great functionality, but loads a lot of resources even if not needed.
If a site loads longer than 5-7 seconds, potential customers already loose interest :)
Add this to your 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 |
add_action( 'wp_enqueue_scripts', 'cleanup_woocommerce_includes', 99 ); function cleanup_woocommerce_includes(){ //check that woo exists if ( function_exists( 'is_woocommerce' ) ) { if ( ! is_woocommerce() && ! is_cart() && ! is_checkout() ) {} /*Styles*/ wp_dequeue_style( 'woocommerce_frontend_styles' ); wp_dequeue_style( 'woocommerce_fancybox_styles' ); wp_dequeue_style( 'woocommerce_chosen_styles' ); wp_dequeue_style( 'woocommerce_prettyPhoto_css' ); wp_dequeue_style( 'woocommerce-layout' ); wp_dequeue_style( 'woocommerce-smallscreen' ); wp_dequeue_style( 'woocommerce-general' ); wp_dequeue_style( 'wc-bto-styles' ); /*Scripts*/ wp_dequeue_script( 'wc-cart' ); wp_dequeue_script( 'wc-cart-fragments' ); wp_deregister_script( 'wc-add-to-cart' ); wp_dequeue_script( 'wc-add-to-cart' ); wp_deregister_script( 'wc-add-to-cart-variation' ); wp_dequeue_script( 'wc-add-to-cart-variation' ); wp_dequeue_script( 'wc-checkout' ); wp_dequeue_script( 'wc-single-product' ); wp_dequeue_script( 'jquery-blockui' ); wp_dequeue_script( 'wc_price_slider' ); wp_dequeue_script( 'wc-chosen' ); wp_dequeue_script( 'woocommerce' ); wp_dequeue_script( 'prettyPhoto' ); wp_dequeue_script( 'prettyPhoto-init' ); wp_dequeue_script( 'jquery-placeholder' ); wp_dequeue_script( 'fancybox' ); wp_dequeue_script( 'jqueryui' ); } } } |
Like always, make sure that nothing breaks. If things break or are required by certain pages add an exception for that!
Check WordPress Condional Tags.
Sources:
1 2 |
remove_action( 'wp_head', array( $GLOBALS['woocommerce'], 'generator' ) ); remove_action('wp_head', 'wc_generator_tag'); |
Simple add this to your wp-config.php
1 2 |
define('WP_SITEURL', 'http://' . $_SERVER['HTTP_HOST']); define('WP_HOME', 'http://' . $_SERVER['HTTP_HOST']); |
Use the MU-Domain-Mapping plugin for that.
We often have some posts that we would like to promote and keep at the first page of the blog. When you are using pagination, the sticky posts will be added to the standard posts, making the post count per page uneven.
If you want to keep your posts per page count consistent, there is a way to do that.
1 2 3 4 5 6 7 8 9 10 11 |
global $paged; if (!isset($paged) || !$paged){ $paged = 1; } // Set posts per page $posts_per_page = 8; // Get sticky posts $sticky = get_option( 'sticky_posts' ); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
// First page and has stickies if($paged == 1 && !empty($sticky)) { $args = array( 'post_type' => 'post', 'paged' => $paged, // Adjust posts per page by subtracting the amount of stickies 'posts_per_page' => $posts_per_page - count($sticky) ); }else{ $args = array( 'post_type' => 'post', 'paged' => $paged, 'posts_per_page' => $posts_per_page, // making sure the 2nd page starts with the right post 'offset' => (($paged-1)*$posts_per_page) - count($sticky) ); } // Do query query_posts($args); |