Skip to content

Instantly share code, notes, and snippets.

View mzalewski's full-sized avatar

Matthew Zalewski mzalewski

View GitHub Profile
@mzalewski
mzalewski / score.php
Last active April 21, 2017 00:47
Code Snippet showing Score in post content
add_filter( 'the_content', 'add_review_score_content' );
function add_review_score_content( $content ) {
global $post;
if ( ! is_singular() || $post->post_type !== 'reviews' )
{
// It's not a single Review so just return standard content
return $content;
{
"id": 1800,
"date": "2017-01-31T00:00:00",
"post_title": "My Page Title",
"post_content": "Featured Images",
"content_buckets": {
"images": [
{
"id": "cb5df5c5-3128-4dc1-b5e4-19144f1dbbbb",
"attachment_id": 1848,
@mzalewski
mzalewski / shortcode.php
Created January 28, 2017 20:56
Creating a shortcode
<?php
function myplugin_my_shortcode( $atts = array(), $content = '' ) {
echo "This is some shortcode output";
}
add_shortcode('my-shortcode','myplugin_my_shortcode');
@mzalewski
mzalewski / listtable.php
Created January 28, 2017 20:53
Create a custom WP List Table
<?php
if( ! class_exists( 'WP_List_Table' ) ) {
require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
}
class MyPlugin_List_Table extends WP_List_Table
{
public function prepare_items()
{
$columns = $this->get_columns();
@mzalewski
mzalewski / menu.php
Created January 28, 2017 20:45
Adding a menu page
<?php
function myplugin_options_page()
{
// add top level menu page
add_menu_page(
'Options Page',
'Options Page Title',
'manage_options',
'myplugin-options',
'myplugin_render_options_page'
@mzalewski
mzalewski / posttype.php
Last active January 28, 2017 20:38
WordPress Custom Post Type
<?php
function myplugin_add_post_type() {
register_post_type( 'reviews',
array(
'labels' => array(
'name' => __( 'Reviews' ),
'singular_name' => __( 'Review' )
),
'public' => true,
@mzalewski
mzalewski / enqueue.php
Last active January 28, 2017 20:39
Adding scripts/styles to a WordPress plugin
<?php
function myplugin_enqueue_scripts() {
$plugin_url = plugin_dir_url( __FILE__ );
wp_enqueue_style( 'cssname', $plugin_url . 'assets/css.css', array(), '1.0' );
wp_enqueue_script( 'jsname', $plugin_url . 'assets/js.js', array( 'jquery' ), '1.0' );
}
<?php
class jpen_Example_Widget extends WP_Widget {
// Set up the widget name and description.
public function __construct() {
$widget_options = array( 'classname' => 'example_widget', 'description' => 'This is an Example Widget' );
parent::__construct( 'example_widget', 'Example Widget', $widget_options );
}
// Create the widget output.
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance[ 'title' ] );
@mzalewski
mzalewski / Returner.php
Created December 12, 2016 05:13
Returner helper class
class Returner {
private static $returned = array();
private $value;
public function __construct( $value ) {
$this->value = $value;
}
public function result( $arg ) {
return $this->value;
}
public static function v( $value ) {
@mzalewski
mzalewski / node-wpapi-oauth-option1.js
Created October 24, 2016 23:10
Node WPAPI OAuth examples
var WPAPI = require( 'wpapi' );
var wp = new WPAPI({
endpoint: endpointUrl,
oauth: {
clientId: clientId,
clientSecret: clientSecret,
callback: callbackUrl // optional: (defaults to oob)
}
});