Just a compilation of kick-ass tools and code snippets to kickstart your development mostly for web and APIs. Contributions are open!
“Never memorize something that you can look up.”
― Albert Einstein
“I consider that a man's brain originally is like a little empty attic, and you have to stock
it with such furniture as you choose. A fool takes in all the lumber of every sort that he comes
across, so that the knowledge which might be useful to him gets crowded out, or at best is jumbled
up with a lot of other things, so that he has a difficulty in laying his hands upon it.
Now the skillful workman is very careful indeed as to what he takes into his brain-attic.
He will have nothing but the tools which may help him in doing his work, but of these he
has a large assortment, and all in the most perfect order. It is a mistake to think that
that little room has elastic walls and can distend to any extent. Depend upon it there
comes a time when for every addition of knowledge you forget something that you knew before.
It is of the highest importance, therefore, not to have useless facts elbowing out the useful ones.”
― Arthur Conan Doyle, A Study in Scarlet
- Add cute emojis
- Add frameworks
- Codeigniter
- Laravel
- Add PHP & Web references
- Add REST Crud
- jp_lib
- Add back to top
- Git Cheatsheet
- Markdown Cheatsheet
- Regular Expressions (RegEx)
- Git commit conventions
- .gitignore for Wordpress
- WP Optimize Guide
- Dayprise Digital Ocean Notes / How to setup LAMP Digital Ocean server with phpMyAdmin
- MySQL databases using Emoji Guide
- Hide dot files!
#rc-imageselect, .g-recaptcha {transform:scale(0.8);-webkit-transform:scale(0.8);transform-origin:0 0;-webkit-transform-origin:0 0;}
div img {
margin: auto;
display: block;
}
$(document).on('click', 'body', function(e){
if(e.target.closest("header") === null){
$('.searchbox').hide(200);
} else {
console.log(e.target.closest("#searchdesktop"));
if(e.target.closest("#searchdesktop") !== null){
$('.searchbox').show(200);
$('input[name=q]').focus();
}
}
});
<style>.embed-container { position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden; max-width: 100%; } .embed-container iframe, .embed-container object, .embed-container embed { position: absolute; top: 0; left: 0; width: 100%; height: 100%; }</style><div class='embed-container'>
<!-- your iframe here -->
<iframe src='https://www.youtube.com/embed/dQw4w9WgXcQ' frameborder='0'
allowfullscreen></iframe>
<!--/ your iframe here -->
</div>
Just paste this on your footer and it will work
<!-- smooth scroll -->
<script type="text/javascript">
// Select all links with hashes
$('a[href*="#"]')
// Remove links that don't actually link to anything
.not('[href="#"]')
.not('[href="#0"]')
.click(function(event) {
// On-page links
if (
location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '')
&&
location.hostname == this.hostname
) {
// Figure out element to scroll to
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
// Does a scroll target exist?
if (target.length) {
// Only prevent default if animation is actually gonna happen
event.preventDefault();
$('html, body').animate({
scrollTop: target.offset().top
}, 1000, function() {
// Callback after animation
// Must change focus!
var $target = $(target);
$target.focus();
if ($target.is(":focus")) { // Checking if the target was focused
return false;
} else {
$target.attr('tabindex','-1'); // Adding tabindex for elements not focusable
$target.focus(); // Set focus again
};
});
}
}
});
</script>
<!-- /smooth scroll -->
$('input[name=check_if]').on('change', function(){
$('input[name=check_if]').not(this).prop('checked', false);
});
$('body').on('click', '.your-btn', function(){
var data_id = $(this).data('id');
...
});
$.getJSON('http://localhost/api/members', function(result){
// your code
}
$.ajax({
url: 'http://localhost/admin/ajax.php',
type: 'POST',
data: form_data,
success: function (result, textStatus, xhr) {
if(xhr.status == 200){
// do something
}else{
// error
alert('All that we are is the result of what we have thought.');
}
},
error: function(err){
console.error(err);
}
});
$("#the_id_of_the_form").submit(function(e){
var form_data = new FormData($(this)[0]);
$.ajax({
url: 'http://localhost/admin/ajax.php',
type: 'POST',
data: form_data,
success: function (result, textStatus, xhr) {
if(xhr.status == 200){
// do something
}else{
// error
alert('All that we are is the result of what we have thought.');
}
},
error: function(err){
console.error(err);
},
cache: false,
contentType: false,
processData: false
});
e.preventDefault();
});
function invokeForm(path, parameters) {
var form = $('<form></form>');
form.attr("method", "post");
form.attr("action", path);
$.each(parameters, function(key, value) {
var field = $('<input></input>');
field.attr("type", "hidden");
field.attr("name", key);
field.attr("value", value);
form.append(field);
});
// The form needs to be a part of the document in
// order for us to be able to submit it.
$(document.body).append(form);
form.submit();
}
$query_string = $_SERVER['QUERY_STRING'];
parse_str($query_string, $_GET);
$property_types = @$_GET['property_type'] ?: []; # set empty arr default
$property_locations = @$_GET['property_location'] ?: []; # set empty arr default
public function export()
{
// output headers so that the file is downloaded rather than displayed
header('Content-type: text/csv');
header('Content-Disposition: attachment; filename="sales_export.csv"');
// do not cache the file
header('Pragma: no-cache');
header('Expires: 0');
// create a file pointer connected to the output stream
$file = fopen('php://output', 'w');
// send the column headers
fputcsv($file, array('Coupon ID', 'Full name', 'Email address', 'Contact No.', 'Reward', 'Promo', 'Created Date'));
$res = $this->redeem_model->allM($this->session->mall_id);
$new_res = [];
foreach ($res as $key => $value) {
$new_res[] = array(
$value->coupon_id,
$value->full_name,
$value->email,
$value->contact_num,
$value->reward_name,
$value->promo_name,
$value->created_at_f,
);
}
$data = $new_res;
foreach ($data as $row)
{
fputcsv($file, $row);
}
exit();
}
/**
* unlink all montage temporary files
* except ffmpeg.exe, of course
* @param string $value [description]
* @return [type] [description]
*/
public function unlinky()
{
$filesToKeep = array(
'uploads/_montage_temp/ffmpeg.exe',
);
$dirList = glob('uploads/_montage_temp/*'); # get all files\
foreach ($dirList as $file) {
if (! in_array($file, $filesToKeep)) { # delet
if (is_dir($file)) {
rmdir($file);
} else {
unlink($file);
}//END IF
}//END IF
}//END FOREACH LOOP
}
Function for formatting price. 20000 -> 20K, 1000000 to 1M, etc
/**
* Returns the price rounded up with English shortcut
* such as K, M, B
* @param int $n unformatted price
* @return string [description]
*/
function formatPrice($n)
{
if ($n < 1000000) {
// Anything less than a million
$f = round(number_format($n / 1000, 3), 2);
$f .= 'K';
} else if ($n < 1000000000) {
// Anything less than a billion
$f = round(number_format($n / 1000000, 3), 2);
$f .= 'M';
} else {
// At least a billion
$f = round(number_format($n / 1000000000, 3), 2);
$f .= 'B';
}
return 'P' . $f;
}
public function sortByOrder($data)
{
usort($data, function($a, $b)
{
if ($a == $b) {
return 0;
}
return (strtotime($a->created_at) < strtotime($b->created_at)) ? -1 : 1;
});
return $data;
}
$array = array_values($array);
$csv = array_map('str_getcsv', file(base_url() . 'test.csv'));
/**
* @link https://stackoverflow.com/a/26722303/7800523
* Compute the start and end date of some fixed o relative quarter in a specific year.
* @param mixed $quarter Integer from 1 to 4 or relative string value:
* 'this', 'current', 'previous', 'first' or 'last'.
* 'this' is equivalent to 'current'. Any other value
* will be ignored and instead current quarter will be used.
* Default value 'current'. Particulary, 'previous' value
* only make sense with current year so if you use it with
* other year like: get_dates_of_quarter('previous', 1990)
* the year will be ignored and instead the current year
* will be used.
* @param int $year Year of the quarter. Any wrong value will be ignored and
* instead the current year will be used.
* Default value null (current year).
* @param string $format String to format returned dates
* @return array Array with two elements (keys): start and end date.
*/
public static function get_dates_of_quarter($quarter = 'current', $year = null, $format = null)
{
if ( !is_int($year) ) {
$year = (new DateTime)->format('Y');
}
$current_quarter = ceil((new DateTime)->format('n') / 3);
switch ( strtolower($quarter) ) {
case 'this':
case 'current':
$quarter = ceil((new DateTime)->format('n') / 3);
break;
case 'previous':
$year = (new DateTime)->format('Y');
if ($current_quarter == 1) {
$quarter = 4;
$year--;
} else {
$quarter = $current_quarter - 1;
}
break;
case 'first':
$quarter = 1;
break;
case 'last':
$quarter = 4;
break;
default:
$quarter = (!is_int($quarter) || $quarter < 1 || $quarter > 4) ? $current_quarter : $quarter;
break;
}
if ( $quarter === 'this' ) {
$quarter = ceil((new DateTime)->format('n') / 3);
}
$start = new DateTime($year.'-'.(3*$quarter-2).'-1 00:00:00');
$end = new DateTime($year.'-'.(3*$quarter).'-'.($quarter == 1 || $quarter == 4 ? 31 : 30) .' 23:59:59');
return array(
'start' => $format ? $start->format($format) : $start,
'end' => $format ? $end->format($format) : $end,
);
}
# usage
get_dates_of_quarter();
//return current quarter start and end dates
get_dates_of_quarter(2);
//return 2nd quarter start and end dates of current year
get_dates_of_quarter('first', 2010, 'Y-m-d');
//return start='2010-01-01' and end='2014-03-31'
get_dates_of_quarter('current', 2009, 'Y-m-d');
//Supposing today is '2014-08-22' (3rd quarter), this will return
//3rd quarter but of year 2009.
//return start='2009-07-01' and end='2009-09-30'
get_dates_of_quarter('previous');
//Supposing today is '2014-02-18' (1st quarter), this will return
//return start='2013-10-01' and end='2013-12-31'
- Go to public_html/wp-login.php
- Around line 900 find the "#loginform" html
- Replace the following lines with this snippet
<form name="loginform" id="loginform" action="<?php echo str_replace("http://", "https://", esc_url( site_url( 'wp-login.php', 'login_post' ) )); ?>" method="post">
After installing wordpress, edit your wp-config.php
and fill-out appropriate credentials
- Delete all themes except the latest one
- Rename the theme to your desired theme name
- In your theme directory, open
style.css
- On
line 2
, replaceTheme Name: Twentyseventeen
toTheme Name: Your Theme Name
- Delete all styles (Because this will override your custom theme style
or you can just run this script...
$ cd wp-content
$ cd themes
$ rm twentyfifteen -r
$ rm twentysixteen -r
$ mv twentyseventeen my-project
$ sed -i -e 's/Twenty Seventeen/my-project/g' my-project/style.css
$ sed -i '16,$d' my-project/style.css
$ rm my-project/style.css.bak
- Finally, don't forget to log-on to WP Admin, go to Appearance > Themes, and select your new theme
-
Copy and replace the contents of
front-page.php
with this.<?php /* Template Name: Home */ get_header(); while(have_posts()): the_post(); ?> <!-- your content here --> <?php endwhile; get_footer();
-
Create a page and let it have a
Home
template -
Go to Settings > Reading
-
On the
Front page displays
section, tickA static page
radio -
On the drop down, select
Home
template as Front page -
Save changes
-
Personalize your
header.php
andfooter.php
-
Replace/Import your assets (css, js, images) inside
/your-theme-name/assets/
folder -
Don't forget to put
<?php wp_head(); ?>
before the closing</head>
of your header tag -
Don't forget to put
<?php wp_footer(); ?>
before the closing</body>
of your body tag -
Use this script for dynamic url in our includes
<link rel="stylesheet" href="<?php echo get_template_directory_uri(); ?>/assets/css/styles.css">
-
Make sure you have this minimum code in every template
<?php /* Template Name: My Template */ get_header(); while(have_posts()): the_post(); ?> <!-- your content here -> <?php endwhile; get_footer();
Just search them in the wordpress plugin database in your WP admin dashboard. WP Admin > Plugins > Add new.
Common WP plugins for a generic website
Plugin name | Description |
---|---|
Contact Form 7 | Most common plugin for contact forms |
Contact Form 7 Database Addon – CFDB7 | Save contact form submissions to wp-admin |
Custom Post Type UI | For custom post types |
Advanced Custom Fields PRO | For additional custom fields |
Enable Media Replace | For replacing images in Media Library |
WP Mail SMTP by WPForms | For sending secure mails |
UpdraftPlus WordPress Backup Plugin | For backups |
These are the plugins used commonly to boost page speed score and fixing SEO related issues
Plugin name | Description |
---|---|
Redirection | Used to handle 301 redirects |
Autoptimize | For minifying CSS & HTML |
W3 Total Cache | Caching plugin |
TODO
<?php
/* Template Name: My Template */
get_header();
while(have_posts()): the_post();
?>
<!-- your content here -->
<?php
endwhile;
get_footer();
<link rel="stylesheet" href="<?php echo get_template_directory_uri(); ?>/assets/css/styles.css">
<?php
$args = array('post_type' => 'service', 'posts_per_page' => -1);
$the_query = new WP_Query($args);
if ( $the_query->have_posts() ) { while ( $the_query->have_posts() ): $the_query->the_post(); ?>
<!-- your content here -->
<?php endwhile; wp_reset_postdata(); } else { /** no posts found **/ } ?>
<?php
$args = array('post_type' => 'service', 'posts_per_page' => -1);
/** optional meta query **/
$args['meta_query'] = array(
array(
'key' => 'key',
'value' => $value,
'compare' => 'LIKE'
)
);
/** /optional meta query **/
$the_query = new WP_Query($args);
if ( $the_query->have_posts() ) { while ( $the_query->have_posts() ): $the_query->the_post(); ?>
<!-- your content here -->
<?php endwhile; wp_reset_postdata(); } else { /** no posts found **/ } ?>
Using BETWEEN
comparison
$price_range_filter = array(
'key' => 'min_price',
'value' => array($lower, $higher),
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
);
Nested meta query example
$price_range_filter_min = array(
'key' => 'min_price',
'value' => array($lower, $higher),
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
);
$price_range_filter_max = array(
'key' => 'max_price',
'value' => array($lower, $higher),
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
);
$price_range_filter = array(
'relation' => 'OR',
$price_range_filter_min,
$price_range_filter_max,
);
$args = array('post_type' => 'our_properties', 'posts_per_page' => -1, 'orderby' => 'post_title', 'order' => 'ASC');
# Taxonomy filter
$args['tax_query'] = array(
array(
'taxonomy' => 'project_classification',
'field' => 'slug', # could be slug or title
'terms' => $type,
)
);
# / Taxonomy filter
$the_query = new WP_Query($args);
/** variable for our menu object **/
$menu_obj = wp_get_nav_menu_items('main'); # wp post object for `main` wp menu
<!-- menu loop -->
<?php foreach ($menu_obj as $obj): ?>
<li><a href="<?= $obj->url; ?>"><?= $obj->title; ?></a></li>
<?php endforeach; ?>
<!-- /menu loop -->
<?php echo do_shortcode('[contact-form-7 id="89" title="Food Tasting"]'); ?>
<?php if( have_rows('repeater_field_name') ): while ( have_rows('repeater_field_name') ) : the_row(); ?>
<li><img src="<?php echo get_sub_field('sub_field_name'); ?>"></li>
<?php endwhile; else : endif; ?>
or
<?php $repeater_object = get_field('repeater_field_name');
foreach ($repeater_object as $obj) { ?>
<!-- your content goes here -->
<p><?php echo $obj['your_field']; ?></p>
<p><?php echo $obj['another_field'];?></p>
<?php } ?>
(parent_repeater->child_repeater->child_repeater_items)
<?php
if( have_rows('parent_repeater') ): # Parent repeater if
while( have_rows('parent_repeater') ): the_row(); # Parent repeater while ?>
<article>
<?php
/** Parent Repeater Fields **/
the_sub_field('panel_header');
?>
<ul>
<?php
if( have_rows('child_repeater') ): # Child repeater if?>
<?php
/** Child repeater loop **/
while( have_rows('child_repeater') ): the_row(); # Child repeater while ?>
<li><?php the_sub_field('child_repeater_values'); #Child repeater values ?></li>
<?php endwhile; # / Child repeater while?>
</ul>
<?php endif; # / Child repeater if?>
</article>
<picture>
<img src="<?php the_sub_field('side_image'); # Some random picture outside?>">
</picture>
<?php endwhile; # / Parent repeater while ?>
<?php endif; # / Parent repeater if ?>
or
<?php $parent_repeater_obj = get_field('repeater_field_name');
foreach ($parent_repeater_obj as $parent_obj): ?>
<p><?php echo $parent_obj['your_field']; ?></p>
<p><?php echo $parent_obj['details']; ?></p>
<?php $child_repeater_obj = $parent_obj['child_repeater_field_name'];
foreach ($child_repeater_obj as $child_obj) : ?>
<p><?php echo $child_obj['photo']; ?></p>
<?php endforeach; # end parent foreach?>
<?php endforeach; # end child foreach?>
<?php
$ctr = 0; # Counter for outer repeater
/** Parent Repeater **/
if( have_rows('package_inclusions') ):
while( have_rows('package_inclusions') ): the_row(); ?>
<main class="<?php echo ((++$ctr%2) != 0) ? 'packagedetail' : 'packageservices'; # if counter is odd ?>">
<article>
<h3><?php
/** Parent Repeater Fields **/
the_sub_field('panel_header');
?></h3>
<ul>
<?php
$li_ctr = 0;
if( have_rows('package_inclusions_ul') ): ?>
<?php
/** Child repeater loop **/
while( have_rows('package_inclusions_ul') ): the_row(); ?>
<li><?php the_sub_field('package_inclusions_li'); #Child repeater values ?></li>
<?php if(++$li_ctr%6 == 0): # if divisible by six?>
</ul>
<ul>
<?php endif; # / if divisible by six?>
<?php endwhile; ?>
</ul>
<?php endif; ?>
</article>
<picture>
<img src="<?php the_sub_field('side_image');?>">
</picture>
</main>
<?php endwhile; ?>
<?php endif; ?>
$path = $_SERVER['DOCUMENT_ROOT'] . '/wealthmart'; # Use this if you have subfolder
$path = $_SERVER['DOCUMENT_ROOT']; # Use this if your project is in public_html (root folder)
$path = '../../../../..'; # Use this if everything else fails 🙄
include_once $path . '/wp-config.php';
include_once $path . '/wp-load.php';
include_once $path . '/wp-includes/wp-db.php';
include_once $path . '/wp-includes/pluggable.php';
<?php echo apply_filters('the_content', get_post_field('post_content', 92)); ?>
$excerpt = wp_trim_words( get_the_content(), 150, '');
if(is_singular(array('cpt_name'))){
}
$post_type = get_post_type_object( get_post_type( $data->ID ))->label;
// Filter except length to 35 words.
// tn custom excerpt length
# Add this to functions.php
function tn_custom_excerpt_length( $length ) {
return 22;
}
add_filter( 'excerpt_length', 'tn_custom_excerpt_length', 999 );
global $post;
$slug = $post->post_name; # Slug of the page
$args = array('post_type' => 'article', 'posts_per_page' => $post_count,
'offset' => 1);
$args = array('post_type' => array('page', 'project', 'location', 'bank'),
'posts_per_page' => -1, 's' => @$_GET['q']);
$args = array('post_type' => 'our_properties', 'posts_per_page' => 3, 'paged' => $paged, 'meta_key' => 'location', 'meta_value' => get_the_ID());
$args = array('post_type' => 'service', 'posts_per_page' => -1);
$the_query = new WP_Query($args);
echo $the_query->post_count; # The post count
/* Template Name: Other template */
global $post;
$post = get_term( 4, 'ee_category', OBJECT ); # house-and-lot (landing page)
setup_postdata( $post );
$tax = $post;
include('taxonomy-ee_category.php'); # include the template you want to load
wp_reset_postdata();
//Default WordPress
the_post_thumbnail( 'thumbnail' ); // Thumbnail (150 x 150 hard cropped)
the_post_thumbnail( 'medium' ); // Medium resolution (300 x 300 max height 300px)
the_post_thumbnail( 'medium_large' ); // Medium Large (added in WP 4.4) resolution (768 x 0 infinite height)
the_post_thumbnail( 'large' ); // Large resolution (1024 x 1024 max height 1024px)
the_post_thumbnail( 'full' ); // Full resolution (original size uploaded)
//With WooCommerce
the_post_thumbnail( 'shop_thumbnail' ); // Shop thumbnail (180 x 180 hard cropped)
the_post_thumbnail( 'shop_catalog' ); // Shop catalog (300 x 300 hard cropped)
the_post_thumbnail( 'shop_single' ); // Shop single (600 x 600 hard cropped)
$field_key = "field_59914624f4ae2"; # Find this in ACF itself under `Screen Options`
$field = get_field_object($field_key);
if($field){
foreach( $field['choices'] as $choice ) {
echo '<option>' . $choice . '</option>';
}
}
get_the_terms($post->ID, "news_archive")[0]->slug;
Execute your own function whenever a certain event occurs. Insert this code in functions.php
file.
function your_function($id,$post_object)
{
//do something with $id / $post_object
}
$status = "publish"; // list of status //https://codex.wordpress.org/Post_Status
$post_type = "project"; // post type name
// fires after a post is published/.$status."ed"
add_action("{$status}_{$post_type}",'your_function',10,2); // or add_action("publish_project","your_function",10,2);
function your_function($id,$before,$after)
{
// $before - post object before it is modified
// $after - updated post object
}
// fires after a post is updated
add_action('post_updated','your_function',10,3);
get_template_part( 'template-parts/reports/banner', 'property' );
#in directory, /site/wwwroot/wp-content/themes/skf/template-parts/reports/banner-property.php
if you want to access the custom fields
function your_function( $post_id ) {
// get post object
$post = get_post($post_id);
$is_new = $post->post_date == $post->post_modified;
$sample_acf_data = get_field('your_acf_field',$post_id);
//fires when a new project is added
if($post->post_type == 'project' && $is_new){
}
//fires when a project is updated
if($post->post_type == 'project' && !$is_new){
}
}
add_action('acf/save_post', 'your_function', 20);
Insert this code in functions.php
file.
/**
* FOR MULTIPLE PAGES
* Adds instruction(text) below the featured image field of a page
*
* $images format [ page_id(int)/post_type_slug(string) => Recommended dimension(string) ]
* example:
* $images = [
* 5 => '1600x900'
* 7 => '300x200',
* 'project' => '300x500',
* ];
*
*/
function imageRecommended($images, &$html){
if(isset($images[get_the_ID()]) || array_key_exists(get_the_ID(),$images)){
$html .= "<p>Recommended dimension, {$images[get_the_ID()]}.</p>";
}
$post_type = get_post_type();
if(isset($images[$post_type])){
$html .= "<p>Recommended dimension, {$images[$post_type]}.</p>";
}
}
function featured_image_instruction( $html ) {
/**
* Sample usage
**/
imageRecommended([
7 => '1680x900',
8 => '1485x900',
'project' => '300x500'
],$html);
return $html;
}
add_filter( 'admin_post_thumbnail_html', 'featured_image_instruction');
array(18) {
["ID"] => int(25)["id"] => int(25)["title"] => string(7)
"slider1" ["filename"] => string(11)
"slider1.jpg" ["url"] => string(59)
"http://localhost/bgc/wp-content/uploads/2017/10/slider1.jpg" ["alt"] => string(0)
"" ["author"] => string(1)
"1" ["description"] => string(0)
"" ["caption"] => string(0)
"" ["name"] => string(7)
"slider1" ["date"] => string(19)
"2017-10-30 08:24:36" ["modified"] => string(19)
"2017-10-30 08:24:36" ["mime_type"] => string(10)
"image/jpeg" ["type"] => string(5)
"image" ["icon"] => string(57)
"http://localhost/bgc/wp-includes/images/media/default.png" ["width"] => int(1366)["height"] => int(537)["sizes"] => array(18) {
["thumbnail"] => string(67)
"http://localhost/bgc/wp-content/uploads/2017/10/slider1-150x150.jpg" ["thumbnail-width"] => int(150)["thumbnail-height"] => int(150)["medium"] => string(67)
"http://localhost/bgc/wp-content/uploads/2017/10/slider1-300x118.jpg" ["medium-width"] => int(300)["medium-height"] => int(118)["medium_large"] => string(67)
"http://localhost/bgc/wp-content/uploads/2017/10/slider1-768x302.jpg" ["medium_large-width"] => int(525)["medium_large-height"] => int(206)["large"] => string(68)
"http://localhost/bgc/wp-content/uploads/2017/10/slider1-1024x403.jpg" ["large-width"] => int(525)["large-height"] => int(207)["twentyseventeen-featured-image"] => string(59)
"http://localhost/bgc/wp-content/uploads/2017/10/slider1.jpg" ["twentyseventeen-featured-image-width"] => int(1366)["twentyseventeen-featured-image-height"] => int(537)["twentyseventeen-thumbnail-avatar"] => string(67)
"http://localhost/bgc/wp-content/uploads/2017/10/slider1-100x100.jpg" ["twentyseventeen-thumbnail-avatar-width"] => int(100)["twentyseventeen-thumbnail-avatar-height"] => int(100)
}
}
object(WP_Query) #875 (49) { ["query"]= > array(2) {
["post_type"] => string(7)
"article" ["posts_per_page"] => int(-1)
}["query_vars"] => array(64) {
["post_type"] => string(7)
"article" ["posts_per_page"] => int(-1)["error"] => string(0)
"" ["m"] => string(0)
"" ["p"] => int(0)["post_parent"] => string(0)
"" ["subpost"] => string(0)
"" ["subpost_id"] => string(0)
"" ["attachment"] => string(0)
"" ["attachment_id"] => int(0)["name"] => string(0)
"" ["static"] => string(0)
"" ["pagename"] => string(0)
"" ["page_id"] => int(0)["second"] => string(0)
"" ["minute"] => string(0)
"" ["hour"] => string(0)
"" ["day"] => int(0)["monthnum"] => int(0)["year"] => int(0)["w"] => int(0)["category_name"] => string(0)
"" ["tag"] => string(0)
"" ["cat"] => string(0)
"" ["tag_id"] => string(0)
"" ["author"] => string(0)
"" ["author_name"] => string(0)
"" ["feed"] => string(0)
"" ["tb"] => string(0)
"" ["paged"] => int(0)["meta_key"] => string(0)
"" ["meta_value"] => string(0)
"" ["preview"] => string(0)
"" ["s"] => string(0)
"" ["sentence"] => string(0)
"" ["title"] => string(0)
"" ["fields"] => string(0)
"" ["menu_order"] => string(0)
"" ["embed"] => string(0)
"" ["category__in"] => array(0) {}["category__not_in"] => array(0) {}["category__and"] => array(0) {}["post__in"] => array(0) {}["post__not_in"] => array(0) {}["post_name__in"] => array(0) {}["tag__in"] => array(0) {}["tag__not_in"] => array(0) {}["tag__and"] => array(0) {}["tag_slug__in"] => array(0) {}["tag_slug__and"] => array(0) {}["post_parent__in"] => array(0) {}["post_parent__not_in"] => array(0) {}["author__in"] => array(0) {}["author__not_in"] => array(0) {}["ignore_sticky_posts"] => bool(false)["suppress_filters"] => bool(false)["cache_results"] => bool(true)["update_post_term_cache"] => bool(true)["lazy_load_term_meta"] => bool(true)["update_post_meta_cache"] => bool(true)["nopaging"] => bool(true)["comments_per_page"] => string(2)
"50" ["no_found_rows"] => bool(false)["order"] => string(4)
"DESC"
}["tax_query"] => object(WP_Tax_Query) #878 (6) { ["queries"]= > array(0) {}["relation"] => string(3)
"AND" ["table_aliases": protected] => array(0) {}["queried_terms"] => array(0) {}["primary_table"] => string(8)
"wp_posts" ["primary_id_column"] => string(2)
"ID"
}["meta_query"] => object(WP_Meta_Query) #876 (9) { ["queries"]= > array(0) {}["relation"] => NULL["meta_table"] => NULL["meta_id_column"] => NULL["primary_table"] => NULL["primary_id_column"] => NULL["table_aliases": protected] => array(0) {}["clauses": protected] => array(0) {}["has_or_relation": protected] => bool(false)
}["date_query"] => bool(false)["request"] => string(231)
"SELECT wp_posts.* FROM wp_posts WHERE 1=1 AND wp_posts.post_type = 'article' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'acf-disabled' OR wp_posts.post_status = 'private') ORDER BY wp_posts.post_date DESC " ["posts"] => array(3) {
[0] => object(WP_Post) #880 (24) { ["ID"]= > int(37)["post_author"] => string(1)
"1" ["post_date"] => string(19)
"2017-11-02 06:30:51" ["post_date_gmt"] => string(19)
"2017-11-02 06:30:51" ["post_content"] => string(0)
"" ["post_title"] => string(14)
"Black Survival" ["post_excerpt"] => string(0)
"" ["post_status"] => string(7)
"publish" ["comment_status"] => string(6)
"closed" ["ping_status"] => string(6)
"closed" ["post_password"] => string(0)
"" ["post_name"] => string(14)
"black-survival" ["to_ping"] => string(0)
"" ["pinged"] => string(0)
"" ["post_modified"] => string(19)
"2017-11-02 06:30:51" ["post_modified_gmt"] => string(19)
"2017-11-02 06:30:51" ["post_content_filtered"] => string(0)
"" ["post_parent"] => int(0)["guid"] => string(49)
"http://localhost/bgc/?post_type=article&p=37" ["menu_order"] => int(0)["post_type"] => string(7)
"article" ["post_mime_type"] => string(0)
"" ["comment_count"] => string(1)
"0" ["filter"] => string(3)
"raw"
}[1] => object(WP_Post) #872 (24) { ["ID"]= > int(35)["post_author"] => string(1)
"1" ["post_date"] => string(19)
"2017-11-02 06:18:28" ["post_date_gmt"] => string(19)
"2017-11-02 06:18:28" ["post_content"] => string(0)
"" ["post_title"] => string(6)
"Test 2" ["post_excerpt"] => string(0)
"" ["post_status"] => string(7)
"publish" ["comment_status"] => string(6)
"closed" ["ping_status"] => string(6)
"closed" ["post_password"] => string(0)
"" ["post_name"] => string(6)
"test-2" ["to_ping"] => string(0)
"" ["pinged"] => string(0)
"" ["post_modified"] => string(19)
"2017-11-02 06:18:28" ["post_modified_gmt"] => string(19)
"2017-11-02 06:18:28" ["post_content_filtered"] => string(0)
"" ["post_parent"] => int(0)["guid"] => string(49)
"http://localhost/bgc/?post_type=article&p=35" ["menu_order"] => int(0)["post_type"] => string(7)
"article" ["post_mime_type"] => string(0)
"" ["comment_count"] => string(1)
"0" ["filter"] => string(3)
"raw"
}[2] => object(WP_Post) #869 (24) { ["ID"]= > int(22)["post_author"] => string(1)
"1" ["post_date"] => string(19)
"2017-10-30 08:15:13" ["post_date_gmt"] => string(19)
"2017-10-30 08:15:13" ["post_content"] => string(0)
"" ["post_title"] => string(4)
"Test" ["post_excerpt"] => string(0)
"" ["post_status"] => string(7)
"publish" ["comment_status"] => string(6)
"closed" ["ping_status"] => string(6)
"closed" ["post_password"] => string(0)
"" ["post_name"] => string(4)
"test" ["to_ping"] => string(0)
"" ["pinged"] => string(0)
"" ["post_modified"] => string(19)
"2017-11-02 04:07:43" ["post_modified_gmt"] => string(19)
"2017-11-02 04:07:43" ["post_content_filtered"] => string(0)
"" ["post_parent"] => int(0)["guid"] => string(49)
"http://localhost/bgc/?post_type=article&p=22" ["menu_order"] => int(0)["post_type"] => string(7)
"article" ["post_mime_type"] => string(0)
"" ["comment_count"] => string(1)
"0" ["filter"] => string(3)
"raw"
}
}["post_count"] => int(3)["current_post"] => int(-1)["in_the_loop"] => bool(false)["post"] => object(WP_Post) #880 (24) { ["ID"]= > int(37)["post_author"] => string(1)
"1" ["post_date"] => string(19)
"2017-11-02 06:30:51" ["post_date_gmt"] => string(19)
"2017-11-02 06:30:51" ["post_content"] => string(0)
"" ["post_title"] => string(14)
"Black Survival" ["post_excerpt"] => string(0)
"" ["post_status"] => string(7)
"publish" ["comment_status"] => string(6)
"closed" ["ping_status"] => string(6)
"closed" ["post_password"] => string(0)
"" ["post_name"] => string(14)
"black-survival" ["to_ping"] => string(0)
"" ["pinged"] => string(0)
"" ["post_modified"] => string(19)
"2017-11-02 06:30:51" ["post_modified_gmt"] => string(19)
"2017-11-02 06:30:51" ["post_content_filtered"] => string(0)
"" ["post_parent"] => int(0)["guid"] => string(49)
"http://localhost/bgc/?post_type=article&p=37" ["menu_order"] => int(0)["post_type"] => string(7)
"article" ["post_mime_type"] => string(0)
"" ["comment_count"] => string(1)
"0" ["filter"] => string(3)
"raw"
}["comment_count"] => int(0)["current_comment"] => int(-1)["found_posts"] => int(3)["max_num_pages"] => int(0)["max_num_comment_pages"] => int(0)["is_single"] => bool(false)["is_preview"] => bool(false)["is_page"] => bool(false)["is_archive"] => bool(false)["is_date"] => bool(false)["is_year"] => bool(false)["is_month"] => bool(false)["is_day"] => bool(false)["is_time"] => bool(false)["is_author"] => bool(false)["is_category"] => bool(false)["is_tag"] => bool(false)["is_tax"] => bool(false)["is_search"] => bool(false)["is_feed"] => bool(false)["is_comment_feed"] => bool(false)["is_trackback"] => bool(false)["is_home"] => bool(true)["is_404"] => bool(false)["is_embed"] => bool(false)["is_paged"] => bool(false)["is_admin"] => bool(false)["is_attachment"] => bool(false)["is_singular"] => bool(false)["is_robots"] => bool(false)["is_posts_page"] => bool(false)["is_post_type_archive"] => bool(false)["query_vars_hash": "WP_Query": private] => string(32)
"588d5ef44472f184074f334ee6c62d1a" ["query_vars_changed": "WP_Query": private] => bool(false)["thumbnails_cached"] => bool(false)["stopwords": "WP_Query": private] => NULL["compat_fields": "WP_Query": private] => array(2) {
[0] => string(15)
"query_vars_hash" [1] => string(18)
"query_vars_changed"
}["compat_methods": "WP_Query": private] => array(2) {
[0] => string(16)
"init_query_flags" [1] => string(15)
"parse_tax_query"
}
}
wp_hash_password($pass); # Magic function. Don't delete! Needed for wp default password hashing
$password_hashed = $row['user_pass']; #password from wp_users table
$plain_password = $_POST['password']; #password inputted from user
$wp_hasher = new PasswordHash(8, TRUE);
if($wp_hasher->CheckPassword($plain_password, $password_hashed)) {
#success..
} else {
#fail..
}
Paste this on functions.php
function remove_all_theme_styles() {
global $wp_styles;
$wp_styles->queue = array();
}
add_action('wp_print_styles', 'remove_all_theme_styles', 100);
/** Memory Limit */
define('WP_MEMORY_LIMIT', '1024G');
define( 'WP_MAX_MEMORY_LIMIT', '1024G' );
/* That's all, stop editing! Happy blogging. */
- Log on to
CPanel
- Go to
phpmyadmin
- Execute the following queries:
SELECT * FROM wp_posts WHERE ID = '0' AND post_type = 'attachment'
Delete FROM wp_posts WHERE ID = '0' AND post_type = 'attachment'
Credits to Divine
⬆ Back to top
Note: Make sure you don't have a custom post type and a slug with a same name
Save this as function-pagination.php
<?php
/**
* @link: http://callmenick.com/post/custom-wordpress-loop-with-pagination
* Create this as a separate file function-pagination.php
*/
function custom_pagination($numpages = '', $pagerange = '', $paged='') {
if (empty($pagerange)) {
$pagerange = 2;
}
/**
* This first part of our function is a fallback
* for custom pagination inside a regular loop that
* uses the global $paged and global $wp_query variables.
*
* It's good because we can now override default pagination
* in our theme, and use this function in default quries
* and custom queries.
*/
global $paged;
if (empty($paged)) {
$paged = 1;
}
if ($numpages == '') {
global $wp_query;
$numpages = $wp_query->max_num_pages;
if(!$numpages) {
$numpages = 1;
}
}
/**
* We construct the pagination arguments to enter into our paginate_links
* function.
*/
$pagination_args = array(
// 'base' => str_replace(999999999, '%#%', esc_url(get_pagenum_link(999999999))), # Uncomment this line to enable dynamic query_string @divine
'base' => get_pagenum_link(1) . '%_%', # Comment out this line to enable dynamic query_string
'format' => 'page/%#%',
'total' => $numpages,
'current' => $paged,
'show_all' => False,
'end_size' => 1,
'mid_size' => $pagerange,
'prev_next' => True,
'prev_text' => __('«'),
'next_text' => __('»'),
'type' => 'list',
'add_args' => false,
'add_fragment' => ''
);
$paginate_links = paginate_links($pagination_args);
if ($paginate_links) {
echo "<div class='pagination'>";
# This is how your span looks like once rendered
# echo "<span class='page-numbers page-num'>Page " . $paged . " of " . $numpages . "</span> ";
echo $paginate_links;
echo "</div>";
}
}
?>
This is an example usage of the function-pagination.php
file
<?php
/**
* @usage
*/
/**
* include the custom pagination here
*/
include_once('function-pagination.php');
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
# QUERY ARGS HERE
$args = array(
'post_type' => 'blog',
'posts_per_page' => 1, # Count of posts per page
'paged' => $paged
);
$the_query = new WP_Query( $args );
if ($the_query->have_posts()) : while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<li>
<div class="featured-img"><img src="<?php the_post_thumbnail_url(); ?>"></div>
<div class="details">
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<h5>Date Posted: <?php echo date("F d, Y", strtotime(get_the_date())); ?></h5>
<p><?php echo substr(get_the_excerpt(), 0, 100) . " ..."; ?></p>
<p><a href="<?php the_permalink(); ?>">Read More</a></p>
</div>
</li>
<?php
endwhile;
/* Pagination */
if (function_exists(custom_pagination)) {
custom_pagination($the_query->max_num_pages,"",$paged);
}
# Make sure to change $the_query variable to your actual loop variable!!!!
/* Pagination */
wp_reset_postdata();
?>
<!-- other usage -->
<?php
if (function_exists(custom_pagination)) {
$pagination = custom_pagination($the_query->max_num_pages,"",$paged);
}
?>
<?php foreach ($pagination as $value):
?>
<li class="<?php echo ($paged == strip_tags($value)) ? 'active':''; ?>">
<?php echo $value ?>
</li>
<?php endforeach ?>
# Put this in your functions.php
function pif_disable_redirect_canonical($redirect_url) {
if (is_single()) $redirect_url = false;
return $redirect_url;
}
- Step 1. Create table for API keys
CREATE TABLE `keys` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`user_id` INT(11) NOT NULL,
`key` VARCHAR(40) NOT NULL,
`level` INT(2) NOT NULL,
`ignore_limits` TINYINT(1) NOT NULL DEFAULT '0',
`is_private_key` TINYINT(1) NOT NULL DEFAULT '0',
`ip_addresses` TEXT NULL DEFAULT NULL,
`date_created` INT(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
- Step 2. (Optional) On
application/config/rest.php
line326
,
Enable rest keys / authorization toTRUE
. If you do this, make sure to insert a row to yourkeys
table and add a base64 encoded username:password to thekey
field.
$config['rest_enable_keys'] = TRUE;
- Step 3. On
application/config/rest.php
line129
,
From LDAP make auth_source into blank line
# Just empty the string
# $config['auth_source'] = 'ldap';
$config['auth_source'] = '';
- Step 4. On
application/config/rest.php
line113
,
From empty string, put tobasic
$config['rest_auth'] = 'basic';
- Step 5. On
application/config/rest.php
line213
,
Configure available credentials for API Access
$config['rest_valid_logins'] = ['admin' => '1234'];
- Step 6. On
application/libraries/REST_Controller.php
line764
,
Make custom response for forbidden and unauthorized access
public function response($data = NULL, $http_code = NULL)
{
ob_start();
// If the HTTP status is not NULL, then cast as an integer
if ($http_code !== NULL)
{
// So as to be safe later on in the process
$http_code = (int) $http_code;
# Forbidden / Unauthorized message
if($http_code == 401 || $http_code == 403){
header("Content-type:application/json");
http_response_code($http_code);
$msg = array('message' => ($http_code == 401) ? "Unauthorized" : "Forbidden");
$this->output->_display(json_encode($msg));
exit;
}
# / Forbidden / Unauthorized message
}
...
- Step 7. (Optional)
Createlogs
Table for recording every access of the API
CREATE TABLE `logs` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`uri` VARCHAR(255) NOT NULL,
`method` VARCHAR(6) NOT NULL,
`params` TEXT DEFAULT NULL,
`api_key` VARCHAR(40) NOT NULL,
`ip_address` VARCHAR(45) NOT NULL,
`time` INT(11) NOT NULL,
`rtime` FLOAT DEFAULT NULL,
`authorized` VARCHAR(1) NOT NULL,
`response_code` smallint(3) DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
- Step 7.1 (Optional) table On
application/config/rest.php
line406
Make logging available
# Make true if you want to use logging
# $config['rest_enable_logging'] = FALSE;
$config['rest_enable_logging'] = TRUE;
Credits to JC sin(π)
Put this in your API
# For setting dynamic origin
$http_origin = $_SERVER['HTTP_ORIGIN'];
if ($http_origin == "https://alveoland.com.ph" ||
$http_origin == "https://www.alveoland.com.ph")
header("AMP-Access-Control-Allow-Source-Origin: $http_origin");
# / For setting dynamic origin
<!-- Twitter block -->
<a class="twitter-timeline" data-width="650"
data-height="270" href="https://twitter.com/jjjjcccjjf">Tweets by @jjjjcccjjf</a>
<script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>
<!-- /Twitter block -->
To be able to fire up these snippets, you must boot up your terminal first.
On windows, you need to launch git bash. On Mac, click on the spotlight
then type in terminal
.
Copy and paste the ff. to your Terminal
cd /path/to/your/environment/
curl -O https://wordpress.org/latest.zip
unzip latest.zip
rm latest.zip
mv wordpress my-project
cd my-project
cp -i wp-config-sample.php wp-config.php
Assuming you're inside your project directory
git init
git add .
git commit -m "initial commit"
git remote add origin https://github.com/your-username/my-project.git
git push -u origin master
(Optional) For fixing forbidden in wp-admin
# BEGIN WordPress
# Uncomment this block if you cannot access wp-admin
# <Files wp-login.php>
# Order Deny,Allow
# Deny from all
# Allow from all
# </Files>
# / Uncomment this block if you cannot access wp-admin
<IfModule mod_rewrite.c>
RewriteEngine On
# Block for redirecting to https
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# / Block for redirecting to https
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# Uncomment this block for blocking wp-admin access except on some IP addresses
# RewriteCond %{REQUEST_URI} ^(.*)?wp-login\.php(.*)$ [OR]
# RewriteCond %{REQUEST_URI} ^(.*)?wp-admin$
# RewriteCond %{REMOTE_ADDR} !^124.106.142.167$
# RewriteRule ^(.*)$ - [R=403,L]
</IfModule>
# END WordPress
Paste this in your .htaccess anywhere I guess? 😅 The purpose of this block of code is to set expiration to caching. (maybe) You can read more of the topic here.
# Begin CACHE CONTROL
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/css A31536000
ExpiresByType text/x-component A31536000
ExpiresByType application/x-javascript A31536000
ExpiresByType application/javascript A31536000
ExpiresByType text/javascript A31536000
ExpiresByType text/x-js A31536000
ExpiresByType text/html A3600
ExpiresByType text/richtext A3600
ExpiresByType image/svg+xml A3600
ExpiresByType text/plain A3600
ExpiresByType text/xsd A3600
ExpiresByType text/xsl A3600
ExpiresByType text/xml A3600
ExpiresByType video/asf A31536000
ExpiresByType video/avi A31536000
ExpiresByType image/bmp A31536000
ExpiresByType application/java A31536000
ExpiresByType video/divx A31536000
ExpiresByType application/msword A31536000
ExpiresByType application/vnd.ms-fontobject A31536000
ExpiresByType application/x-msdownload A31536000
ExpiresByType image/gif A31536000
ExpiresByType application/x-gzip A31536000
ExpiresByType image/x-icon A31536000
ExpiresByType image/jpeg A31536000
ExpiresByType image/webp A31536000
ExpiresByType application/json A31536000
ExpiresByType application/vnd.ms-access A31536000
ExpiresByType audio/midi A31536000
ExpiresByType video/quicktime A31536000
ExpiresByType audio/mpeg A31536000
ExpiresByType video/mp4 A31536000
ExpiresByType video/mpeg A31536000
ExpiresByType application/vnd.ms-project A31536000
ExpiresByType application/x-font-otf A31536000
ExpiresByType application/vnd.ms-opentype A31536000
ExpiresByType application/vnd.oasis.opendocument.database A31536000
ExpiresByType application/vnd.oasis.opendocument.chart A31536000
ExpiresByType application/vnd.oasis.opendocument.formula A31536000
ExpiresByType application/vnd.oasis.opendocument.graphics A31536000
ExpiresByType application/vnd.oasis.opendocument.presentation A31536000
ExpiresByType application/vnd.oasis.opendocument.spreadsheet A31536000
ExpiresByType application/vnd.oasis.opendocument.text A31536000
ExpiresByType audio/ogg A31536000
ExpiresByType application/pdf A31536000
ExpiresByType image/png A31536000
ExpiresByType application/vnd.ms-powerpoint A31536000
ExpiresByType audio/x-realaudio A31536000
ExpiresByType image/svg+xml A31536000
ExpiresByType application/x-shockwave-flash A31536000
ExpiresByType application/x-tar A31536000
ExpiresByType image/tiff A31536000
ExpiresByType application/x-font-ttf A31536000
ExpiresByType application/vnd.ms-opentype A31536000
ExpiresByType audio/wav A31536000
ExpiresByType audio/wma A31536000
ExpiresByType application/vnd.ms-write A31536000
ExpiresByType application/font-woff A31536000
ExpiresByType application/font-woff2 A31536000
ExpiresByType application/vnd.ms-excel A31536000
ExpiresByType application/zip A31536000
</IfModule>
# End CACHE CONTROL
Put this in your .htaccess
<FilesMatch "^\.">
Order allow,deny
Deny from all
</FilesMatch>