Skip to content

Instantly share code, notes, and snippets.

View harisrozak's full-sized avatar
Upgrading multi-lang module...

Haris Ainur Rozak harisrozak

Upgrading multi-lang module...
View GitHub Profile
@harisrozak
harisrozak / db-config.php
Last active October 2, 2020 04:21
WordPress HyperDB setting for multisite with 3 datasets: global, even_sites, odd_sites
<?php
/**
* HyperDB configuration file
*
* This file should be installed at ABSPATH/db-config.php
*
* $wpdb is an instance of the hyperdb class which extends the wpdb class.
*
* See readme.txt for documentation.
@harisrozak
harisrozak / wordpress-cookie.php
Last active July 3, 2021 05:48
WordPress / PHP Cookie
@harisrozak
harisrozak / request-rest-api.php
Created November 19, 2020 03:43
WordPress :: Request to REST API from PHP
<?php
// send data trough REST API
$request = new WP_REST_Request( 'POST', '/wp/v2/custom_post_type' );
$request->set_header( 'content-type', 'application/json' );
$request->set_body( wp_json_encode( $order_data ) );
// get response
$response = rest_do_request( $request );
$response_data = $response->get_data();
@harisrozak
harisrozak / wp-fix-rest-url-on-ssl.php
Last active February 5, 2021 03:15
WordPress Fix `rest_url` that isn't become https on certain server
<?php
add_filter( 'rest_url', function( $url ) {
if ( is_ssl() ) {
$url = set_url_scheme( $url, 'https' );
return $url;
}
return $url;
});
@harisrozak
harisrozak / customizer-preview-certain-page.php
Last active February 5, 2021 03:14
WordPress customizer a certain page on accessing a certain section
<?php
add_action( 'customize_controls_print_scripts', 'custom_customize_controls_print_scripts', 30 );
function custom_customize_controls_print_scripts() {
?>
<script type='text/javascript'>
jQuery( function( $ ) {
wp.customize.panel( 'customize_checkout_settings', function( section ) {
section.expanded.bind( function( isExpanded ) {
if ( isExpanded ) {
@harisrozak
harisrozak / db-config.php
Last active April 1, 2021 02:56
LudicrousDB (based on Automattic's HyperDB) configuration file that will split multisite per 100 sites based on site ID
<?php
/**
* LudicrousDB configuration file
*
* This file should be copied to ABSPATH/db-config.php and modified to suit your
* database environment. This file comes with a basic configuration by default.
*
* See README.md for documentation.
*
@harisrozak
harisrozak / multisite-on-app.php
Last active April 15, 2021 04:37
Main multisite-site on app.example.com. Also fixed `cookies` related error on child-sites login
<?php
/**
* Modify multisite childs subdomain path
* The main multisite path is on app.example.com
* And then it's children should be on foo.example.com, bar.example.com, etc.
* This code should be located on mu-plugins folder
*/
if ( ! function_exists( 'harisrozak_change_site_subdomain' ) ) {
function commercioo_change_site_subdomain( $data ) {
@harisrozak
harisrozak / wp_cron_sample.php
Last active September 4, 2021 07:01
WP Cron with wp_schedule_event()
<?php
/**
* Register the cron on plugin loaded.
* Valid values for the recurrence are ‘hourly’, ‘daily’, and ‘twicedaily’.
* @return void
*/
function harisrozak_register_wp_cron() {
if ( ! wp_next_scheduled( 'harisrozak_wp_cron' ) ) {
wp_schedule_event( time(), 'twicedaily', 'harisrozak_wp_cron' );
@harisrozak
harisrozak / wp-gutenberg-select-terms.js
Last active December 13, 2021 02:59
Programmatically add/select terms on WordPress Gutenberg editor
/**
* Programmatically add/select term on Gutenberg editor.
* On this function we are also ready if the classic-editor is loaded.
* Related issue: https://github.com/WordPress/gutenberg/issues/15147
*/
$('.bike-types').on('click', '.bike-type-item', function() {
var tag_id = $( this ).data( 'id' );
var tag_name = $( this ).data( 'name' );
if ( typeof wp !== 'undefined' && typeof wp.blocks !== 'undefined' ) {
@harisrozak
harisrozak / wp-bail-out-plugin-activation.php
Last active March 2, 2022 06:45
Bail out a plugin activation, even before the plugin file is loaded
<?php
/**
* Bail out a plugin activation, even before the plugin file is loaded.
* Using this hook will prevent any PHP fatal error, because of any defined constants, class names or functions.
*/
function bail_out_standalone_plugin_activation( $action, $result ) {
$plugin_name = 'YourPluginName';
$plugin_file_name = '/yourpluginname.php';
$activate_plugin_referer = 'activate-plugin_';