Skip to content

Instantly share code, notes, and snippets.

View whoisryosuke's full-sized avatar
👾
Going deep with game development

Ryosuke whoisryosuke

👾
Going deep with game development
View GitHub Profile
@whoisryosuke
whoisryosuke / docker-compose.yml
Created April 9, 2018 18:02
Docker Compose file for basic Wordpress projects
my-wpdb:
image: mariadb
ports:
- "8081:3306"
environment:
MYSQL_ROOT_PASSWORD: root
my-wp:
image: wordpress
volumes:
- ./:/var/www/html
@whoisryosuke
whoisryosuke / template.php
Last active April 9, 2018 20:27
WORDPRESS: Check if user has certain role
<?php
# Credit: https://wordpress.stackexchange.com/questions/5047/how-to-check-if-a-user-is-in-a-specific-role
$user = wp_get_current_user();
if ( in_array( 'author', (array) $user->roles ) ) {
//The user has the "author" role
}
?>
@whoisryosuke
whoisryosuke / new-post.php
Created April 9, 2018 20:27
WORDPRESS: Create new post with meta data
<?php
# Credits: https://wordpress.stackexchange.com/questions/106973/wp-insert-post-or-similar-for-custom-post-type
$post_id = wp_insert_post(array (
'post_type' => 'your_post_type',
'post_title' => $your_title,
'post_content' => $your_content,
'post_status' => 'publish',
'comment_status' => 'closed', // if you prefer
@whoisryosuke
whoisryosuke / react-instagram-embed.js
Created April 25, 2018 23:31
Instagram component to quickly embed IG posts inside a component
export default class BlogPost extends Component {
constructor(props) {
super(props);
this.state = {
'instagram': false
};
}
@whoisryosuke
whoisryosuke / gatsby-image-example.js
Created April 26, 2018 17:52
Best practice for importing static images into GatsbyJS to ensure lazyloading/scaling. If image is responsive (like background cover image), use sizes property instead of resolutions in query and <Img /> component. See: https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-image
import React, { Component } from "react";
import Img from "gatsby-image";
export default class Frontpage extends Component {
render() {
return (
<div className="Frontpage pt2">
<Img resolutions={this.props.data.PeaceEmoji.resolutions} alt="Peace sign emoji" />
</div>
);
@whoisryosuke
whoisryosuke / laravel-eager-load-model-relationships.php
Created April 28, 2018 00:13
Laravel - Automatically eager load model relationships by setting a protected $with var on your model -- found via: https://stackoverflow.com/a/25674216
<?php
namespace YourApp;
use Illuminate\Database\Eloquent\Model;
class Posts extends Model
{
/**
* Automagically eager load any Model relationships
@whoisryosuke
whoisryosuke / incrementing-slugs.php
Last active March 21, 2022 13:17
Laravel: Check database for duplicate slugs and generate incrementing slug name (repeat-name-2, repeat-name-3). Found via: https://codereview.stackexchange.com/questions/162254/append-a-incrementing-number-to-a-slug-username
<?php
protected function create(array $data)
{
$slug = str_slug($data['first_name'] . '.' . $data['last_name']);
$next = 2;
// Loop until we can query for the slug and it returns false
while (User::where('slug', '=', $slug)->first()) {
$slug = $slug . '-' . $next;
$next++;
@whoisryosuke
whoisryosuke / array-destructuring-in-php7-specific.php
Created May 12, 2018 20:23
Specify the variables for specific keys in arrays in PHP7 -- found via: https://blog.frankdejonge.nl/array-destructuring-in-php/
<?php
$members = [
['id' => 1, 'name'=> 'Seb', 'twitter' => '@sebdedeyne' ],
['id' => 2, 'name'=> 'Alex', 'twitter' => '@alexvanderbist'],
['id' => 3, 'name'=> 'Brent', 'twitter' => '@brendt_gd'],
];
foreach ($members as ['twitter' => $twitterHandle, 'name' => $firstName]) {
// do stuff with $twitterHandle and $firstName
@whoisryosuke
whoisryosuke / array-destructuring-in-php7.php
Created May 12, 2018 20:24
Specify the variables for keys in arrays in PHP7 -- found via: https://blog.frankdejonge.nl/array-destructuring-in-php/
<?php
$members = [
[1, 'Seb'],
[2, 'Alex'],
[3, 'Brent'],
];
foreach ($members as [$id, $name]) {
// do stuff with $id and $name
@whoisryosuke
whoisryosuke / semantic-ui-state-search-dropdown.html
Created May 14, 2018 20:53
Semantic UI - State Search Dropdown (Requires Semantic CSS/JS + jQuery)
<form class="ui form">
<section class="field">
<label for="shipper_state_state">
State:
</label>
<div class="ui fluid search selection dropdown">
<input type="hidden" name="shipper_state_state">
<i class="dropdown icon"></i>
<div class="default text">Select State</div>
<div class="menu">