Skip to content

Instantly share code, notes, and snippets.

View kingjmaningo's full-sized avatar
🌱
A work in progress

kingjmaningo

🌱
A work in progress
View GitHub Profile
@kingjmaningo
kingjmaningo / get-current-postdata-through-json.php
Last active May 21, 2020 01:40
Get current post/page properties and use it on your scripts through JSON (Wordpress)
<?php
// In your wp_enqueue_scripts, just declare it when you localise your script or include it in your ajax array
add_action( 'wp_enqueue_scripts', 'wp_enqueue_child_scripts' );
function wp_enqueue_child_scripts() {
// Globalize post variable to get current post data
global $post;
// Enqueue child script used
wp_enqueue_script( 'custom-name', get_stylesheet_directory_uri() . '/js/script.js', array( 'jquery' ) );
@kingjmaningo
kingjmaningo / wordpress-simple-page-redirect.php
Last active May 17, 2020 01:24
Page redirect by page or by user role - Wordpress
<?php
add_action( 'template_redirect', 'page_redirection' );
function page_redirection() {
global $current_user;
$user_id = $current_user->ID;
$user_info = get_userdata($user_id);
$user_role = $user_info->roles[0];
@kingjmaningo
kingjmaningo / simple-enqueue-styles-scripts-wordpress.php
Last active May 17, 2020 03:07
Enqueue styles and scripts in Wordpress
<?php
add_action( 'wp_enqueue_scripts', 'my_custom_enqueue' );
function my_custom_enqueue() {
// Custom scipt
------------------------//
// Child theme's function.php
wp_enqueue_script( 'custom-js', get_template_directory_uri() . '/path/to/script.js' );
// In your plugin's function.php
@kingjmaningo
kingjmaningo / simple-post-loop-with-ajax.js
Last active May 20, 2020 01:03
Simple post loop with Ajax. (Wordpress)
jQuery(document).ready(function($){
$('.my-posts').change(function(){
var post_id = $('.my-posts').val();
$.ajax({
url : my_ajax_object.ajaxurl,
type : 'post',
data : {
action : 'get_data',
post_id : post_id,
@kingjmaningo
kingjmaningo / simple-merge-array-values.php
Last active May 19, 2020 03:34
Simple merge array (PHP)
<?php
$new_data = array(
'id' => 4,
'name' => 'Magic',
'email' => 'user_email_4',
'birth_date' => '10-11-2019'
);
$current_data = array (
array(
'id' => 1,
@kingjmaningo
kingjmaningo / next-prev-month.php
Last active May 20, 2020 02:37
Get previous/next month php
<?php
// Number of months after/before
$num = 3;
// Months before
$now = new DateTime();
$lastMonth = $now->sub(new DateInterval('P'. $num . 'M'));
// displays month/s before
echo $lastMonth->format('Y M');
// quick and short way
echo date('M', strtotime("-". $num . " month"));
@kingjmaningo
kingjmaningo / delete-value-in-multi-dimentional-array.php
Last active May 19, 2020 03:31
Remove an element from an array if certain value matched
<?php
// **** For simple array
// Delete value 2 in the array
$id = 2;
$simple_array = array(1, 2, 3);
$key = array_search($id, $array);
unset($array[$key]);
// We are expecting to remove id #2. To check:
print_r($array);
@kingjmaningo
kingjmaningo / update-value-in-multi-dimentional-array.php
Last active May 19, 2020 03:14
Update an element in an array if certain value matched
<?php
// Uinique id to point on a certain data
$id = 3;
// New value
$name = 'Michael';
// We are trying to change the name that matches the id
$array_values = array (
array(
'id' => 1,
'name' => 'Kobe',
@kingjmaningo
kingjmaningo / simple-preloader.html
Last active May 20, 2020 02:44
Simple website preloader
// Paste html in your header
<div class="loader-wrap">
<div class="loader-wrap-inner">
<div class="loader-holder">
<img src="your-loader.gif" alt="pre-loader-img" />
</div>
</div>
</div>
// Preloader style
@kingjmaningo
kingjmaningo / get-data-from-previous-page-gravity-form.php
Last active May 20, 2020 02:59
Get value form previous page (Gravity form)
<?php
// In this example im going to get certain fields that the user put in in the first page
// then make them choices on the second page
// You can paste this in your child's functions.php
add_filter('gform_pre_render_1', 'select_username_callback'); //only get data form form id 1
function select_username_callback($form) {
// To get what page you are on
$current_page = GFFormDisplay::get_current_page($form["id"]);
if ($current_page == 2) {
// Get values from previous form